Class ByteWriter

java.lang.Object
io.permazen.util.ByteWriter

public class ByteWriter extends Object
Writes bytes to a buffer.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor.
    ByteWriter(int capacity)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    Return a view of this instance as an OutputStream.
    byte[]
    Retrieve all of the bytes that have been written to this instance.
    byte[]
    getBytes(int off)
    Retrieve the bytes that have been written to this instance, starting at the given offset.
    byte[]
    getBytes(int off, int len)
    Retrieve a sub-range of the bytes that have been written to this instance.
    int
    Get the current buffer length.
    int
    Mark current position.
    void
    reset(int mark)
    Reset write position to a previously marked position.
    void
    write(byte[] data)
    Write an array of bytes to this instance.
    void
    write(byte[] data, int off, int len)
    Write a sub-range from an array of bytes.
    void
    write(ByteReader reader)
    Read all remaining content from the given ByteReader and write it to this instance.
    void
    writeByte(int value)
    Write a single byte to this instance.

    Methods inherited from class java.lang.Object

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

    • ByteWriter

      public ByteWriter()
      Default constructor.
    • ByteWriter

      public ByteWriter(int capacity)
      Constructor.
      Parameters:
      capacity - initial capacity of buffer
  • Method Details

    • getBytes

      public byte[] getBytes()
      Retrieve all of the bytes that have been written to this instance.
      Returns:
      byte content written so far (not necessarily a copy; caller must not modify)
    • getBytes

      public byte[] getBytes(int off)
      Retrieve the bytes that have been written to this instance, starting at the given offset.
      Parameters:
      off - offset into written bytes
      Returns:
      byte content written so far starting at off (not necessarily a copy; caller must not modify)
      Throws:
      IndexOutOfBoundsException - if off or len is out of bounds
    • getBytes

      public byte[] getBytes(int off, int len)
      Retrieve a sub-range of the bytes that have been written to this instance.
      Parameters:
      off - offset into written bytes
      len - desired length
      Returns:
      len bytes written so far starting from off (not necessarily a copy; caller must not modify)
      Throws:
      IndexOutOfBoundsException - if off or len is out of bounds
    • writeByte

      public void writeByte(int value)
      Write a single byte to this instance.
      Parameters:
      value - byte to write; all but the lower 8 bits are ignored
    • write

      public void write(ByteReader reader)
      Read all remaining content from the given ByteReader and write it to this instance.
      Parameters:
      reader - source for bytes to write
    • write

      public void write(byte[] data)
      Write an array of bytes to this instance.
      Parameters:
      data - bytes to write
    • write

      public void write(byte[] data, int off, int len)
      Write a sub-range from an array of bytes.
      Parameters:
      data - bytes to write
      off - offset into data
      len - the number of bytes to write
      Throws:
      IndexOutOfBoundsException - if off or len is out of bounds
    • getLength

      public int getLength()
      Get the current buffer length. Returns the same value as #mark.
      Returns:
      number of bytes written so far
    • mark

      public int mark()
      Mark current position. Returns the same value as #getLength.
      Returns:
      number of bytes written so far
    • reset

      public void reset(int mark)
      Reset write position to a previously marked position.
      Parameters:
      mark - value previously returned by mark()
      Throws:
      IndexOutOfBoundsException - if mark is out of bounds
    • asOutputStream

      public OutputStream asOutputStream()
      Return a view of this instance as an OutputStream.
      Returns:
      streaming view of this instance