using System;
using System.Runtime.InteropServices;
namespace System.IO
{
/// Creates a stream whose backing store is memory.
/// 2
// Token: 0x020001BF RID: 447
[ComVisible(true)]
[MonoTODO("Serialization format not compatible with .NET")]
[Serializable]
public class MemoryStream : Stream
{
/// Initializes a new instance of the class with an expandable capacity initialized to zero.
// Token: 0x060016F5 RID: 5877 RVA: 0x00058714 File Offset: 0x00056914
public MemoryStream()
: this(0)
{
}
/// Initializes a new instance of the class with an expandable capacity initialized as specified.
/// The initial size of the internal array in bytes.
///
/// is negative.
// Token: 0x060016F6 RID: 5878 RVA: 0x00058720 File Offset: 0x00056920
public MemoryStream(int capacity)
{
if (capacity < 0)
{
throw new ArgumentOutOfRangeException("capacity");
}
this.canWrite = true;
this.capacity = capacity;
this.internalBuffer = new byte[capacity];
this.expandable = true;
this.allowGetBuffer = true;
}
/// Initializes a new non-resizable instance of the class based on the specified byte array.
/// The array of unsigned bytes from which to create the current stream.
///
/// is null.
// Token: 0x060016F7 RID: 5879 RVA: 0x00058770 File Offset: 0x00056970
public MemoryStream(byte[] buffer)
{
if (buffer == null)
{
throw new ArgumentNullException("buffer");
}
this.InternalConstructor(buffer, 0, buffer.Length, true, false);
}
/// Initializes a new non-resizable instance of the class based on the specified byte array with the property set as specified.
/// The array of unsigned bytes from which to create this stream.
/// The setting of the property, which determines whether the stream supports writing.
///
/// is null.
// Token: 0x060016F8 RID: 5880 RVA: 0x000587A4 File Offset: 0x000569A4
public MemoryStream(byte[] buffer, bool writable)
{
if (buffer == null)
{
throw new ArgumentNullException("buffer");
}
this.InternalConstructor(buffer, 0, buffer.Length, writable, false);
}
/// Initializes a new non-resizable instance of the class based on the specified region (index) of a byte array.
/// The array of unsigned bytes from which to create this stream.
/// The index into at which the stream begins.
/// The length of the stream in bytes.
///
/// is null.
///
/// or is less than zero.
/// The sum of and is greater than the length of .
// Token: 0x060016F9 RID: 5881 RVA: 0x000587D8 File Offset: 0x000569D8
public MemoryStream(byte[] buffer, int index, int count)
{
this.InternalConstructor(buffer, index, count, true, false);
}
/// Initializes a new non-resizable instance of the class based on the specified region of a byte array, with the property set as specified.
/// The array of unsigned bytes from which to create this stream.
/// The index in at which the stream begins.
/// The length of the stream in bytes.
/// The setting of the property, which determines whether the stream supports writing.
///
/// is null.
///
/// or are negative.
/// The sum of and is greater than the length of .
// Token: 0x060016FA RID: 5882 RVA: 0x000587F8 File Offset: 0x000569F8
public MemoryStream(byte[] buffer, int index, int count, bool writable)
{
this.InternalConstructor(buffer, index, count, writable, false);
}
/// Initializes a new instance of the class based on the specified region of a byte array, with the property set as specified, and the ability to call set as specified.
/// The array of unsigned bytes from which to create this stream.
/// The index into at which the stream begins.
/// The length of the stream in bytes.
/// The setting of the property, which determines whether the stream supports writing.
/// true to enable , which returns the unsigned byte array from which the stream was created; otherwise, false.
///
/// is null.
///
/// or is negative.
/// The buffer length minus is less than .
// Token: 0x060016FB RID: 5883 RVA: 0x00058818 File Offset: 0x00056A18
public MemoryStream(byte[] buffer, int index, int count, bool writable, bool publiclyVisible)
{
this.InternalConstructor(buffer, index, count, writable, publiclyVisible);
}
// Token: 0x060016FC RID: 5884 RVA: 0x00058838 File Offset: 0x00056A38
private void InternalConstructor(byte[] buffer, int index, int count, bool writable, bool publicallyVisible)
{
if (buffer == null)
{
throw new ArgumentNullException("buffer");
}
if (index < 0 || count < 0)
{
throw new ArgumentOutOfRangeException("index or count is less than 0.");
}
if (buffer.Length - index < count)
{
throw new ArgumentException("index+count", "The size of the buffer is less than index + count.");
}
this.canWrite = writable;
this.internalBuffer = buffer;
this.capacity = count + index;
this.length = this.capacity;
this.position = index;
this.initialIndex = index;
this.allowGetBuffer = publicallyVisible;
this.expandable = false;
}
// Token: 0x060016FD RID: 5885 RVA: 0x000588CC File Offset: 0x00056ACC
private void CheckIfClosedThrowDisposed()
{
if (this.streamClosed)
{
throw new ObjectDisposedException("MemoryStream");
}
}
/// Gets a value indicating whether the current stream supports reading.
/// true if the stream is open.
/// 2
// Token: 0x17000412 RID: 1042
// (get) Token: 0x060016FE RID: 5886 RVA: 0x000588E4 File Offset: 0x00056AE4
public override bool CanRead
{
get
{
return !this.streamClosed;
}
}
/// Gets a value indicating whether the current stream supports seeking.
/// true if the stream is open.
/// 2
// Token: 0x17000413 RID: 1043
// (get) Token: 0x060016FF RID: 5887 RVA: 0x000588F0 File Offset: 0x00056AF0
public override bool CanSeek
{
get
{
return !this.streamClosed;
}
}
/// Gets a value indicating whether the current stream supports writing.
/// true if the stream supports writing; otherwise, false.
/// 2
// Token: 0x17000414 RID: 1044
// (get) Token: 0x06001700 RID: 5888 RVA: 0x000588FC File Offset: 0x00056AFC
public override bool CanWrite
{
get
{
return !this.streamClosed && this.canWrite;
}
}
/// Gets or sets the number of bytes allocated for this stream.
/// The length of the usable portion of the buffer for the stream.
/// A capacity is set that is negative or less than the current length of the stream.
/// The current stream is closed.
/// set is invoked on a stream whose capacity cannot be modified.
/// 2
// Token: 0x17000415 RID: 1045
// (get) Token: 0x06001701 RID: 5889 RVA: 0x00058914 File Offset: 0x00056B14
// (set) Token: 0x06001702 RID: 5890 RVA: 0x0005892C File Offset: 0x00056B2C
public virtual int Capacity
{
get
{
this.CheckIfClosedThrowDisposed();
return this.capacity - this.initialIndex;
}
set
{
this.CheckIfClosedThrowDisposed();
if (value == this.capacity)
{
return;
}
if (!this.expandable)
{
throw new NotSupportedException("Cannot expand this MemoryStream");
}
if (value < 0 || value < this.length)
{
throw new ArgumentOutOfRangeException("value", string.Concat(new object[] { "New capacity cannot be negative or less than the current capacity ", value, " ", this.capacity }));
}
byte[] array = null;
if (value != 0)
{
array = new byte[value];
Buffer.BlockCopy(this.internalBuffer, 0, array, 0, this.length);
}
this.dirty_bytes = 0;
this.internalBuffer = array;
this.capacity = value;
}
}
/// Gets the length of the stream in bytes.
/// The length of the stream in bytes.
/// The stream is closed.
/// 2
// Token: 0x17000416 RID: 1046
// (get) Token: 0x06001703 RID: 5891 RVA: 0x000589EC File Offset: 0x00056BEC
public override long Length
{
get
{
this.CheckIfClosedThrowDisposed();
return (long)(this.length - this.initialIndex);
}
}
/// Gets or sets the current position within the stream.
/// The current position within the stream.
/// The position is set to a negative value or a value greater than .
/// The stream is closed.
/// 2
// Token: 0x17000417 RID: 1047
// (get) Token: 0x06001704 RID: 5892 RVA: 0x00058A04 File Offset: 0x00056C04
// (set) Token: 0x06001705 RID: 5893 RVA: 0x00058A1C File Offset: 0x00056C1C
public override long Position
{
get
{
this.CheckIfClosedThrowDisposed();
return (long)(this.position - this.initialIndex);
}
set
{
this.CheckIfClosedThrowDisposed();
if (value < 0L)
{
throw new ArgumentOutOfRangeException("value", "Position cannot be negative");
}
if (value > 2147483647L)
{
throw new ArgumentOutOfRangeException("value", "Position must be non-negative and less than 2^31 - 1 - origin");
}
this.position = this.initialIndex + (int)value;
}
}
/// Releases the unmanaged resources used by the class and optionally releases the managed resources.
/// true to release both managed and unmanaged resources; false to release only unmanaged resources.
// Token: 0x06001706 RID: 5894 RVA: 0x00058A74 File Offset: 0x00056C74
protected override void Dispose(bool disposing)
{
this.streamClosed = true;
this.expandable = false;
}
/// Overrides so that no action is performed.
/// 2
// Token: 0x06001707 RID: 5895 RVA: 0x00058A84 File Offset: 0x00056C84
public override void Flush()
{
}
/// Returns the array of unsigned bytes from which this stream was created.
/// The byte array from which this stream was created, or the underlying array if a byte array was not provided to the constructor during construction of the current instance.
/// The MemoryStream instance was not created with a publicly visible buffer.
/// 2
// Token: 0x06001708 RID: 5896 RVA: 0x00058A88 File Offset: 0x00056C88
public virtual byte[] GetBuffer()
{
if (!this.allowGetBuffer)
{
throw new UnauthorizedAccessException();
}
return this.internalBuffer;
}
/// Reads a block of bytes from the current stream and writes the data to .
/// The total number of bytes written into the buffer. This can be less than the number of bytes requested if that number of bytes are not currently available, or zero if the end of the stream is reached before any bytes are read.
/// When this method returns, contains the specified byte array with the values between and ( + - 1) replaced by the characters read from the current stream.
/// The byte offset in at which to begin reading.
/// The maximum number of bytes to read.
///
/// is null.
///
/// or is negative.
///
/// subtracted from the buffer length is less than .
/// The current stream instance is closed.
/// 2
// Token: 0x06001709 RID: 5897 RVA: 0x00058AA4 File Offset: 0x00056CA4
public override int Read([In] [Out] byte[] buffer, int offset, int count)
{
this.CheckIfClosedThrowDisposed();
if (buffer == null)
{
throw new ArgumentNullException("buffer");
}
if (offset < 0 || count < 0)
{
throw new ArgumentOutOfRangeException("offset or count less than zero.");
}
if (buffer.Length - offset < count)
{
throw new ArgumentException("offset+count", "The size of the buffer is less than offset + count.");
}
if (this.position >= this.length || count == 0)
{
return 0;
}
if (this.position > this.length - count)
{
count = this.length - this.position;
}
Buffer.BlockCopy(this.internalBuffer, this.position, buffer, offset, count);
this.position += count;
return count;
}
/// Reads a byte from the current stream.
/// The byte cast to a , or -1 if the end of the stream has been reached.
/// The current stream instance is closed.
/// 2
// Token: 0x0600170A RID: 5898 RVA: 0x00058B5C File Offset: 0x00056D5C
public override int ReadByte()
{
this.CheckIfClosedThrowDisposed();
if (this.position >= this.length)
{
return -1;
}
return (int)this.internalBuffer[this.position++];
}
/// Sets the position within the current stream to the specified value.
/// The new position within the stream, calculated by combining the initial reference point and the offset.
/// The new position within the stream. This is relative to the parameter, and can be positive or negative.
/// A value of type , which acts as the seek reference point.
/// Seeking is attempted before the beginning of the stream.
///
/// is greater than .
/// There is an invalid . -or- caused an arithmetic overflow.
/// The current stream instance is closed.
/// 2
// Token: 0x0600170B RID: 5899 RVA: 0x00058B9C File Offset: 0x00056D9C
public override long Seek(long offset, SeekOrigin loc)
{
this.CheckIfClosedThrowDisposed();
if (offset > 2147483647L)
{
throw new ArgumentOutOfRangeException("Offset out of range. " + offset);
}
int num;
switch (loc)
{
case SeekOrigin.Begin:
if (offset < 0L)
{
throw new IOException("Attempted to seek before start of MemoryStream.");
}
num = this.initialIndex;
break;
case SeekOrigin.Current:
num = this.position;
break;
case SeekOrigin.End:
num = this.length;
break;
default:
throw new ArgumentException("loc", "Invalid SeekOrigin");
}
num += (int)offset;
if (num < this.initialIndex)
{
throw new IOException("Attempted to seek before start of MemoryStream.");
}
this.position = num;
return (long)this.position;
}
// Token: 0x0600170C RID: 5900 RVA: 0x00058C5C File Offset: 0x00056E5C
private int CalculateNewCapacity(int minimum)
{
if (minimum < 256)
{
minimum = 256;
}
if (minimum < this.capacity * 2)
{
minimum = this.capacity * 2;
}
return minimum;
}
// Token: 0x0600170D RID: 5901 RVA: 0x00058C8C File Offset: 0x00056E8C
private void Expand(int newSize)
{
if (newSize > this.capacity)
{
this.Capacity = this.CalculateNewCapacity(newSize);
}
else if (this.dirty_bytes > 0)
{
Array.Clear(this.internalBuffer, this.length, this.dirty_bytes);
this.dirty_bytes = 0;
}
}
/// Sets the length of the current stream to the specified value.
/// The value at which to set the length.
/// The current stream is not resizable and is larger than the current capacity.-or- The current stream does not support writing.
///
/// is negative or is greater than the maximum length of the , where the maximum length is( - origin), and origin is the index into the underlying buffer at which the stream starts.
/// 2
// Token: 0x0600170E RID: 5902 RVA: 0x00058CE4 File Offset: 0x00056EE4
public override void SetLength(long value)
{
if (!this.expandable && value > (long)this.capacity)
{
throw new NotSupportedException("Expanding this MemoryStream is not supported");
}
this.CheckIfClosedThrowDisposed();
if (!this.canWrite)
{
throw new NotSupportedException(Locale.GetText("Cannot write to this MemoryStream"));
}
if (value < 0L || value + (long)this.initialIndex > 2147483647L)
{
throw new ArgumentOutOfRangeException();
}
int num = (int)value + this.initialIndex;
if (num > this.length)
{
this.Expand(num);
}
else if (num < this.length)
{
this.dirty_bytes += this.length - num;
}
this.length = num;
if (this.position > this.length)
{
this.position = this.length;
}
}
/// Writes the stream contents to a byte array, regardless of the property.
/// A new byte array.
/// 2
// Token: 0x0600170F RID: 5903 RVA: 0x00058DC0 File Offset: 0x00056FC0
public virtual byte[] ToArray()
{
int num = this.length - this.initialIndex;
byte[] array = new byte[num];
if (this.internalBuffer != null)
{
Buffer.BlockCopy(this.internalBuffer, this.initialIndex, array, 0, num);
}
return array;
}
/// Writes a block of bytes to the current stream using data read from buffer.
/// The buffer to write data from.
/// The byte offset in at which to begin writing from.
/// The maximum number of bytes to write.
///
/// is null.
/// The stream does not support writing. For additional information see .-or- The current position is closer than bytes to the end of the stream, and the capacity cannot be modified.
///
/// subtracted from the buffer length is less than .
///
/// or are negative.
/// An I/O error occurs.
/// The current stream instance is closed.
/// 2
// Token: 0x06001710 RID: 5904 RVA: 0x00058E04 File Offset: 0x00057004
public override void Write(byte[] buffer, int offset, int count)
{
this.CheckIfClosedThrowDisposed();
if (!this.canWrite)
{
throw new NotSupportedException("Cannot write to this stream.");
}
if (buffer == null)
{
throw new ArgumentNullException("buffer");
}
if (offset < 0 || count < 0)
{
throw new ArgumentOutOfRangeException();
}
if (buffer.Length - offset < count)
{
throw new ArgumentException("offset+count", "The size of the buffer is less than offset + count.");
}
if (this.position > this.length - count)
{
this.Expand(this.position + count);
}
Buffer.BlockCopy(buffer, offset, this.internalBuffer, this.position, count);
this.position += count;
if (this.position >= this.length)
{
this.length = this.position;
}
}
/// Writes a byte to the current stream at the current position.
/// The byte to write.
/// The stream does not support writing. For additional information see .-or- The current position is at the end of the stream, and the capacity cannot be modified.
/// The current stream is closed.
/// 2
// Token: 0x06001711 RID: 5905 RVA: 0x00058ED0 File Offset: 0x000570D0
public override void WriteByte(byte value)
{
this.CheckIfClosedThrowDisposed();
if (!this.canWrite)
{
throw new NotSupportedException("Cannot write to this stream.");
}
if (this.position >= this.length)
{
this.Expand(this.position + 1);
this.length = this.position + 1;
}
this.internalBuffer[this.position++] = value;
}
/// Writes the entire contents of this memory stream to another stream.
/// The stream to write this memory stream to.
///
/// is null.
/// The current or target stream is closed.
/// 2
// Token: 0x06001712 RID: 5906 RVA: 0x00058F40 File Offset: 0x00057140
public virtual void WriteTo(Stream stream)
{
this.CheckIfClosedThrowDisposed();
if (stream == null)
{
throw new ArgumentNullException("stream");
}
stream.Write(this.internalBuffer, this.initialIndex, this.length - this.initialIndex);
}
// Token: 0x040006AD RID: 1709
private bool canWrite;
// Token: 0x040006AE RID: 1710
private bool allowGetBuffer;
// Token: 0x040006AF RID: 1711
private int capacity;
// Token: 0x040006B0 RID: 1712
private int length;
// Token: 0x040006B1 RID: 1713
private byte[] internalBuffer;
// Token: 0x040006B2 RID: 1714
private int initialIndex;
// Token: 0x040006B3 RID: 1715
private bool expandable;
// Token: 0x040006B4 RID: 1716
private bool streamClosed;
// Token: 0x040006B5 RID: 1717
private int position;
// Token: 0x040006B6 RID: 1718
private int dirty_bytes;
}
}