Class UByteArrayOutputStream

java.lang.Object
java.io.OutputStream
cl.netswitch.lib.util.UByteArrayOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public final class UByteArrayOutputStream extends OutputStream
Unsynchronized OutputStream in which data is written to a byte array. The buffer automatically grows as data is written to it. The data can be retrieved using toByteArray() and toString().

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

  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new UByteArrayOutputStream.
    Creates a new UByteArrayOutputStream, with a buffer capacity of the specified size, in bytes.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closing a UByteArrayOutputStream has no effect.
    byte[]
    Returns the backing byte array of this UByteArrayOutputStream.
    void
    Resets the count field of this UByteArrayOutputStream to zero, so that all currently accumulated output in the output stream is discarded.
    int
    Returns the current size of the buffer of this UByteArrayOutputStream.
    byte[]
    Creates a newly allocated byte array.
    Converts the buffer's contents into a string decoding bytes using the platform's default character set.
    toString(Charset charset)
    Converts the buffer's contents into a string by decoding the bytes using the specified charset.
    void
    write(byte[] bytes, int offset, int length)
    Writes length bytes from the specified byte array starting at offset offset to this UByteArrayOutputStream.
    void
    write(int value)
    Writes the specified byte to this UByteArrayOutputStream.
    void
    writeBytes(byte[] bytes)
    Writes the complete contents of the specified byte array to this UByteArrayOutputStream.
    void
    Writes the complete contents of this UByteArrayOutputStream to the specified output stream argument, as if by calling the output stream's write method using output.write(buffer, 0, count).

    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 Details

    • UByteArrayOutputStream

      public UByteArrayOutputStream()
      Creates a new UByteArrayOutputStream. The buffer capacity is initially 32 bytes, though its size increases if necessary.
    • UByteArrayOutputStream

      public UByteArrayOutputStream(int size)
      Creates a new UByteArrayOutputStream, with a buffer capacity of the specified size, in bytes.
      Parameters:
      size - the initial size.
      Throws:
      IllegalArgumentException - if size is negative.
  • Method Details

    • write

      public void write(int value)
      Writes the specified byte to this UByteArrayOutputStream.
      Specified by:
      write in class OutputStream
      Parameters:
      value - the byte to be written.
    • write

      public void write(byte[] bytes, int offset, int length)
      Writes length bytes from the specified byte array starting at offset offset to this UByteArrayOutputStream.
      Overrides:
      write in class OutputStream
      Parameters:
      bytes - the data.
      offset - the start offset in the data.
      length - the number of bytes to write.
      Throws:
      NullPointerException - if bytes is null.
      IndexOutOfBoundsException - if offset is negative, length is negative, or length is greater than bytes.length - offset.
    • 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.
    • close

      public void close() throws IOException
      Closing a UByteArrayOutputStream 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 AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
      Throws:
      IOException
    • reset

      public void reset()
      Resets the count field of this UByteArrayOutputStream to zero, so that all currently accumulated output in the output stream is discarded. The output stream can be used again, reusing the already allocated buffer space.
    • size

      public int size()
      Returns the current size of the buffer of this UByteArrayOutputStream.
      Returns:
      the value of the count field, which is the number of valid bytes in this output stream.
    • getByteArray

      public byte[] getByteArray()
      Returns the backing byte array of this UByteArrayOutputStream.
      Returns:
      the backing byte array of this UByteArrayOutputStream.
    • 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.
    • writeBytes

      public void writeBytes(byte[] bytes)
      Writes the complete contents of the specified byte array to this UByteArrayOutputStream.
      Parameters:
      bytes - the data.
      Throws:
      NullPointerException - if bytes is null.
    • writeTo

      public void writeTo(OutputStream output) throws IOException
      Writes the complete contents of this UByteArrayOutputStream to the specified output stream argument, as if by calling the output stream's write method using output.write(buffer, 0, count).
      Parameters:
      output - the output stream to which to write the data.
      Throws:
      NullPointerException - if output is null.
      IOException - if an I/O error occurs.
    • toString

      public String toString(Charset charset)
      Converts the buffer's contents into a string by decoding the bytes using the specified charset. 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 the charset's default replacement string. The CharsetDecoder class should be used when more control over the decoding process is required.

      Parameters:
      charset - the charset to be used to decode the bytes
      Returns:
      String decoded from the buffer's contents.