Class MemoryStream

java.lang.Object
com.pdftools.sys.MemoryStream
All Implemented Interfaces:
Stream, AutoCloseable

public class MemoryStream extends Object implements Stream
The stream implementation for in-memory processing. This can be used to read from or write to byte arrays.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create 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.
    Create a new memory stream by copying a stream.
    Create a new memory stream by copying the given stream.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Close the stream and release all associated resources.
    long
    Get the length of the stream in bytes.
    int
    read(byte[] buffer)
    Read from the stream.
    int
    read(byte[] buffer, int offset, int length)
    Read from the stream.
    boolean
    seek(long position)
    Set byte position.
    long
    Get current byte position.
    byte[]
    Creates a newly allocated byte array.
    int
    Read data from the input stream and write it to this stream.
    long
    Read data from this stream and write it to the output stream.
    void
    write(byte[] buffer, int offset, int length)
    Write to the stream.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 copied
      offset - The offset where the first byte from the buffer is copied
      length - 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

      public MemoryStream(Stream stream) throws IOException
      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

      public MemoryStream(InputStream inStream) throws IOException
      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.
      Specified by:
      getLength in interface Stream
      Returns:
      the length of the stream in bytes
    • seek

      public boolean seek(long position)
      Set byte position.
      Specified by:
      seek in interface Stream
      Parameters:
      position - The new position of the stream (-1 for EOS)
      Returns:
      true if successful
    • tell

      public long tell()
      Get current byte position.
      Specified by:
      tell in interface Stream
      Returns:
      byte position, -1 if position unknown
    • read

      public int read(byte[] buffer, int offset, int length)
      Read from the stream.
      Specified by:
      read in interface Stream
      Parameters:
      buffer - The buffer where the data is written
      offset - The starting element in the buffer
      length - The maximum number of bytes to be read
      Returns:
      The actual number of bytes read (-1 if EOS)
    • 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

      public int transferFrom(InputStream inStream) throws IOException
      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

      public long transferTo(OutputStream outStream) throws IOException
      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 using seek(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.
      Specified by:
      write in interface Stream
      Parameters:
      buffer - The buffer where the data lies
      offset - The starting element in the buffer
      length - The maximum number of bytes to be written
    • close

      public void close() throws IOException
      Description copied from interface: Stream
      Close the stream and release all associated resources.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Stream
      Throws:
      IOException