Class DirectByteArrayOutputStream

java.lang.Object
java.io.OutputStream
java.io.ByteArrayOutputStream
com.pnfsoftware.jeb.util.io.DirectByteArrayOutputStream
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable

public class DirectByteArrayOutputStream extends ByteArrayOutputStream
Extend ByteArrayOutputStream to provide direct access to the underlying byte array. In particular, this class is meant to circumvent the memory consumption issues of ByteArrayOutputStream.toByteArray().
  • Constructor Details

    • DirectByteArrayOutputStream

      public DirectByteArrayOutputStream()
  • Method Details

    • getRawBytes

      public byte[] getRawBytes()
      Direct access to the underlying byte array. This method is dangerous, but can be used instead of the expensive ByteArrayOutputStream.toByteArray(). It should be used in combination with ByteArrayOutputStream.size(). A safer, as efficient alternative, is getBytes().
      Returns:
      array with potentially more bytes than what was actually written to the stream (see ByteArrayOutputStream.size())
    • getBytes

      public ByteBuffer getBytes()
      Direct access to the underlying byte array.
      Returns:
      a buffer view of the bytes written to the stream