Class NonBlockingByteArrayOutputStream

java.lang.Object
java.io.OutputStream
com.helger.base.io.nonblocking.NonBlockingByteArrayOutputStream
All Implemented Interfaces:
IHasSize, IWriteToStream, Closeable, Flushable, AutoCloseable

public class NonBlockingByteArrayOutputStream extends OutputStream implements IHasSize, IWriteToStream
A non-synchronized copy of the class ByteArrayOutputStream.
Author:
Philip Helger
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected byte[]
    The buffer where data is stored.
    protected int
    The number of valid bytes in the buffer.
  • Constructor Summary

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

    Modifier and Type
    Method
    Description
    void
    Closing a ByteArrayOutputStream has no effect.
    byte @NonNull []
     
    Create a new InputStream from the contained byte array WITHOUT COPYING it.
    @NonNull String
    getAsString(int nOfs, int nLength, @NonNull Charset aCharset)
    Converts the buffer's contents into a string by decoding the bytes using the specified charsetName.
    @NonNull String
    getAsString(int nLength, @NonNull Charset aCharset)
    Converts the buffer's contents into a string by decoding the bytes using the specified charsetName.
    @NonNull String
    getAsString(@NonNull Charset aCharset)
    Converts the buffer's contents into a string by decoding the bytes using the specified charsetName.
    byte @NonNull []
     
    int
     
    byte
    getByteAt(int nIndex)
    Get the byte at the specified index
    boolean
    boolean
    void
    readFrom(@NonNull InputStream aIS)
    Reads the given InputStream completely into the buffer.
    void
    Resets the count field of this byte array output stream to zero, so that all currently accumulated output in the output stream is discarded.
    int
    Returns the current size of the buffer.
    boolean
    startsWith(byte @NonNull [] aBytes)
    Check if the buffer starts with the specified byte array.
    boolean
    startsWith(byte @NonNull [] aBytes, int nOfs, int nLen)
    Check if the buffer starts with the specified byte array.
    byte @NonNull []
    Creates a newly allocated byte array.
     
    void
    write(byte @NonNull [] aBuf)
     
    void
    write(byte @NonNull [] aBuf, int nOfs, int nLen)
    Writes nLen bytes from the specified byte array starting at offset nOfs to this byte array output stream.
    void
    write(int b)
    Writes the specified byte to this byte array output stream.
    void
    writeTo(@NonNull OutputStream aOS)
    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).

    Methods inherited from class java.io.OutputStream

    flush, nullOutputStream

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface com.helger.base.io.iface.IWriteToStream

    writeToAndClose
  • Field Details

    • m_aBuf

      protected byte[] m_aBuf
      The buffer where data is stored.
    • m_nCount

      protected int m_nCount
      The number of valid bytes in the buffer.
  • Constructor Details

    • NonBlockingByteArrayOutputStream

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

      public NonBlockingByteArrayOutputStream(@Nonnegative int nSize)
      Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
      Parameters:
      nSize - the initial size.
      Throws:
      IllegalArgumentException - if size is negative.
  • Method Details

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

      public void write(byte @NonNull [] aBuf)
      Overrides:
      write in class OutputStream
    • write

      public void write(byte @NonNull [] aBuf, int nOfs, int nLen)
      Writes nLen bytes from the specified byte array starting at offset nOfs to this byte array output stream.
      Overrides:
      write in class OutputStream
      Parameters:
      aBuf - the data.
      nOfs - the start offset in the data.
      nLen - the number of bytes to write.
    • writeTo

      public void writeTo(@WillNotClose @NonNull OutputStream aOS) 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). The content of this stream is not altered by calling this method.
      Specified by:
      writeTo in interface IWriteToStream
      Parameters:
      aOS - the output stream to which to write the data. May not be null.
      Throws:
      IOException - if an I/O error occurs.
    • readFrom

      public void readFrom(@WillNotClose @NonNull InputStream aIS) throws IOException
      Reads the given InputStream completely into the buffer.
      Parameters:
      aIS - the InputStream to read from. May not be null. Is not closed internally.
      Throws:
      IOException - If reading fails
    • reset

      public void reset()
      Resets the count field of this byte array output stream 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.
    • toByteArray

      @ReturnsMutableCopy public byte @NonNull [] 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.
      If you are sure, that this OutputStream is not altered anymore, it maybe preferred to use getBufferOrCopy() because it avoids copying the internal buffer if the size matches exactly.
      Returns:
      the current contents of this output stream, as a byte array.
    • getByteAt

      public byte getByteAt(@Nonnegative int nIndex)
      Get the byte at the specified index
      Parameters:
      nIndex - The index to use. Must be ≥ 0 and < count
      Returns:
      The byte at the specified position
    • getBufferSize

      @Nonnegative public int getBufferSize()
      Returns:
      The number of pre-allocated bytes. Always ≥ 0.
    • size

      @Nonnegative public int size()
      Returns the current size of the buffer.
      Specified by:
      size in interface IHasSize
      Returns:
      the value of the count field, which is the number of valid bytes in this output stream.
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface IHasSize
      Returns:
      true if no items are present, false if at least a single item is present.
      See Also:
    • isNotEmpty

      public boolean isNotEmpty()
      Specified by:
      isNotEmpty in interface IHasSize
      Returns:
      true if at least one item is present, false if no item is present.
      See Also:
    • startsWith

      public boolean startsWith(byte @NonNull [] aBytes)
      Check if the buffer starts with the specified byte array.
      Parameters:
      aBytes - The bytes to check against. May not be null.
      Returns:
      true if the buffer starts with the specified bytes.
    • startsWith

      public boolean startsWith(byte @NonNull [] aBytes, @Nonnegative int nOfs, @Nonnegative int nLen)
      Check if the buffer starts with the specified byte array.
      Parameters:
      aBytes - The bytes to check against. May not be null.
      nOfs - Offset into the byte array. Must be ≥ 0.
      nLen - Length of bytes to check. Must be ≥ 0.
      Returns:
      true if the buffer starts with the specified bytes.
    • getAsString

      public @NonNull String getAsString(@NonNull Charset aCharset)
      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:
      aCharset - the charset to be used. May not be null.
      Returns:
      String decoded from the buffer's contents.
    • getAsString

      public @NonNull String getAsString(@Nonnegative int nLength, @NonNull Charset aCharset)
      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:
      nLength - The number of bytes to be converted to a String. Must be ≥ 0.
      aCharset - the charset to be used. May not be null.
      Returns:
      String decoded from the buffer's contents.
    • getAsString

      public @NonNull String getAsString(@Nonnegative int nOfs, @Nonnegative int nLength, @NonNull Charset aCharset)
      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:
      nOfs - The start index to use
      nLength - The number of bytes to be converted to a String. Must be ≥ 0.
      aCharset - the charset to be used. May not be null.
      Returns:
      String decoded from the buffer's contents.
    • directGetBuffer

      @ReturnsMutableObject public byte @NonNull [] directGetBuffer()
      Returns:
      The internally used byte buffer. Never null. Handle with care!
    • getBufferOrCopy

      public byte @NonNull [] getBufferOrCopy()
      Returns:
      The direct buffer, if the array size and the count are identical, a copy otherwise.
      Since:
      9.1.3
      See Also:
    • close

      public void close()
      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 operation does nothing on this class.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
    • getAsInputStream

      public @NonNull NonBlockingByteArrayInputStream getAsInputStream()
      Create a new InputStream from the contained byte array WITHOUT COPYING it. So please be careful as this method is not thread-safe and any modifications done later on this object are NOT reflected in the InputStream!
      This is a shortcut for new NonBlockingByteArrayInputStream (directGetBuffer (), 0, getSize ())
      Returns:
      A new NonBlockingByteArrayInputStream.
      Since:
      9.0.0
    • toString

      public String toString()
      Overrides:
      toString in class Object