Package com.pdftools.sys
Class MemoryStream
java.lang.Object
com.pdftools.sys.MemoryStream
- All Implemented Interfaces:
Stream,AutoCloseable
The stream implementation for in-memory processing.
This can be used to read from or write to byte arrays.
-
Constructor Summary
ConstructorsConstructorDescriptionCreate a new memory stream with initial capacity of 0.MemoryStream(byte[] buffer) Create a new memory stream by copying from a buffer.MemoryStream(byte[] buffer, int offset, int length) Create a new memory stream by copying from a buffer.MemoryStream(Stream stream) Create a new memory stream by copying a stream.MemoryStream(InputStream inStream) Create a new memory stream by copying the given stream. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Close the stream and release all associated resources.longGet the length of the stream in bytes.intread(byte[] buffer) Read from the stream.intread(byte[] buffer, int offset, int length) Read from the stream.booleanseek(long position) Set byte position.longtell()Get current byte position.byte[]Creates a newly allocated byte array.inttransferFrom(InputStream inStream) Read data from the input stream and write it to this stream.longtransferTo(OutputStream outStream) Read data from this stream and write it to the output stream.voidwrite(byte[] buffer, int offset, int length) Write to the stream.
-
Constructor Details
-
MemoryStream
public MemoryStream()Create a new memory stream with initial capacity of 0. -
MemoryStream
public MemoryStream(byte[] buffer, int offset, int length) Create a new memory stream by copying from a buffer.- Parameters:
buffer- The buffer from which the initial data is copiedoffset- The offset where the first byte from the buffer is copiedlength- The number of bytes that are copied from the buffer
-
MemoryStream
public MemoryStream(byte[] buffer) Create a new memory stream by copying from a buffer.- Parameters:
buffer- The buffer from which the initial data is copied
-
MemoryStream
Create a new memory stream by copying a stream.- Parameters:
stream- The stream from which the initial data is copied- Throws:
IOException- if an I/O error occurs
-
MemoryStream
Create a new memory stream by copying the given stream.- Parameters:
inStream- The stream from which the initial data is copied- Throws:
IOException- if an I/O error occurs
-
-
Method Details
-
getLength
public long getLength()Get the length of the stream in bytes. -
seek
public boolean seek(long position) Set byte position. -
tell
public long tell()Get current byte position. -
read
public int read(byte[] buffer, int offset, int length) Read from the stream. -
read
public int read(byte[] buffer) Read from the stream.- Parameters:
buffer- The buffer where the data is written to- Returns:
- The actual number of bytes read (-1 if EOS)
-
toByteArray
public byte[] toByteArray()Creates a newly allocated byte array. Its size is the current size of this stream and the contents have been copied into it.- Returns:
- The current contents of this stream as a byte array.
- Throws:
OutOfMemoryError- if an array larger than 2GB would be required to store the bytes.
-
transferFrom
Read data from the input stream and write it to this stream. The data is appended at the stream's current byte position.- Parameters:
inStream- The stream from which the data is copied- Returns:
- The actual number of transferred bytes
- Throws:
IOException- if an I/O error occurs
-
transferTo
Read data from this stream and write it to the output stream. The data is read starting from the stream's current byte position. In order to read the entire stream, make sure to first position the stream at the beginning usingseek(0).- Parameters:
outStream- The stream to which the data is copied- Returns:
- The actual number of transfered bytes
- Throws:
IOException- if an I/O error occurs
-
write
public void write(byte[] buffer, int offset, int length) Write to the stream. -
close
Description copied from interface:StreamClose the stream and release all associated resources.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceStream- Throws:
IOException
-