Class MemoryStream

  • All Implemented Interfaces:
    Stream, java.lang.AutoCloseable

    public class MemoryStream
    extends java.lang.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
      MemoryStream()
      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.
      MemoryStream​(Stream stream)
      Create a new memory stream by copying a stream.
      MemoryStream​(java.io.InputStream inStream)
      Create a new memory stream by copying the given stream.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Close the stream and release all associated resources.
      long getLength()
      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 tell()
      Get current byte position.
      byte[] toByteArray()
      Creates a newly allocated byte array.
      int transferFrom​(java.io.InputStream inStream)
      Read data from the input stream and write it to this stream.
      long transferTo​(java.io.OutputStream outStream)
      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 Detail

      • 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 java.io.IOException
        Create a new memory stream by copying a stream.
        Parameters:
        stream - The stream from which the initial data is copied
        Throws:
        java.io.IOException - if an I/O error occurs
      • MemoryStream

        public MemoryStream​(java.io.InputStream inStream)
                     throws java.io.IOException
        Create a new memory stream by copying the given stream.
        Parameters:
        inStream - The stream from which the initial data is copied
        Throws:
        java.io.IOException - if an I/O error occurs
    • Method Detail

      • 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:
        java.lang.OutOfMemoryError - if an array larger than 2GB would be required to store the bytes.
      • transferFrom

        public int transferFrom​(java.io.InputStream inStream)
                         throws java.io.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:
        java.io.IOException - if an I/O error occurs
      • transferTo

        public long transferTo​(java.io.OutputStream outStream)
                        throws java.io.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:
        java.io.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 java.io.IOException
        Description copied from interface: Stream
        Close the stream and release all associated resources.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface Stream
        Throws:
        java.io.IOException