Package com.mku.streams
Class MemoryStream
java.lang.Object
com.mku.streams.RandomAccessStream
com.mku.streams.MemoryStream
Memory Stream for seeking, reading, and writing to a memory buffer (modeled after C# MemoryStream).
If the memory buffer is not specified then an internal resizable buffer will be created.
-
Nested Class Summary
Nested classes/interfaces inherited from class com.mku.streams.RandomAccessStream
RandomAccessStream.SeekOrigin
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionboolean
canRead()
True if the stream is readable.boolean
canSeek()
True if the stream is seekable.boolean
canWrite()
True if the stream is writable.void
close()
Close any resources the stream is using.void
flush()
Flush the stream.long
Get the current position of the stream.long
length()
Get the length of the stream.int
read
(byte[] buffer, int offset, int count) Read a sequence of bytes into the provided buffer.long
seek
(long offset, RandomAccessStream.SeekOrigin origin) Seek to a position in the stream.void
setLength
(long value) Changes the length of the stream.void
setPosition
(long value) Changes the current position of the stream.byte[]
toArray()
Convert the stream to an array:void
write
(byte[] buffer, int offset, int count) Write a sequence of bytes into the stream.Methods inherited from class com.mku.streams.RandomAccessStream
copyTo, copyTo, copyTo
-
Constructor Details
-
MemoryStream
public MemoryStream(byte[] bytes) Create a memory stream.- Parameters:
bytes
- Optional existing byte array to use as backing buffer. If omitted a new backing array will be created automatically.
-
MemoryStream
public MemoryStream()Create a memory stream.
-
-
Method Details
-
canRead
public boolean canRead()Description copied from class:RandomAccessStream
True if the stream is readable.- Specified by:
canRead
in classRandomAccessStream
- Returns:
- If the stream can be used for reading.
-
canWrite
public boolean canWrite()Description copied from class:RandomAccessStream
True if the stream is writable.- Specified by:
canWrite
in classRandomAccessStream
- Returns:
- If the stream can be used for writing.
-
canSeek
public boolean canSeek()Description copied from class:RandomAccessStream
True if the stream is seekable.- Specified by:
canSeek
in classRandomAccessStream
- Returns:
- If the stream is seekable.
-
length
public long length()Description copied from class:RandomAccessStream
Get the length of the stream.- Specified by:
length
in classRandomAccessStream
- Returns:
- The length of the stream.
-
getPosition
public long getPosition()Description copied from class:RandomAccessStream
Get the current position of the stream.- Specified by:
getPosition
in classRandomAccessStream
- Returns:
- The position of the stream.
-
setPosition
public void setPosition(long value) Changes the current position of the stream. For more options use seek() method.- Specified by:
setPosition
in classRandomAccessStream
- Parameters:
value
- The new position of the stream.
-
setLength
public void setLength(long value) Changes the length of the stream. The capacity of the stream might also change if the value is lesser than the current capacity.- Specified by:
setLength
in classRandomAccessStream
- Parameters:
value
- The new file length.
-
read
Read a sequence of bytes into the provided buffer.- Specified by:
read
in classRandomAccessStream
- Parameters:
buffer
- The buffer to write the bytes that are read from the stream.offset
- The offset of the buffer that will be used to write the bytes.count
- The length of the bytes that can be read from the stream and written to the buffer.- Returns:
- The number of bytes read.
- Throws:
IOException
- Thrown if there is an IO error.
-
write
public void write(byte[] buffer, int offset, int count) Write a sequence of bytes into the stream.- Specified by:
write
in classRandomAccessStream
- Parameters:
buffer
- The buffer that the bytes will be read from.offset
- The position offset that will be used to read from the buffer.count
- The number of bytes that will be written to the stream.
-
seek
Seek to a position in the stream.- Specified by:
seek
in classRandomAccessStream
- Parameters:
offset
- The offset to use.origin
- Possible Values: Begin, Current, End- Returns:
- The new position after seeking.
-
flush
public void flush()Flush the stream. Not-Applicable for memory stream.- Specified by:
flush
in classRandomAccessStream
-
close
public void close()Close any resources the stream is using. Not-Applicable for memory stream.- Specified by:
close
in classRandomAccessStream
-
toArray
public byte[] toArray()Convert the stream to an array:- Returns:
- A byte array containing the data from the stream.
-