public class MemoryStream extends Stream
Constructor and Description |
---|
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.
|
Modifier and Type | Method and Description |
---|---|
boolean |
CanRead()
When overridden in a derived class, gets a value indicating whether the current stream supports reading.
|
boolean |
CanSeek()
When overridden in a derived class, gets a value indicating whether the current stream supports seeking.
|
boolean |
CanWrite()
When overridden in a derived class, gets a value indicating whether the current stream supports writing.
|
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()
When overridden in a derived class, gets the length in bytes of the stream.
|
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.
|
BeginRead, BeginWrite, CanTimeout, Close, Dispose, EndRead, EndWrite
public MemoryStream()
public MemoryStream(int capacity)
capacity
- The initial size of the internal array in bytes.ArgumentOutOfRangeException
- capacity is negative.public MemoryStream(byte[] buffer)
buffer
- The array of unsigned bytes from which to create the current stream.ArgumentNullException
- buffer is null.public MemoryStream(byte[] buffer, boolean writable)
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.ArgumentNullException
- buffer is null.public MemoryStream(byte[] buffer, int index, int count)
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.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.public MemoryStream(byte[] buffer, int index, int count, boolean writable)
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.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.public MemoryStream(byte[] buffer, int index, int count, boolean writable, boolean publiclyVisible)
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.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.public boolean CanRead()
Stream
public boolean CanSeek()
Stream
public boolean CanWrite()
Stream
public int getCapacity()
ObjectDisposedException
- The current stream is closed.public void setCapacity(int value)
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.public long Length()
Stream
public long getPosition()
getPosition
in class Stream
ObjectDisposedException
- The stream is closed.public void setPosition(long value)
setPosition
in class Stream
value
- The new position within the stream.ObjectDisposedException
- The stream is closed.ArgumentOutOfRangeException
- The position is set to a negative value or a value greater than MaxValue.protected void Dispose(boolean disposing)
public void Flush()
public byte[] GetBuffer()
UnauthorizedAccessException
- The MemoryStream instance was not created with a publicly visible buffer.public int ReadByte()
ReadByte
in class Stream
ObjectDisposedException
- The current stream instance is closed.public int Read(byte[] buffer, int offset, int count)
Read
in class Stream
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.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.public long Seek(long offset, SeekOrigin origin)
Seek
in class Stream
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.ArgumentOutOfRangeException
- offset is greater than MaxValue.ObjectDisposedException
- The current stream instance is closed.IOException
- Seeking is attempted before the beginning of the stream.ArgumentException
public void SetLength(long value)
SetLength
in class Stream
value
- The value at which to set the length.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.public byte[] ToArray()
public void WriteByte(byte value)
WriteByte
in class Stream
value
- The byte to write.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.public void Write(byte[] buffer, int offset, int count)
Write
in class Stream
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.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.public void WriteTo(Stream stream)
stream
- The stream to write this memory stream to.ArgumentNullException
- stream is null.ObjectDisposedException
- The current or target stream is closed.public void WriteTo(java.io.OutputStream stream)
stream
- The stream to write this memory stream to.ArgumentNullException
- stream is null.ObjectDisposedException
- The current or target stream is closed.