Class Buffers

java.lang.Object
org.bitcoinj.base.internal.Buffers

public class Buffers extends Object
Utility methods for common operations on Bitcoin P2P message buffers.
  • Constructor Details

    • Buffers

      public Buffers()
  • Method Details

    • readBytes

      public static byte[] readBytes(ByteBuffer buf, int length) throws BufferUnderflowException
      Read given number of bytes from the buffer.
      Parameters:
      buf - buffer to read from
      length - number of bytes to read
      Returns:
      bytes read
      Throws:
      BufferUnderflowException - if the read value extends beyond the remaining bytes of the buffer
    • readLengthPrefixedBytes

      public static byte[] readLengthPrefixedBytes(ByteBuffer buf) throws BufferUnderflowException
      First read a VarInt from the buffer and use it to determine the number of bytes to be read. Then read that many bytes into the byte array to be returned. This construct is frequently used by Bitcoin protocols.
      Parameters:
      buf - buffer to read from
      Returns:
      read bytes
      Throws:
      BufferUnderflowException - if the read value extends beyond the remaining bytes of the buffer
    • writeLengthPrefixedBytes

      public static ByteBuffer writeLengthPrefixedBytes(ByteBuffer buf, byte[] bytes) throws BufferOverflowException
      First write the length of the byte array as a VarInt. Then write the array contents.
      Parameters:
      buf - buffer to write to
      bytes - bytes to write
      Returns:
      the buffer
      Throws:
      BufferOverflowException - if the value doesn't fit the remaining buffer
    • readLengthPrefixedString

      public static String readLengthPrefixedString(ByteBuffer buf) throws BufferUnderflowException
      First read a VarInt from the buffer and use it to determine the number of bytes to read. Then read that many bytes and interpret it as an UTF-8 encoded string to be returned. This construct is frequently used by Bitcoin protocols.
      Parameters:
      buf - buffer to read from
      Returns:
      read string
      Throws:
      BufferUnderflowException - if the read value extends beyond the remaining bytes of the buffer
    • writeLengthPrefixedString

      public static ByteBuffer writeLengthPrefixedString(ByteBuffer buf, String str) throws BufferOverflowException
      Encode a given string using UTF-8. Then write the length of the encoded bytes as a VarInt. Then write the bytes themselves.
      Parameters:
      buf - buffer to write to
      str - string to write
      Returns:
      the buffer
      Throws:
      BufferOverflowException - if the value doesn't fit the remaining buffer
    • skipBytes

      public static ByteBuffer skipBytes(ByteBuffer buf, int numBytes) throws BufferUnderflowException
      Advance buffer position by a given number of bytes.
      Parameters:
      buf - buffer to skip bytes on
      numBytes - number of bytes to skip
      Returns:
      the buffer
      Throws:
      BufferUnderflowException - if the read value extends beyond the remaining bytes of the buffer