Class ByteData.Writer

java.lang.Object
java.io.OutputStream
io.permazen.util.ByteData.Writer
All Implemented Interfaces:
Closeable, Flushable, AutoCloseable
Enclosing class:
ByteData

public static final class ByteData.Writer extends OutputStream
Gathers data from which to build a ByteData instance.

If any write operation would cause the total size to exceed Integer.MAX_VALUE, then an IndexOutOfBoundsException is thrown.

Instances are thread safe.

  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Close this instance.
    void
    Flush this instance.
    void
    makeRoom(int len)
    Expand the internal buffer as needed to ensure there is room to add the specified number of additional bytes.
    void
    Reset this instance so that it contains zero bytes.
    int
    Get the number of bytes written to this instance so far.
    Return a ByteData containing the data written to this instance so far.
    void
    truncate(int size)
    Truncate this instance to discard all but the first size bytes previously written.
    void
    write(byte[] data)
     
    void
    write(byte[] data, int off, int len)
     
    void
    write(int b)
     
    void
    Write the given byte data to this instance.

    Methods inherited from class java.io.OutputStream

    nullOutputStream

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • write

      public void write(int b)
      Specified by:
      write in class OutputStream
    • write

      public void write(byte[] data)
      Overrides:
      write in class OutputStream
    • write

      public void write(byte[] data, int off, int len)
      Overrides:
      write in class OutputStream
    • flush

      public void flush()
      Flush this instance.

      The implementation in ByteData.Writer does nothing.

      Specified by:
      flush in interface Flushable
      Overrides:
      flush in class OutputStream
    • close

      public void close()
      Close this instance.

      The implementation in ByteData.Writer does nothing.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class OutputStream
    • write

      public void write(ByteData data)
      Write the given byte data to this instance.
      Parameters:
      data - data to write
      Throws:
      IllegalArgumentException - if data is null
    • toByteData

      public ByteData toByteData()
      Return a ByteData containing the data written to this instance so far.
      Returns:
      the data written to this writer
    • size

      public int size()
      Get the number of bytes written to this instance so far.
      Returns:
      size of this instance
    • reset

      public void reset()
      Reset this instance so that it contains zero bytes.

      Equivalent to: truncate(0).

    • truncate

      public void truncate(int size)
      Truncate this instance to discard all but the first size bytes previously written.
      Parameters:
      size - new truncated size
      Throws:
      IndexOutOfBoundsException - if size is negative or greater than this instance's current size
    • makeRoom

      public void makeRoom(int len)
      Expand the internal buffer as needed to ensure there is room to add the specified number of additional bytes.
      Parameters:
      len - number of additional bytes to make room for
      Throws:
      IndexOutOfBoundsException - if len is negative or would cause the buffer to exceed Integer.MAX_VALUE bytes