Class MemoryStream
- java.lang.Object
-
- java.io.InputStream
-
- in.dragonbra.javasteam.util.stream.MemoryStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class MemoryStream extends java.io.InputStream implements java.io.CloseableCreates a stream whose backing store is memory.
-
-
Constructor Summary
Constructors Constructor 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 writable 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 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 writable set as specified.MemoryStream(byte[] buffer, int index, int count, boolean writable, boolean publiclyVisible)Initializes a new instance of the MemoryStream class based on the specified region of a byte array, with writable set as specified, and publiclyVisible set as specified.MemoryStream(int capacity)Initializes a new instance of the MemoryStream class with an expandable capacity initialized as specified.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.io.OutputStreamasOutputStream()Get an OutputStream that will write to this MemoryStream, at the current position.intavailable()voidclose()byte[]getBuffer()Returns the array of bytes from which this stream was created.intgetCapacity()Gets the number of bytes allocated for this stream.longgetLength()Gets the length of the stream in bytes.longgetPosition()Gets the current position within the stream.voidmark(int readAheadLimit)booleanmarkSupported()intread()intread(byte[] b, int off, int len)voidreset()longseek(long offset, SeekOrigin loc)Sets the position within the current stream to the specified value.voidsetCapacity(int value)Sets the number of bytes allocated for this stream.voidsetLength(long value)Sets the length of this stream to the given value.voidsetPosition(long value)Sets the current position within the stream.longskip(long n)byte[]toByteArray()voidwrite(byte[] buffer, int offset, int count)Writes a block of bytes to the current stream using data read from buffer.voidwriteByte(byte value)
-
-
-
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.
-
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.
-
MemoryStream
public MemoryStream(byte[] buffer, boolean writable)Initializes a new non-resizable instance of the MemoryStream class based on the specified byte array, with writable set as specified.- Parameters:
buffer- The array of unsigned bytes from which to create this stream.writable- True if the stream supports writing, false otherwise.
-
MemoryStream
public MemoryStream(byte[] buffer, int index, int count)Initializes a new non-resizable instance of the MemoryStream class based on the specified region 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.
-
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 writable set as specified.- 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.writable- True if the stream supports writing, false otherwise.
-
MemoryStream
public MemoryStream(byte[] buffer, int index, int count, boolean writable, boolean publiclyVisible)Initializes a new instance of the MemoryStream class based on the specified region of a byte array, with writable set as specified, and publiclyVisible set as specified.- 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.writable- Whether the stream supports writing.publiclyVisible- Whether the stream allows direct access to the underlying buffer.
-
-
Method Detail
-
available
public int available()
- Overrides:
availablein classjava.io.InputStream
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream
-
mark
public void mark(int readAheadLimit)
- Overrides:
markin classjava.io.InputStream
-
markSupported
public boolean markSupported()
- Overrides:
markSupportedin classjava.io.InputStream
-
reset
public void reset()
- Overrides:
resetin classjava.io.InputStream
-
read
public int read()
- Specified by:
readin classjava.io.InputStream
-
read
public int read(byte[] b, int off, int len)- Overrides:
readin classjava.io.InputStream
-
skip
public long skip(long n)
- Overrides:
skipin classjava.io.InputStream
-
getBuffer
public byte[] getBuffer()
Returns the array of bytes from which this stream was created.- Returns:
- byte[] 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.
-
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.
-
setCapacity
public void setCapacity(int value)
Sets the number of bytes allocated for this stream.- Parameters:
value- The new length of the usable portion of the buffer for the stream.
-
getLength
public long getLength()
Gets the length of the stream in bytes.- Returns:
- The length of the stream in bytes.
-
setLength
public void setLength(long value)
Sets the length of this stream to the given value.- Parameters:
value- The new length of the stream.
-
getPosition
public long getPosition()
Gets the current position within the stream.- Returns:
- The current position within the stream.
-
setPosition
public void setPosition(long value)
Sets the current position within the stream.- Parameters:
value- The new position within the stream.
-
seek
public long seek(long offset, SeekOrigin loc)Sets the position within the current stream to the specified value.- Parameters:
offset- The new position within the stream. This is relative to the loc parameter, and can be positive or negative.loc- 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.
-
write
public void write(byte[] buffer, int offset, int count)Writes a block of bytes to the current stream using data read from buffer.- Parameters:
buffer- The buffer to write data from.offset- The zero-based byte offset in buffer at which to begin copying bytes to the current stream.count- The maximum number of bytes to write.
-
writeByte
public void writeByte(byte value)
-
toByteArray
public byte[] toByteArray()
-
asOutputStream
public java.io.OutputStream asOutputStream()
Get an OutputStream that will write to this MemoryStream, at the current position.- Returns:
- an OutputStream that will write in-place to this MemoryStream.
-
-