public class Protocols extends Object
Constructor and Description |
---|
Protocols() |
Modifier and Type | Method and Description |
---|---|
static boolean |
canReadInt(ByteBufferView buffer)
Check that remaining bytes in buffer are enough to read an int
|
static BitSet |
copyOf(BitSet bitSet)
Get a copy of the provided bitset.
|
static byte[] |
fromHex(String s)
Get binary data from its' hex-encoded representation (regardless of case).
|
static int |
getBit(byte[] bytes,
BitOrder bitOrder,
int i)
Gets i-th bit in a bitmask.
|
static byte[] |
getIntBytes(int i)
Get 4-bytes binary representation of an
Integer . |
static byte[] |
getLongBytes(long l)
Get 8-bytes binary representation of a
Long . |
static byte[] |
getShortBytes(int s)
Get 2-bytes binary representation of a
Short . |
static byte[] |
infoHashFromBase32(String s)
Get 20-bytes long info hash from its' base32-encoded representation (regardless of case).
|
static boolean |
isSet(byte[] bytes,
BitOrder bitOrder,
int i)
Check if i-th bit in the bitmask is set.
|
static int |
readInt(byte[] bytes,
int offset)
Decode the binary representation of an
Integer from a byte array. |
static Integer |
readInt(ByteBuffer buffer)
Decode the binary representation of an
Integer from a buffer. |
static int |
readInt(ByteBufferView buffer)
Decode the binary representation of an
Integer from a buffer. |
static long |
readLong(byte[] bytes,
int offset)
Decode the binary representation of a
Long from a byte array. |
static Long |
readLong(ByteBuffer buffer)
Decode the binary representation of a
Long from a buffer. |
static short |
readShort(byte[] bytes,
int offset)
Decode the binary representation of a
Short from a byte array. |
static Short |
readShort(ByteBuffer buffer)
Decode the binary representation of a
Short from a buffer. |
static Short |
readShort(ByteBufferView buffer)
Decode the binary representation of a
Short from a buffer. |
static byte |
reverseBits(byte b)
Returns a reversed byte.
|
static byte[] |
reverseBits(byte[] bytes)
Returns a copy of the provided byte array with each byte reversed.
|
static void |
setBit(byte[] bytes,
BitOrder bitOrder,
int i)
Sets i-th bit in a bitmask.
|
static String |
toHex(byte[] bytes)
Get hex-encoded representation of a binary array.
|
static void |
verifyPayloadHasLength(Class<? extends Message> type,
int expectedLength,
int actualLength)
Convenience method to check if actual message length is the same as expected length.
|
public static byte[] getLongBytes(long l)
Long
.public static byte[] getIntBytes(int i)
Integer
.public static byte[] getShortBytes(int s)
Short
.public static long readLong(byte[] bytes, int offset)
Long
from a byte array.bytes
- Arbitrary byte array.
It's length must be at least offset + 8.offset
- Offset in byte array to start decoding from (inclusive, 0-based)public static Long readLong(ByteBuffer buffer)
Long
from a buffer.buffer
- Buffer to read from.
Decoding will be done starting with the index denoted by Buffer.position()
public static int readInt(byte[] bytes, int offset)
Integer
from a byte array.bytes
- Arbitrary byte array.
It's length must be at least offset + 4.offset
- Offset in byte array to start decoding from (inclusive, 0-based)public static Integer readInt(ByteBuffer buffer)
Integer
from a buffer.buffer
- Buffer to read from.
Decoding will be done starting with the index denoted by Buffer.position()
public static int readInt(ByteBufferView buffer)
Integer
from a buffer.buffer
- Buffer to read from.
Decoding will be done starting with the index denoted by Buffer.position()
IllegalStateException
- if there are insufficient bytes in buffer
(i.e. buffer.remaining() < 4)public static boolean canReadInt(ByteBufferView buffer)
buffer
- Buffer to read from.public static short readShort(byte[] bytes, int offset)
Short
from a byte array.bytes
- Arbitrary byte array.
It's length must be at least offset + 2.offset
- Offset in byte array to start decoding from (inclusive, 0-based)public static Short readShort(ByteBuffer buffer)
Short
from a buffer.buffer
- Buffer to read from.
Decoding will be done starting with the index denoted by Buffer.position()
public static Short readShort(ByteBufferView buffer)
Short
from a buffer.buffer
- Buffer to read from.
Decoding will be done starting with the index denoted by Buffer.position()
public static void verifyPayloadHasLength(Class<? extends Message> type, int expectedLength, int actualLength)
InvalidMessageException
- if expectedLength != actualLengthpublic static void setBit(byte[] bytes, BitOrder bitOrder, int i)
bytes
- Bitmask.bitOrder
- Order of bits in a bytei
- Bit index (0-based)public static int getBit(byte[] bytes, BitOrder bitOrder, int i)
bytes
- Bitmask.bitOrder
- Order of bits in a bytei
- Bit index (0-based)public static boolean isSet(byte[] bytes, BitOrder bitOrder, int i)
bytes
- Bitmask.bitOrder
- Order of bits in a bytei
- Bit index (0-based)public static String toHex(byte[] bytes)
bytes
- Binary datapublic static byte[] fromHex(String s)
s
- Hex-encoded representation of binary datapublic static byte[] infoHashFromBase32(String s)
s
- base32-encoded representation of info hashIllegalArgumentException
- if s.length()
is not 32 characters longpublic static byte[] reverseBits(byte[] bytes)
public static byte reverseBits(byte b)
Copyright © 2016–2021. All rights reserved.