Class ByteOutputBuffer

java.lang.Object
jj2000.j2k.entropy.encoder.ByteOutputBuffer

public class ByteOutputBuffer extends Object
This class provides a buffering output stream similar to ByteArrayOutputStream, with some additional methods.

Once an array has been written to an output stream or to a byte array, the object can be reused as a new stream if the reset() method is called.

Unlike the ByteArrayOutputStream class, this class is not thread safe.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The default initial buffer size
    static final int
    The buffer increase size
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new byte array output stream.
    ByteOutputBuffer(int size)
    Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    getByte(int pos)
    Returns the byte buffered at the given position in the buffer.
    void
    Discards all the buffered data, by resetting the counter of written bytes to 0.
    int
    Returns the number of valid bytes in the output buffer (count class variable).
    void
    toByteArray(int off, int len, byte[] outbuf, int outoff)
    Copies the specified part of the stream to the 'outbuf' byte array.
    final void
    write(int b)
    Writes the specified byte to this byte array output stream.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • BUF_INC

      public static final int BUF_INC
      The buffer increase size
      See Also:
    • BUF_DEF_LEN

      public static final int BUF_DEF_LEN
      The default initial buffer size
      See Also:
  • Constructor Details

    • ByteOutputBuffer

      public ByteOutputBuffer()
      Creates a new byte array output stream. The buffer capacity is initially BUF_DEF_LEN bytes, though its size increases if necessary.
    • ByteOutputBuffer

      public ByteOutputBuffer(int size)
      Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
      Parameters:
      size - the initial size.
  • Method Details

    • write

      public final void write(int b)
      Writes the specified byte to this byte array output stream. The functionality provided by this implementation is the same as for the one in the superclass, however this method is not synchronized and therefore not safe thread, but faster.
      Parameters:
      b - The byte to write
    • toByteArray

      public void toByteArray(int off, int len, byte[] outbuf, int outoff)
      Copies the specified part of the stream to the 'outbuf' byte array.
      Parameters:
      off - The index of the first element in the stream to copy.
      len - The number of elements of the array to copy
      outbuf - The destination array
      outoff - The index of the first element in 'outbuf' where to write the data.
    • size

      public int size()
      Returns the number of valid bytes in the output buffer (count class variable).
      Returns:
      The number of bytes written to the buffer
    • reset

      public void reset()
      Discards all the buffered data, by resetting the counter of written bytes to 0.
    • getByte

      public int getByte(int pos)
      Returns the byte buffered at the given position in the buffer. The position in the buffer is the index of the 'write()' method call after the last call to 'reset()'.
      Parameters:
      pos - The position of the byte to return
      Returns:
      The value (betweeb 0-255) of the byte at position 'pos'.