Package org.bitcoinj.base.internal
Class Buffers
- java.lang.Object
-
- org.bitcoinj.base.internal.Buffers
-
public class Buffers extends java.lang.Object
Utility methods for common operations on Bitcoin P2P message buffers.
-
-
Constructor Summary
Constructors Constructor Description Buffers()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static byte[]
readBytes(java.nio.ByteBuffer buf, int length)
Read given number of bytes from the buffer.static byte[]
readLengthPrefixedBytes(java.nio.ByteBuffer buf)
First read aVarInt
from the buffer and use it to determine the number of bytes to be read.static java.lang.String
readLengthPrefixedString(java.nio.ByteBuffer buf)
First read aVarInt
from the buffer and use it to determine the number of bytes to read.static java.nio.ByteBuffer
skipBytes(java.nio.ByteBuffer buf, int numBytes)
Advance buffer position by a given number of bytes.static java.nio.ByteBuffer
writeLengthPrefixedBytes(java.nio.ByteBuffer buf, byte[] bytes)
First write the length of the byte array as aVarInt
.static java.nio.ByteBuffer
writeLengthPrefixedString(java.nio.ByteBuffer buf, java.lang.String str)
Encode a given string using UTF-8.
-
-
-
Method Detail
-
readBytes
public static byte[] readBytes(java.nio.ByteBuffer buf, int length) throws java.nio.BufferUnderflowException
Read given number of bytes from the buffer.- Parameters:
buf
- buffer to read fromlength
- number of bytes to read- Returns:
- bytes read
- Throws:
java.nio.BufferUnderflowException
- if the read value extends beyond the remaining bytes of the buffer
-
readLengthPrefixedBytes
public static byte[] readLengthPrefixedBytes(java.nio.ByteBuffer buf) throws java.nio.BufferUnderflowException
First read aVarInt
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:
java.nio.BufferUnderflowException
- if the read value extends beyond the remaining bytes of the buffer
-
writeLengthPrefixedBytes
public static java.nio.ByteBuffer writeLengthPrefixedBytes(java.nio.ByteBuffer buf, byte[] bytes) throws java.nio.BufferOverflowException
First write the length of the byte array as aVarInt
. Then write the array contents.- Parameters:
buf
- buffer to write tobytes
- bytes to write- Returns:
- the buffer
- Throws:
java.nio.BufferOverflowException
- if the value doesn't fit the remaining buffer
-
readLengthPrefixedString
public static java.lang.String readLengthPrefixedString(java.nio.ByteBuffer buf) throws java.nio.BufferUnderflowException
First read aVarInt
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:
java.nio.BufferUnderflowException
- if the read value extends beyond the remaining bytes of the buffer
-
writeLengthPrefixedString
public static java.nio.ByteBuffer writeLengthPrefixedString(java.nio.ByteBuffer buf, java.lang.String str) throws java.nio.BufferOverflowException
Encode a given string using UTF-8. Then write the lnegth of the encoded bytes as aVarInt
. Then write the bytes themselves.- Parameters:
buf
- buffer to write tostr
- string to write- Returns:
- the buffer
- Throws:
java.nio.BufferOverflowException
- if the value doesn't fit the remaining buffer
-
skipBytes
public static java.nio.ByteBuffer skipBytes(java.nio.ByteBuffer buf, int numBytes) throws java.nio.BufferUnderflowException
Advance buffer position by a given number of bytes.- Parameters:
buf
- buffer to skip bytes onnumBytes
- number of bytes to skip- Returns:
- the buffer
- Throws:
java.nio.BufferUnderflowException
- if the read value extends beyond the remaining bytes of the buffer
-
-