System.IO
Class MemoryStream

java.lang.Object
  extended by System.IO.Stream
      extended by System.IO.MemoryStream
All Implemented Interfaces:
IDisposable

public class MemoryStream
extends Stream

Defines a stream whose backing store is memory.

Author:
Halofreak1990

Field Summary
 
Fields inherited from class System.IO.Stream
Null
 
Constructor Summary
MemoryStream()
          Initializes a new instance of the MemoryStream class with an expandable capacity initialized to zero.
MemoryStream(byte[] buffer)
          Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array.
MemoryStream(byte[] buffer, boolean writable)
          Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array with the CanWrite property set as specified.
MemoryStream(byte[] buffer, int index, int count)
          Initializes a new non-resizable instance of the MemoryStream class based on the specified region (index) of a byte array.
MemoryStream(byte[] buffer, int index, int count, boolean writable)
          Initializes a new non-resizable instance of the MemoryStream class based on the specified region of a byte array, with the CanWrite property set as specified.
MemoryStream(byte[] buffer, int index, int count, boolean writable, boolean publiclyVisible)
          Initializes a new non-resizable instance of the MemoryStream class based on the specified region of a byte array, with the CanWrite property set as specified, and the ability to call GetBuffer set as specified.
MemoryStream(int capacity)
          Initializes a new instance of the MemoryStream class with an expandable capacity initialized as specified.
 
Method Summary
 boolean CanRead()
           
 boolean CanSeek()
           
 boolean CanWrite()
           
protected  void Dispose(boolean disposing)
          Releases the unmanaged resources used by the MemoryStream and optionally releases the managed resources.
 void Flush()
          Overrides Flush so that no action is performed.
 byte[] GetBuffer()
          Returns the array of unsigned bytes from which this stream was created.
 int getCapacity()
          Gets the number of bytes allocated for this stream.
 long getPosition()
          Gets the current position within the stream.
 long Length()
           
 int Read(byte[] buffer, int offset, int count)
          Reads a block of bytes from the current stream and writes the data to buffer.
 int ReadByte()
          Reads a byte from the current stream.
 long Seek(long offset, SeekOrigin origin)
          Sets the position within the current stream to the specified value.
 void setCapacity(int value)
          Sets the number of bytes allocated for this stream.
 void SetLength(long value)
          Sets the length of the current stream to the specified value.
 void setPosition(long value)
          Sets the current position within the stream.
 byte[] ToArray()
          Writes the entire stream contents to a byte array, regardless of the Position property.
 void Write(byte[] buffer, int offset, int count)
          Writes a block of bytes to the current stream using data read from buffer.
 void WriteByte(byte value)
          Writes a byte to the current stream at the current position.
 void WriteTo(java.io.OutputStream stream)
          Writes the entire contents of this memory stream to a java.io.OutputStream.
 void WriteTo(Stream stream)
          Writes the entire contents of this memory stream to another stream.
 
Methods inherited from class System.IO.Stream
BeginRead, BeginWrite, CanTimeout, Close, Dispose, EndRead, EndWrite
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MemoryStream

public MemoryStream()
Initializes a new instance of the MemoryStream class with an expandable capacity initialized to zero.


MemoryStream

public MemoryStream(int capacity)
Initializes a new instance of the MemoryStream class with an expandable capacity initialized as specified.

Parameters:
capacity - The initial size of the internal array in bytes.
Throws:
ArgumentOutOfRangeException - capacity is negative.

MemoryStream

public MemoryStream(byte[] buffer)
Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array.

Parameters:
buffer - The array of unsigned bytes from which to create the current stream.
Throws:
ArgumentNullException - buffer is null.

MemoryStream

public MemoryStream(byte[] buffer,
                    boolean writable)
Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array with the CanWrite property set as specified.

Parameters:
buffer - The array of unsigned bytes from which to create this stream.
writable - The setting of the CanWrite property, which determines whether the stream supports writing.
Throws:
ArgumentNullException - buffer is null.

MemoryStream

public MemoryStream(byte[] buffer,
                    int index,
                    int count)
Initializes a new non-resizable instance of the MemoryStream class based on the specified region (index) of a byte array.

Parameters:
buffer - The array of unsigned bytes from which to create this stream.
index - The index into buffer at which the stream begins.
count - The length of the stream in bytes.
Throws:
ArgumentNullException - buffer is null.
ArgumentOutOfRangeException - index or count is less than zero.
ArgumentException - The sum of index and count is greater than the length of buffer.

MemoryStream

public MemoryStream(byte[] buffer,
                    int index,
                    int count,
                    boolean writable)
Initializes a new non-resizable instance of the MemoryStream class based on the specified region of a byte array, with the CanWrite property set as specified.

Parameters:
buffer - The array of bytes from which to create this stream.
index - The index into buffer at which the stream begins.
count - The length of the stream in bytes.
writable - The setting of the CanWrite property, which determines whether the stream supports writing.
Throws:
ArgumentNullException - buffer is null.
ArgumentOutOfRangeException - index or count is less than zero.
ArgumentException - The sum of index and count is greater than the length of buffer.

MemoryStream

public MemoryStream(byte[] buffer,
                    int index,
                    int count,
                    boolean writable,
                    boolean publiclyVisible)
Initializes a new non-resizable instance of the MemoryStream class based on the specified region of a byte array, with the CanWrite property set as specified, and the ability to call GetBuffer set as specified.

Parameters:
buffer - The array of bytes from which to create this stream.
index - The index into buffer at which the stream begins.
count - The length of the stream in bytes.
writable - The setting of the CanWrite property, which determines whether the stream supports writing.
publiclyVisible - true to enable GetBuffer, which returns the byte array from which the stream was created; otherwise, false.
Throws:
ArgumentNullException - buffer is null.
ArgumentOutOfRangeException - index or count is less than zero.
ArgumentException - The sum of index and count is greater than the length of buffer.
Method Detail

