java.lang.Object
i2p.susi.util.MemoryBuffer
All Implemented Interfaces:
Buffer

public class MemoryBufferextends Object implements Buffer
Buffer implementation backed by a byte array. Suitable for small amounts of data only.
  • Field Details

    • _baos

      private ByteArrayOutputStream _baos
      Buffer collecting the bytes written.
    • content

      private byte[] content
      The bytes read back, filled in when the buffer closes.
    • _size

      private final int _size
      Initial buffer size, in bytes.
  • Constructor Details

    • MemoryBuffer

      public MemoryBuffer()
      Create a MemoryBuffer with default initial capacity.
    • MemoryBuffer

      public MemoryBuffer(int size)
      Create a MemoryBuffer with specified initial capacity.
      Parameters:
      size - the initial capacity in bytes
  • Method Details

    • getInputStream

      public InputStream getInputStream() throws IOException
      Get an InputStream for the buffered data.
      Specified by:
      getInputStream in interface Buffer
      Returns:
      a stream over the buffered bytes, positioned at the start
      Throws:
      IOException - if no data has been written
    • getOutputStream

      public OutputStream getOutputStream()
      Get an OutputStream for writing data to this buffer.
      Specified by:
      getOutputStream in interface Buffer
      Returns:
      new or existing ByteArrayOutputStream
    • readComplete

      public void readComplete(boolean success)
      Description copied from interface: Buffer
      Top-level reader MUST call this to close the input stream.
      Specified by:
      readComplete in interface Buffer
      Parameters:
      success - if false, the read is considered failed
    • writeComplete

      public void writeComplete(boolean success)
      Finalize the write. If successful, the data is stored. If not, the data is discarded.
      Specified by:
      writeComplete in interface Buffer
      Parameters:
      success - if true, stores the data; if false, discards it
    • getLength

      public int getLength()
      Get the current size of data in this buffer.
      Specified by:
      getLength in interface Buffer
      Returns:
      the size in bytes
    • getOffset

      public int getOffset()
      Current write position in the array.
      Specified by:
      getOffset in interface Buffer
      Returns:
      0 always
    • getContent

      public byte[] getContent()
      Get the buffered content after writeComplete(true).
      Returns:
      content bytes if writeComplete(true) was called, otherwise null
    • toString

      public String toString()
      Overrides:
      toString in class Object