Class FastByteArrayOutputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable

    public final class FastByteArrayOutputStream
    extends java.io.ByteArrayOutputStream
    An unsynchronized ByteArrayOutputStream implementation. This version also has a constructor that lets you create a stream with initial content.
    Version:
    $Id: FastByteArrayOutputStream.java#2 $
    • Field Summary

      • Fields inherited from class java.io.ByteArrayOutputStream

        buf, count
    • Constructor Summary

      Constructors 
      Constructor Description
      FastByteArrayOutputStream​(byte[] pBuffer)
      Creates a ByteArrayOutputStream with the given initial content.
      FastByteArrayOutputStream​(int pSize)
      Creates a ByteArrayOutputStream with the given initial buffer size.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.io.ByteArrayInputStream createInputStream()
      Creates a ByteArrayInputStream that reads directly from this FastByteArrayOutputStream's byte buffer.
      private void growIfNeeded​(int pNewCount)  
      byte[] toByteArray()  
      void write​(byte[] pBytes, int pOffset, int pLength)  
      void write​(int pByte)  
      void writeTo​(java.io.OutputStream pOut)  
      • Methods inherited from class java.io.ByteArrayOutputStream

        close, reset, size, toString, toString, toString, toString, writeBytes
      • Methods inherited from class java.io.OutputStream

        flush, nullOutputStream, write
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • FastByteArrayOutputStream

        public FastByteArrayOutputStream​(int pSize)
        Creates a ByteArrayOutputStream with the given initial buffer size.
        Parameters:
        pSize - initial buffer size
      • FastByteArrayOutputStream

        public FastByteArrayOutputStream​(byte[] pBuffer)
        Creates a ByteArrayOutputStream with the given initial content.

        Note that the buffer is not cloned, for maximum performance.

        Parameters:
        pBuffer - initial buffer
    • Method Detail

      • write

        public void write​(byte[] pBytes,
                          int pOffset,
                          int pLength)
        Overrides:
        write in class java.io.ByteArrayOutputStream
      • write

        public void write​(int pByte)
        Overrides:
        write in class java.io.ByteArrayOutputStream
      • growIfNeeded

        private void growIfNeeded​(int pNewCount)
      • writeTo

        public void writeTo​(java.io.OutputStream pOut)
                     throws java.io.IOException
        Overrides:
        writeTo in class java.io.ByteArrayOutputStream
        Throws:
        java.io.IOException
      • toByteArray

        public byte[] toByteArray()
        Overrides:
        toByteArray in class java.io.ByteArrayOutputStream
      • createInputStream

        public java.io.ByteArrayInputStream createInputStream()
        Creates a ByteArrayInputStream that reads directly from this FastByteArrayOutputStream's byte buffer. The buffer is not cloned, for maximum performance.

        Note that care needs to be taken to avoid writes to this output stream after the input stream is created. Failing to do so, may result in unpredictable behaviour.

        Returns:
        a new ByteArrayInputStream, reading from this stream's buffer.