Class B2JsonBoundedByteArrayOutputStream

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

    public class B2JsonBoundedByteArrayOutputStream
    extends java.io.OutputStream
    A B2Json implementation of OutputStream that - stores stream content in a byte array - expands capacity when needed (typically doubles) - has an upper bound limit on output array size (max capacity) - throws IOException if the max capacity threshold is crossed. The last behavior is different from standard Java ByteArrayOutputStream Implementation where an OutOfMemoryError would be thrown. OutOfMemoryError is typically interpreted as JVM running out of heap space, and JVM could be killed if configured so. This may not be desired for Java applications configured with heap space much larger than 2 GB, and such applications may want to continue to run after catching this threshold-crossing exception. THREAD-SAFE
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int getMaxCapacity()
      returns the max capacity of output array
      int getSize()
      returns the current output array size
      byte[] toByteArray()
      returns a copy of the output byte array
      java.lang.String toString​(java.lang.String charsetName)
      makes a new String by decoding the bytes in output array using the specified charset.
      void write​(byte[] bytes, int offset, int length)
      writes an array of bytes into the output array
      void write​(int i)
      writes one byte into the output array
      • 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, toString, wait, wait, wait
    • Constructor Detail

      • B2JsonBoundedByteArrayOutputStream

        public B2JsonBoundedByteArrayOutputStream​(int maxCapacity)
    • Method Detail

      • write

        public void write​(int i)
                   throws java.io.IOException
        writes one byte into the output array
        Specified by:
        write in class java.io.OutputStream
        Parameters:
        i - one byte
        Throws:
        java.io.IOException - if expanding capacity would cross the maxCapacity threshold
      • write

        public void write​(byte[] bytes,
                          int offset,
                          int length)
                   throws java.io.IOException
        writes an array of bytes into the output array
        Overrides:
        write in class java.io.OutputStream
        Parameters:
        bytes - input array of bytes
        offset - offset for the input array
        length - number of bytes to write
        Throws:
        java.io.IOException - if expanding capacity would cross the maxCapacity threshold
      • toString

        public java.lang.String toString​(java.lang.String charsetName)
                                  throws java.io.UnsupportedEncodingException
        makes a new String by decoding the bytes in output array using the specified charset.
        Parameters:
        charsetName - the charset to be used in decoding
        Returns:
        the string representation of output bytes after decoding
        Throws:
        java.io.UnsupportedEncodingException - if the named charset is not supported
      • toByteArray

        public byte[] toByteArray()
        returns a copy of the output byte array
        Returns:
        a copy of output buffer
      • getMaxCapacity

        public int getMaxCapacity()
        returns the max capacity of output array
        Returns:
        the max capacity
      • getSize

        public int getSize()
        returns the current output array size
        Returns:
        output output array size