Class PooledBufferedOutputStream

  • All Implemented Interfaces:
    Closeable, Flushable, AutoCloseable

    public class PooledBufferedOutputStream
    extends OutputStream
    See if we can't manage buffers better than the standard java classes - at least for the specific use we have. We are generating a lot of output and causing a lot of expansion and copying.

    This may result in a lot of JVM churn. See if a pool of buffers can improve matters.

    Author:
    Mike Douglass
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int count
      The number of valid bytes in the virtual buffer.
    • Field Detail

      • count

        protected int count
        The number of valid bytes in the virtual buffer.
    • Constructor Detail

      • PooledBufferedOutputStream

        public PooledBufferedOutputStream()
        Creates a new pooled buffered output stream.
    • Method Detail

      • 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.
      • 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.
      • getInputStream

        public InputStream getInputStream()
                                   throws IOException
        Get an InputStream for the bytes in the buffer. No guarantees if writes take place after this is called.
        Returns:
        InputStream
        Throws:
        IOException
      • 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:
        ByteArrayOutputStream.size()
      • 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:
        ByteArrayOutputStream.count
      • toString

        public String toString()
        Converts the buffer's contents into a string decoding bytes using the platform's default character set. The length of the new String is a function of the character set, and hence may not be equal to the size of the buffer.

        This method always replaces malformed-input and unmappable-character sequences with the default replacement string for the platform's default character set. The CharsetDecoder class should be used when more control over the decoding process is required.

        Overrides:
        toString in class Object
        Returns:
        String decoded from the buffer's contents.
        Since:
        JDK1.1
      • toString

        public String toString​(String charsetName)
                        throws UnsupportedEncodingException
        Converts the buffer's contents into a string by decoding the bytes using the specified charsetName. The length of the new String is a function of the charset, and hence may not be equal to the length of the byte array.

        This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement string. The CharsetDecoder class should be used when more control over the decoding process is required.

        Parameters:
        charsetName - the name of a supported charset
        Returns:
        String decoded from the buffer's contents.
        Throws:
        UnsupportedEncodingException - If the named charset is not supported
        Since:
        JDK1.1
      • close

        public void close()
                   throws IOException
        Closing can have no effect as we need to access the buffers afterwards. The methods in this class can be called after the stream has been closed without generating an IOException.

        The release method MUST be called for this class to release the buffers.

        Specified by:
        close in interface AutoCloseable
        Specified by:
        close in interface Closeable
        Overrides:
        close in class OutputStream
        Throws:
        IOException
      • release

        public void release()
                     throws IOException
        This really release buffers back to the pool. MUST be called to gain the benefit of pooling.
        Throws:
        IOException