CanRead

public boolean CanRead()
Specified by:
CanRead in class Stream

CanSeek

public boolean CanSeek()
Specified by:
CanSeek in class Stream

CanWrite

public boolean CanWrite()
Specified by:
CanWrite in class Stream

getCapacity

public int getCapacity()
Gets the number of bytes allocated for this stream.

Returns:
The length of the usable portion of the buffer for the stream.
Throws:
ObjectDisposedException - The current stream is closed.

setCapacity

public void setCapacity(int value)
Sets the number of bytes allocated for this stream.

Throws:
ArgumentOutOfRangeException - A capacity is set that is negative or less than the current length of the stream.
ObjectDisposedException - The current stream is closed.
NotSupportedException - set is invoked on a stream whose capacity cannot be modified.

Length

public long Length()
Specified by:
Length in class Stream

getPosition

public long getPosition()
Gets the current position within the stream.

Specified by:
getPosition in class Stream
Returns:
The current position within the stream.
Throws:
ObjectDisposedException - The stream is closed.

setPosition

public void setPosition(long value)
Sets the current position within the stream.

Specified by:
setPosition in class Stream
Throws:
ObjectDisposedException - The stream is closed.
ArgumentOutOfRangeException - The position is set to a negative value or a value greater than MaxValue.

Dispose

protected void Dispose(boolean disposing)
Releases the unmanaged resources used by the MemoryStream and optionally releases the managed resources.

Overrides:
Dispose in class Stream
Parameters:
disposing - true to release both managed and unmanaged resources; false to release only unmanaged resources.

Flush

public void Flush()
Overrides Flush so that no action is performed.

Specified by:
Flush in class Stream

GetBuffer

public byte[] GetBuffer()
Returns the array of unsigned bytes from which this stream was created.

Returns:
The byte array from which this stream was created, or the underlying array if a byte array was not provided to the MemoryStream constructor during construction of the current instance.
Throws:
UnauthorizedAccessException - The MemoryStream instance was not created with a publicly visible buffer.

ReadByte

public int ReadByte()
Reads a byte from the current stream.

Overrides:
ReadByte in class Stream
Returns:
The byte cast to a Int32, or -1 if the end of the stream has been reached.
Throws:
ObjectDisposedException - The current stream instance is closed.

Read

public int Read(byte[] buffer,
                int offset,
                int count)
Reads a block of bytes from the current stream and writes the data to buffer.

Specified by:
Read in class Stream
Parameters:
buffer - When this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the characters read from the current stream.
offset - The byte offset in buffer at which to begin reading.
count - The maximum number of bytes to read.
Returns:
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.
Throws:
ObjectDisposedException - The current stream instance is closed.
ArgumentNullException - buffer is null.
ArgumentOutOfRangeException - offset or count is negative.
ArgumentException - offset subtracted from the buffer length is less than count.

Seek

public long Seek(long offset,
                 SeekOrigin origin)
Sets the position within the current stream to the specified value.

Specified by:
Seek in class Stream
Parameters:
offset - The new position within the stream. This is relative to the origin parameter, and can be positive or negative.
origin - A value of type SeekOrigin, which acts as the seek reference point.
Returns:
The new position within the stream, calculated by combining the initial reference point and the offset.
Throws:
ArgumentOutOfRangeException - offset is greater than MaxValue.
ObjectDisposedException - The current stream instance is closed.
IOException - Seeking is attempted before the beginning of the stream.
ArgumentException

SetLength

public void SetLength(long value)
Sets the length of the current stream to the specified value.

Specified by:
SetLength in class Stream
Parameters:
value - The value at which to set the length.
Throws:
ArgumentOutOfRangeException - value is negative or is greater than the maximum length of the MemoryStream, where the maximum length is (MaxValue - origin), and origin is the index into the underlying buffer at which the stream starts.
NotSupportedException - The current stream is not resizable and value is larger than the current capacity.-or- The current stream does not support writing.

ToArray

public byte[] ToArray()
Writes the entire stream contents to a byte array, regardless of the Position property.

Returns:
A new byte array.

WriteByte

public void WriteByte(byte value)
Writes a byte to the current stream at the current position.

Overrides:
WriteByte in class Stream
Parameters:
value - The byte to write.
Throws:
NotSupportedException - The stream does not support writing. For additional information see CanWrite.-or- The current position is at the end of the stream, and the capacity cannot be modified.
ObjectDisposedException - The current stream is closed.

Write

public void Write(byte[] buffer,
                  int offset,
                  int count)
Writes a block of bytes to the current stream using data read from buffer.

Specified by:
Write in class Stream
Parameters:
buffer - The buffer to write data from.
offset - The byte offset in buffer at which to begin writing from.
count - The maximum number of bytes to write.
Throws:
ObjectDisposedException - The current stream is closed.
NotSupportedException - The stream does not support writing. For additional information see CanWrite.-or- The current position is closer than count bytes to the end of the stream, and the capacity cannot be modified.
ArgumentNullException - buffer is null.
IOException - An I/O error occurs.

WriteTo

public void WriteTo(Stream stream)
Writes the entire contents of this memory stream to another stream.

Parameters:
stream - The stream to write this memory stream to.
Throws:
ArgumentNullException - stream is null.
ObjectDisposedException - The current or target stream is closed.

WriteTo

public void WriteTo(java.io.OutputStream stream)
Writes the entire contents of this memory stream to a java.io.OutputStream.

Parameters:
stream - The stream to write this memory stream to.
Throws:
ArgumentNullException - stream is null.
ObjectDisposedException - The current or target stream is closed.