Class FastByteArrayOutputStream

java.lang.Object
java.io.OutputStream
java.io.ByteArrayOutputStream
VASSAL.tools.io.FastByteArrayOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class FastByteArrayOutputStream extends ByteArrayOutputStream
A ByteArrayOutputStream which does not retain its byte buffer, and can produce an InputStream sharing the same byte buffer. The advantage of this is that the byte buffer can be shared between the output and input, and provides better performance when the stream will be used only a single time and the buffer is full when toByteArray() is called.
Since:
3.1.0
Author:
Joel Uckelman
  • Constructor Details

    • FastByteArrayOutputStream

      public FastByteArrayOutputStream()
      Creates a new byte array output stream. The buffer capacity is initially 32 bytes.
    • FastByteArrayOutputStream

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

    • toByteArray

      public byte[] toByteArray()
      Creates a newly allocated byte array. Its size is the current size of this output stream. If the buffer is full, then the array returned is the buffer itself, not a copy, and a new empty buffer is created. Otherwise, a copy of valid portion of the buffer is returned.
      Overrides:
      toByteArray in class ByteArrayOutputStream
      Returns:
      the current contents of this output stream as a byte array
    • toInputStream

      public InputStream toInputStream()
      Creates an InputStream which reads the bytes stored by this FastByteArrayOutputStream.
      Returns:
      the InputStream for this buffer