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