Class MemoryStream

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class MemoryStream
    extends java.io.InputStream
    implements java.io.Closeable
    Creates 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.OutputStream asOutputStream()
      Get an OutputStream that will write to this MemoryStream, at the current position.
      int available()  
      void close()  
      byte[] getBuffer()
      Returns the array of bytes from which this stream was created.
      int getCapacity()
      Gets the number of bytes allocated for this stream.
      long getLength()
      Gets the length of the stream in bytes.
      long getPosition()
      Gets the current position within the stream.
      void mark​(int readAheadLimit)  
      boolean markSupported()  
      int read()  
      int read​(byte[] b, int off, int len)  
      void reset()  
      long seek​(long offset, SeekOrigin loc)
      Sets the position within the current stream to the specified value.
      void setCapacity​(int value)
      Sets the number of bytes allocated for this stream.
      void setLength​(long value)
      Sets the length of this stream to the given value.
      void setPosition​(long value)
      Sets the current position within the stream.
      long skip​(long n)  
      byte[] toByteArray()  
      void write​(byte[] buffer, int offset, int count)
      Writes a block of bytes to the current stream using data read from buffer.
      void writeByte​(byte value)  
      • Methods inherited from class java.io.InputStream

        nullInputStream, read, readAllBytes, readNBytes, readNBytes, transferTo
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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:
        available in class java.io.InputStream
      • close

        public void close()
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.InputStream
      • mark

        public void mark​(int readAheadLimit)
        Overrides:
        mark in class java.io.InputStream
      • markSupported

        public boolean markSupported()
        Overrides:
        markSupported in class java.io.InputStream
      • reset

        public void reset()
        Overrides:
        reset in class java.io.InputStream
      • read

        public int read()
        Specified by:
        read in class java.io.InputStream
      • read

        public int read​(byte[] b,
                        int off,
                        int len)
        Overrides:
        read in class java.io.InputStream
      • skip

        public long skip​(long n)
        Overrides:
        skip in class java.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.