Package net.jini.io

Class OptimizedByteArrayOutputStream

java.lang.Object
java.io.OutputStream
net.jini.io.OptimizedByteArrayOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class OptimizedByteArrayOutputStream extends OutputStream
This class is an optimized porting of java.io.ByteArrayOutputStream: a) All methods are not synchronized. b) Most safety checks have been removed. c) ensureCapacity method have been added, for low level optimizations. Naturally, this class and all of its methods are not thread safe.
Since:
6.6
Author:
niv
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected byte[]
    The buffer where data is stored.
    protected int
    The number of valid bytes in the buffer.
  • Constructor Summary

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

    Modifier and Type
    Method
    Description
    final void
    Copy internal buffer
    boolean
    ensureCapacity(int delta)
     
    byte[]
    Gets internal buffers
    int
    The current buffer capacity.
    void
    Resets the count field of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded.
    void
    setBuffer(byte[] buf)
    Set stream buffer, and reset the counter.
    void
    setBuffer(byte[] buffer, int count)
    Set stream buffer and set the counter.
    void
    setSize(int size)
    Set the buffer size.
    int
    Returns the current size of the buffer.
    byte[]
    Creates a newly allocated byte array.
    void
    write(byte[] b, int off, int len)
    Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
    void
    write(int b)
    Writes the specified byte to this byte array output stream.
    void
    writeByte(byte b)
     
    void
    Writes the complete contents of this byte array output stream to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count).

    Methods inherited from class java.io.OutputStream

    close, flush, nullOutputStream, write

    Methods inherited from class java.lang.Object

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

    • _buffer

      protected byte[] _buffer
      The buffer where data is stored.
    • _count

      protected int _count
      The number of valid bytes in the buffer.
  • Constructor Details

    • OptimizedByteArrayOutputStream

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

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

    • copyToBuffer

      public final void copyToBuffer(ByteBuffer buffer)
      Copy internal buffer
    • setBuffer

      public void setBuffer(byte[] buf)
      Set stream buffer, and reset the counter.
      Parameters:
      buf - new buffer
    • setBuffer

      public void setBuffer(byte[] buffer, int count)
      Set stream buffer and set the counter.
      Parameters:
      count - amount of valid bytes
      buffer - stream buffer
    • getBuffer

      public byte[] getBuffer()
      Gets internal buffers
      Returns:
      internal buffer
    • setSize

      public void setSize(int size)
      Set the buffer size.
      Parameters:
      size - amount of valid bytes
    • size

      public int size()
      Returns the current size of the buffer.
      Returns:
      the value of the count field, which is the number of valid bytes in this output stream.
      See Also:
    • getCapacity

      public int getCapacity()
      The current buffer capacity.
      Returns:
      buffer current capacity
    • write

      public void write(int b)
      Writes the specified byte to this byte array output stream.
      Specified by:
      write in class OutputStream
      Parameters:
      b - the byte to be written.
    • writeByte

      public void writeByte(byte b)
    • write

      public void write(byte[] b, int off, int len)
      Writes len bytes from the specified byte array starting at offset off to this byte array output stream.
      Overrides:
      write in class OutputStream
      Parameters:
      b - the data.
      off - the start offset in the data.
      len - the number of bytes to write.
    • ensureCapacity

      public boolean ensureCapacity(int delta)
    • writeTo

      public void writeTo(OutputStream out) throws IOException
      Writes the complete contents of this byte array output stream to the specified output stream argument, as if by calling the output stream's write method using out.write(buf, 0, count).
      Parameters:
      out - the output stream to which to write the data.
      Throws:
      IOException - if an I/O error occurs.
    • reset

      public void reset()
      Resets the count field of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded. The output stream can be used again, reusing the already allocated buffer space.
      See Also:
    • toByteArray

      public byte[] toByteArray()
      Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it.
      Returns:
      the current contents of this output stream, as a byte array.
      See Also: