Class ByteReader

java.lang.Object
io.permazen.util.ByteReader

public class ByteReader extends Object
Reads bytes from a buffer.
  • Constructor Summary

    Constructors
    Constructor
    Description
    ByteReader(byte[] buf)
    Constructor.
    ByteReader(byte[] buf, int off)
    Constructor.
    ByteReader(byte[] buf, int off, int len)
    Constructor.
    Constructor.
    ByteReader(ByteWriter writer, int mark)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    Return a view of this instance as an InputStream.
    byte[]
    Copy all the of bytes in the buffer.
    byte[]
    getBytes(int off)
    Copy a range of bytes from the given offset to the end of the buffer.
    byte[]
    getBytes(int off, int len)
    Copy a range of bytes from the buffer.
    int
    Get maximum offset into buffer.
    int
    Get current offset into buffer.
    int
    Mark current read position.
    int
    Peek at next byte, if any.
    int
    Read the next byte.
    byte[]
    readBytes(int len)
    Read the specified number of bytes.
    byte[]
    Read all the of remaining bytes and advance the read position to the end.
    int
    Get the number of bytes remaining.
    void
    reset(int mark)
    Reset read position to a previously marked position.
    void
    skip(int num)
    Skip over bytes.
    void
    Unread the previously read byte.
    void
    unread(int len)
    Unread the given number of previously read bytes.

    Methods inherited from class java.lang.Object

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

    • ByteReader

      public ByteReader(byte[] buf)
      Constructor. The provided array is read from directly; no copy is made.
      Parameters:
      buf - array to read from
      Throws:
      NullPointerException - if buf is null
    • ByteReader

      public ByteReader(byte[] buf, int off)
      Constructor. The provided array is read from directly; no copy is made.
      Parameters:
      buf - array to read from
      off - starting offset into buf
      Throws:
      IndexOutOfBoundsException - if off is out of bounds
      NullPointerException - if buf is null
    • ByteReader

      public ByteReader(byte[] buf, int off, int len)
      Constructor. The provided array is read from directly; no copy is made.
      Parameters:
      buf - array to read from
      off - offset into buf
      len - number of bytes to read
      Throws:
      IndexOutOfBoundsException - if off or len are out of bounds
      NullPointerException - if buf is null
    • ByteReader

      public ByteReader(ByteWriter writer)
      Constructor. Takes a snapshot of the given writer's entire content.
      Parameters:
      writer - ByteWriter to read data from
      Throws:
      NullPointerException - if writer is null
    • ByteReader

      public ByteReader(ByteWriter writer, int mark)
      Constructor. Takes a snapshot of the given writer's content starting at the specified position.
      Parameters:
      writer - ByteWriter to read data from
      mark - position previously returned by ByteWriter.mark()
      Throws:
      IndexOutOfBoundsException - if mark is out of bounds
      NullPointerException - if writer is null
  • Method Details

    • peek

      public int peek()
      Peek at next byte, if any.
      Returns:
      next byte (0-255)
      Throws:
      IndexOutOfBoundsException - if there are no more bytes
    • readByte

      public int readByte()
      Read the next byte.
      Returns:
      next byte (0-255)
      Throws:
      IndexOutOfBoundsException - if there are no more bytes
    • unread

      public void unread()
      Unread the previously read byte. Equivalent to unread(1).
      Throws:
      IndexOutOfBoundsException - if there are no more bytes to unread
    • unread

      public void unread(int len)
      Unread the given number of previously read bytes.
      Parameters:
      len - the number of bytes to unread
      Throws:
      IndexOutOfBoundsException - if there are no more bytes to unread
    • readBytes

      public byte[] readBytes(int len)
      Read the specified number of bytes.
      Parameters:
      len - number of bytes to read
      Returns:
      bytes read
      Throws:
      IndexOutOfBoundsException - if there are not enough bytes
      IllegalArgumentException - if len is negative
    • readRemaining

      public byte[] readRemaining()
      Read all the of remaining bytes and advance the read position to the end.
      Returns:
      copy of the remaining data
    • remain

      public int remain()
      Get the number of bytes remaining.
      Returns:
      bytes remaining
    • skip

      public void skip(int num)
      Skip over bytes.
      Parameters:
      num - the number of bytes to skip
      Throws:
      IndexOutOfBoundsException - if num is negative
      IndexOutOfBoundsException - if less than num bytes remain
    • getOffset

      public int getOffset()
      Get current offset into buffer.
      Returns:
      current offset
    • getMax

      public int getMax()
      Get maximum offset into buffer.
      Returns:
      maximum offset
    • getBytes

      public byte[] getBytes(int off, int len)
      Copy a range of bytes from the buffer. Does not change the read position.
      Parameters:
      off - offset into buffer
      len - number of bytes
      Returns:
      copy of the specified byte range
      Throws:
      IndexOutOfBoundsException - if off and/or len is out of bounds
    • getBytes

      public byte[] getBytes(int off)
      Copy a range of bytes from the given offset to the end of the buffer. Does not change the read position.
      Parameters:
      off - offset into buffer
      Returns:
      copy of the specified byte range
      Throws:
      IndexOutOfBoundsException - if off is out of bounds
    • getBytes

      public byte[] getBytes()
      Copy all the of bytes in the buffer. Does not change the read position.
      Returns:
      copy of the entire buffer
    • mark

      public int mark()
      Mark current read position.
      Returns:
      the current offset
    • reset

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

      public InputStream asInputStream()
      Return a view of this instance as an InputStream.
      Returns:
      streaming view of this instance