Class ByteBuffer

java.lang.Object
com.itextpdf.io.source.ByteBuffer

public class ByteBuffer extends Object
A growable byte buffer with append and byte-oriented conversion operations.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected int
    The number of bytes currently stored in this buffer.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a buffer with the default initial capacity.
    ByteBuffer(int size)
    Creates a buffer with the requested initial capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    append(byte b)
    Appends one byte, expanding the backing array when necessary.
    append(byte[] b)
    Appends all bytes from an array.
    append(byte[] b, int off, int len)
    Appends a range from a byte array.
    append(int b)
    Appends the low eight bits of an integer.
    append(String str)
    Appends the ISO-8859-1 compatible byte representation of a string.
    appendHex(byte b)
    Appends two lowercase hexadecimal characters representing a byte.
    int
    Gets the current backing-array capacity.
    byte
    get(int index)
    Gets a stored byte by index.
    static int
    getHex(int v)
    Converts an ASCII hexadecimal digit to its numeric value.
    byte[]
    Gets the mutable backing array without copying.
    boolean
    Tests whether this buffer has no stored bytes.
    reset()
    Discards all stored bytes while retaining the backing array.
    int
    size()
    Gets the number of bytes stored in this buffer.
    boolean
    startsWith(byte[] b)
    Tests whether the stored bytes begin with a sequence.
    byte[]
    Copies all stored bytes.
    byte[]
    toByteArray(int off, int len)
    Copies a range from the backing array.

    Methods inherited from class java.lang.Object

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

    • count

      protected int count
      The number of bytes currently stored in this buffer.
  • Constructor Details

    • ByteBuffer

      public ByteBuffer()
      Creates a buffer with the default initial capacity.
    • ByteBuffer

      public ByteBuffer (int size)
      Creates a buffer with the requested initial capacity.
      Parameters:
      size - the initial capacity; values below one use the default capacity
  • Method Details

    • getHex

      public static int getHex (int v)
      Converts an ASCII hexadecimal digit to its numeric value.
      Parameters:
      v - the character value to convert
      Returns:
      a value from 0 through 15, or -1 when v is not hexadecimal
    • append

      public ByteBuffer append (byte b)
      Appends one byte, expanding the backing array when necessary.
      Parameters:
      b - the byte to append
      Returns:
      this buffer
    • append

      public ByteBuffer append (byte[] b, int off, int len)
      Appends a range from a byte array.
      Parameters:
      b - the source array
      off - the zero-based source offset
      len - the number of bytes to append
      Returns:
      this buffer; invalid ranges and zero lengths leave it unchanged
    • append

      public ByteBuffer append (byte[] b)
      Appends all bytes from an array.
      Parameters:
      b - the source array
      Returns:
      this buffer
    • append

      public ByteBuffer append (int b)
      Appends the low eight bits of an integer.
      Parameters:
      b - the value whose low byte is appended
      Returns:
      this buffer
    • append

      public ByteBuffer append (String str)
      Appends the ISO-8859-1 compatible byte representation of a string.
      Parameters:
      str - the string to append
      Returns:
      this buffer
    • appendHex

      public ByteBuffer appendHex (byte b)
      Appends two lowercase hexadecimal characters representing a byte.
      Parameters:
      b - the byte to encode
      Returns:
      this buffer
    • get

      public byte get (int index)
      Gets a stored byte by index.
      Parameters:
      index - the zero-based index
      Returns:
      the byte at index
      Throws:
      IndexOutOfBoundsException - if index is at or beyond size()
    • getInternalBuffer

      public byte[] getInternalBuffer()
      Gets the mutable backing array without copying.
      Returns:
      the backing array, whose length may exceed size()
    • size

      public int size()
      Gets the number of bytes stored in this buffer.
      Returns:
      the logical byte count
    • isEmpty

      public boolean isEmpty()
      Tests whether this buffer has no stored bytes.
      Returns:
      true when size() is zero
    • capacity

      public int capacity()
      Gets the current backing-array capacity.
      Returns:
      the number of bytes the backing array can hold without growing
    • reset

      public ByteBuffer reset()
      Discards all stored bytes while retaining the backing array.
      Returns:
      this buffer
    • toByteArray

      public byte[] toByteArray (int off, int len)
      Copies a range from the backing array.
      Parameters:
      off - the zero-based source offset
      len - the number of bytes to copy
      Returns:
      a new array containing the requested bytes
    • toByteArray

      public byte[] toByteArray()
      Copies all stored bytes.
      Returns:
      a new array containing bytes from zero through size()
    • startsWith

      public boolean startsWith (byte[] b)
      Tests whether the stored bytes begin with a sequence.
      Parameters:
      b - the candidate prefix
      Returns:
      true when this buffer starts with b