Class ByteArrayOutputStream

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

    public class ByteArrayOutputStream
    extends java.io.OutputStream
    This class implements an output stream in which the data is written into a byte array. The buffer automatically grows as data is written to it.

    The data can be retrieved using toByteArray() and toString().

    Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.

    This is an alternative implementation of the ByteArrayOutputStream class. The original implementation only allocates 32 bytes at the beginning. As this class is designed for heavy duty it starts at 1024 bytes. In contrast to the original it doesn't reallocate the whole memory block but allocates additional buffers. This way no buffers need to be garbage collected and the contents don't have to be copied to the new buffer. This class is designed to behave exactly like the original. The only exception is the deprecated toString(int) method that has been ignored.

    • Constructor Summary

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

      All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void close()
      Closing a ByteArrayOutputStream has no effect.
      void reset()  
      int size()
      Return the current size of the byte array.
      static java.io.InputStream toBufferedInputStream​(java.io.InputStream input)
      Fetches entire contents of an InputStream and represent same data as result InputStream.
      static java.io.InputStream toBufferedInputStream​(java.io.InputStream input, int size)
      Fetches entire contents of an InputStream and represent same data as result InputStream.
      byte[] toByteArray()
      Gets the curent contents of this byte stream as a byte array.
      java.io.InputStream toInputStream()
      Gets the current contents of this byte stream as a Input Stream.
      java.lang.String toString()
      Deprecated.
      2.5 use toString(String) instead
      java.lang.String toString​(java.lang.String enc)
      Gets the curent contents of this byte stream as a string using the specified encoding.
      java.lang.String toString​(java.nio.charset.Charset charset)
      Gets the curent contents of this byte stream as a string using the specified encoding.
      void write​(byte[] b, int off, int len)
      Write the bytes to byte array.
      void write​(int b)
      Write a byte to byte array.
      int write​(java.io.InputStream in)
      Writes the entire contents of the specified input stream to this byte stream.
      void writeTo​(java.io.OutputStream out)
      Writes the entire contents of this byte stream to the specified output stream.
      • 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

      • ByteArrayOutputStream

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

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

      • write

        public void write​(byte[] b,
                          int off,
                          int len)
        Write the bytes to byte array.
        Overrides:
        write in class java.io.OutputStream
        Parameters:
        b - the bytes to write
        off - The start offset
        len - The number of bytes to write
      • write

        public void write​(int b)
        Write a byte to byte array.
        Specified by:
        write in class java.io.OutputStream
        Parameters:
        b - the byte to write
      • write

        public int write​(java.io.InputStream in)
                  throws java.io.IOException
        Writes the entire contents of the specified input stream to this byte stream. Bytes from the input stream are read directly into the internal buffers of this streams.
        Parameters:
        in - the input stream to read from
        Returns:
        total number of bytes read from the input stream (and written to this stream)
        Throws:
        java.io.IOException - if an I/O error occurs while reading the input stream
        Since:
        1.4
      • size

        public int size()
        Return the current size of the byte array.
        Returns:
        the current size of the byte array
      • close

        public void close()
                   throws java.io.IOException
        Closing a ByteArrayOutputStream has no effect. The methods in this class can be called after the stream has been closed without generating an IOException.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.OutputStream
        Throws:
        java.io.IOException - never (this method should not declare this exception but it has to now due to backwards compatibility)
      • reset

        public void reset()
        See Also:
        ByteArrayOutputStream.reset()
      • writeTo

        public void writeTo​(java.io.OutputStream out)
                     throws java.io.IOException
        Writes the entire contents of this byte stream to the specified output stream.
        Parameters:
        out - the output stream to write to
        Throws:
        java.io.IOException - if an I/O error occurs, such as if the stream is closed
        See Also:
        ByteArrayOutputStream.writeTo(java.io.OutputStream)
      • toBufferedInputStream

        public static java.io.InputStream toBufferedInputStream​(java.io.InputStream input)
                                                         throws java.io.IOException
        Fetches entire contents of an InputStream and represent same data as result InputStream.

        This method is useful where,

        • Source InputStream is slow.
        • It has network resources associated, so we cannot keep it open for long time.
        • It has network timeout associated.
        It can be used in favor of toByteArray(), since it avoids unnecessary allocation and copy of byte[].
        This method buffers the input internally, so there is no need to use a BufferedInputStream.
        Parameters:
        input - Stream to be fully buffered.
        Returns:
        A fully buffered stream.
        Throws:
        java.io.IOException - if an I/O error occurs
        Since:
        2.0
      • toBufferedInputStream

        public static java.io.InputStream toBufferedInputStream​(java.io.InputStream input,
                                                                int size)
                                                         throws java.io.IOException
        Fetches entire contents of an InputStream and represent same data as result InputStream.

        This method is useful where,

        • Source InputStream is slow.
        • It has network resources associated, so we cannot keep it open for long time.
        • It has network timeout associated.
        It can be used in favor of toByteArray(), since it avoids unnecessary allocation and copy of byte[].
        This method buffers the input internally, so there is no need to use a BufferedInputStream.
        Parameters:
        input - Stream to be fully buffered.
        size - the initial buffer size
        Returns:
        A fully buffered stream.
        Throws:
        java.io.IOException - if an I/O error occurs
        Since:
        2.5
      • toInputStream

        public java.io.InputStream toInputStream()
        Gets the current contents of this byte stream as a Input Stream. The returned stream is backed by buffers of this stream, avoiding memory allocation and copy, thus saving space and time.
        Returns:
        the current contents of this output stream.
        Since:
        2.5
        See Also:
        ByteArrayOutputStream.toByteArray(), reset()
      • toByteArray

        public byte[] toByteArray()
        Gets the curent contents of this byte stream as a byte array. The result is independent of this stream.
        Returns:
        the current contents of this output stream, as a byte array
        See Also:
        ByteArrayOutputStream.toByteArray()
      • toString

        @Deprecated
        public java.lang.String toString()
        Deprecated.
        2.5 use toString(String) instead
        Gets the curent contents of this byte stream as a string using the platform default charset.
        Overrides:
        toString in class java.lang.Object
        Returns:
        the contents of the byte array as a String
        See Also:
        ByteArrayOutputStream.toString()
      • toString

        public java.lang.String toString​(java.lang.String enc)
                                  throws java.io.UnsupportedEncodingException
        Gets the curent contents of this byte stream as a string using the specified encoding.
        Parameters:
        enc - the name of the character encoding
        Returns:
        the string converted from the byte array
        Throws:
        java.io.UnsupportedEncodingException - if the encoding is not supported
        See Also:
        ByteArrayOutputStream.toString(String)
      • toString

        public java.lang.String toString​(java.nio.charset.Charset charset)
        Gets the curent contents of this byte stream as a string using the specified encoding.
        Parameters:
        charset - the character encoding
        Returns:
        the string converted from the byte array
        Since:
        2.5
        See Also:
        ByteArrayOutputStream.toString(String)