Class SeekableByteArrayOutputStream

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private byte[] buffer  
      private boolean closed  
      private int incrementStep  
      private int offset  
      private int size  
    • Constructor Summary

      Constructors 
      Constructor Description
      SeekableByteArrayOutputStream()
      Creates a new object of this class, setting initial capacity and increment size to default values.
      SeekableByteArrayOutputStream​(int initialCapacity)
      Creates a new object of this class, setting initial capacity to the argument value.
      SeekableByteArrayOutputStream​(int initialCapacity, int increment)
      Creates a new object of this class, setting initial capacity and increment to the argument values.
    • Field Detail

      • buffer

        private byte[] buffer
      • closed

        private boolean closed
      • incrementStep

        private int incrementStep
      • offset

        private int offset
      • size

        private int size
    • Constructor Detail

      • SeekableByteArrayOutputStream

        public SeekableByteArrayOutputStream()
        Creates a new object of this class, setting initial capacity and increment size to default values.
      • SeekableByteArrayOutputStream

        public SeekableByteArrayOutputStream​(int initialCapacity)
        Creates a new object of this class, setting initial capacity to the argument value. The increment size is set to the initial capacity as well if that value is larger than 0. Otherwise it is set to a default value.
        Parameters:
        initialCapacity - the number of bytes that are allocated in this constructor (0 or larger)
      • SeekableByteArrayOutputStream

        public SeekableByteArrayOutputStream​(int initialCapacity,
                                             int increment)
        Creates a new object of this class, setting initial capacity and increment to the argument values.
        Parameters:
        initialCapacity - the number of bytes that are allocated in this constructor (0 or larger)
        increment - the number of bytes by which the internal byte array is increased if it is full (1 or larger)
    • Method Detail

      • getPosition

        public int getPosition()
        Returns the current offset in the output stream. Larger than or equal to 0 and smaller than or equal to getSize().
        Returns:
        current position in the output stream, 0-based
      • getSize

        public int getSize()
        Returns the current size of the output stream.
        Returns:
        size of the output stream in bytes (0 or larger)
      • increaseBuffer

        private void increaseBuffer​(int newLength)
      • seek

        public void seek​(int newOffset)
                  throws IOException
        Sets the current position in the output stream to the argument.
        Parameters:
        newOffset - new offset into the file, must be >= 0 and <= getSize()
        Throws:
        IOException - if the argument is invalid
      • toByteArray

        public byte[] toByteArray()
        Allocates a new byte[] object, copies getSize() bytes from the internal byte array to that new array and returns the array.
        Returns:
        a copy of the byte[] data stored internally
      • truncate

        public void truncate()
        Removes all bytes after the current position. After a call to this method, getSize() is equal to getPosition().
      • write

        public void write​(int b)
                   throws IOException
        Writes the least significant eight bits of the argument int to the internal array.
        Specified by:
        write in class OutputStream
        Parameters:
        b - int variable that stores the byte value to be written
        Throws:
        IOException
      • write

        public void write​(byte[] data)
                   throws IOException
        Write the complete argument array to this stream. Copies the data to the internal byte array. Simply calls write(data, 0, data.length);.
        Overrides:
        write in class OutputStream
        Parameters:
        data - array to be copied to this stream
        Throws:
        IOException
      • write

        public void write​(byte[] src,
                          int srcOffset,
                          int num)
                   throws IOException
        Write some bytes from the argument array to this stream. Copies num bytes starting at src[srcOffset] to this stream.
        Overrides:
        write in class OutputStream
        Parameters:
        src - the array from which data is copied
        srcOffset - int index into that array pointing to the first byte to be copied
        num - number of bytes to be copied
        Throws:
        IOException
      • writeTo

        public void writeTo​(OutputStream out)
                     throws IOException
        Writes the bytes in the internal byte array to the argument output stream. A call to this method has the same effect as
         byte[] copy = toByteArray();
         out.write(copy, 0, copy.length);
         
        However, you with this method you save the allocation of an additional byte array and the copying to that new array.
        Parameters:
        out - the output stream to which this stream's content is copied
        Throws:
        IOException - if out has a problem writing the bytes