Class FastByteArrayOutputStream

java.lang.Object
java.io.OutputStream
com.cedarsoftware.util.FastByteArrayOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class FastByteArrayOutputStream extends OutputStream
Faster version of ByteArrayOutputStream that does not have synchronized methods and also provides direct access to its internal buffer so that it does not need to be duplicated when read.
Author:
John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected byte[]
     
    protected int
     
    protected int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct a new FastByteArrayOutputStream with a logical size of 0, but an initial capacity of 1K (1024 bytes).
    Construct a new FastByteArrayOutputStream with the passed in capacity, and a default delta (1024).
    FastByteArrayOutputStream(int capacity, int delta)
    Construct a new FastByteArrayOutputStream with a logical size of 0, but an initial capacity of 'capacity'.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Reset the stream so it can be used again.
    byte[]
     
    int
    The logical size of the byte[] this stream represents, not its physical size, which could be larger.
     
    void
    write(byte[] bytes, int offset, int len)
    Writes len bytes from the specified byte array starting at offset off to this stream.
    void
    write(int b)
    Writes the specified byte to this byte array output stream.
    void
    writeTo(byte[] dest)
    Copy the internal byte[] to the passed in byte[].
    void
    Convenience method to copy the contained byte[] to the passed in OutputStream.

    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, wait, wait, wait
  • Field Details

    • buffer

      protected byte[] buffer
    • size

      protected int size
    • delta

      protected int delta
  • Constructor Details

    • FastByteArrayOutputStream

      public FastByteArrayOutputStream()
      Construct a new FastByteArrayOutputStream with a logical size of 0, but an initial capacity of 1K (1024 bytes). The delta increment is x2.
    • FastByteArrayOutputStream

      public FastByteArrayOutputStream(int capacity)
      Construct a new FastByteArrayOutputStream with the passed in capacity, and a default delta (1024). The delta increment is x2.
      Parameters:
      capacity - int size of internal buffer
    • FastByteArrayOutputStream

      public FastByteArrayOutputStream(int capacity, int delta)
      Construct a new FastByteArrayOutputStream with a logical size of 0, but an initial capacity of 'capacity'.
      Parameters:
      capacity - int capacity (internal buffer size), must be > 0
      delta - int delta, size to increase the internal buffer by when limit reached. If the value is negative, then the internal buffer is doubled in size when additional capacity is needed.
  • Method Details

    • getBuffer

      public byte[] getBuffer()
      Returns:
      byte[], the internal byte buffer. Remember, the length of this array is likely larger than 'size' (whats been written to it). Therefore, use this byte[] along with 0 to size() to fetch the contents of this buffer without creating a new byte[].
    • 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.
    • write

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

      public void writeTo(OutputStream out) throws IOException
      Convenience method to copy the contained byte[] to the passed in OutputStream. You could also code out.write(fastBa.getBuffer(), 0, fastBa.size())
      Parameters:
      out - OutputStream target
      Throws:
      IOException - if one occurs
    • writeTo

      public void writeTo(byte[] dest)
      Copy the internal byte[] to the passed in byte[]. No new space is allocated.
      Parameters:
      dest - byte[] target
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Returns:
      String (UTF-8) from the byte[] in this object.
    • clear

      public void clear()
      Reset the stream so it can be used again. The size() will be 0, but the internal storage is still allocated.
    • size

      public int size()
      The logical size of the byte[] this stream represents, not its physical size, which could be larger.
      Returns:
      int the number of bytes written to this stream