public final class Bits extends Object
ByteBuffer or output stream, using
variable-length encoding. If
there are not enough byte in the buffer to write a type, a BufferOverflowException is thrown.
If the variable cannot be written to the output stream, an IOException is thrown.
The read methods read a variable-length encoded type from a buffer or input stream. If there are fewer bytes in
the buffer than needed to read the type, a BufferUnderflowException is thrown. If the read fails,
an IOException is thrown.
The size() methods return the number of bytes used to encode the given type with variable-length encoding.
There are additional helper methods to write/read custom JGroups types, e.g. address lists, Views etc
Note that methods to read/write atomic types (char, int etc) should only be used if variable-length encoding is
desired; otherwise DataOutput.writeInt(int) or ByteBuffer.putInt(int) should be used instead.
At the time of writing this (Feb 2014), most methods have not yet been implemented.| Modifier and Type | Method and Description |
|---|---|
protected static byte |
bytesRequiredFor(int number) |
protected static byte |
bytesRequiredFor(long number) |
protected static byte[] |
decodeLength(byte len) |
protected static byte |
encodeLength(byte len1,
byte len2)
Encodes the number of bytes needed into a single byte.
|
protected static byte |
getByteAt(long num,
int index) |
static char |
makeChar(byte[] buf,
int offset) |
static int |
makeInt(byte[] buf,
int offset,
int bytes_to_read) |
static long |
makeLong(byte[] buf,
int offset,
int bytes_to_read) |
static short |
makeShort(byte a) |
static short |
makeShort(byte a,
byte b) |
static AsciiString |
readAsciiString(ByteBuffer buf)
Reads an AsciiString from buf.
|
static AsciiString |
readAsciiString(DataInput in)
Reads an AsciiString from buf.
|
static char |
readChar(byte[] buf,
int offset) |
static double |
readDouble(byte[] buf,
int offset) |
static double |
readDouble(ByteBuffer buf)
Reads a double from a buffer.
|
static double |
readDouble(DataInput in)
Reads a double from an input stream
|
static float |
readFloat(byte[] buf,
int offset) |
static float |
readFloat(ByteBuffer buf)
Reads a a float from a buffer.
|
static float |
readFloat(DataInput in)
Reads a a float from an input stream.
|
static int |
readInt(byte[] buf,
int offset) |
static int |
readInt(ByteBuffer buf)
Reads an int from a buffer.
|
static int |
readInt(DataInput in)
Reads an int from an input stream
|
static int |
readIntCompressed(byte[] buf,
int offset) |
static long |
readLong(byte[] buf,
int offset) |
static long |
readLong(ByteBuffer buf)
Reads a long from a buffer.
|
static long |
readLong(DataInput in)
Reads a variable-length encoded long from an input stream.
|
static long |
readLongCompressed(byte[] buf,
int offset) |
static long[] |
readLongSequence(ByteBuffer buf)
Reads 2 compressed longs from buf.
|
static long[] |
readLongSequence(DataInput in)
Reads 2 compressed longs from in.
|
static short |
readShort(byte[] buf,
int offset) |
static String |
readString(ByteBuffer buf)
Reads a string from buf.
|
static String |
readString(DataInput in)
Reads a string from buf.
|
static int |
size(AsciiString str)
Measures the number of bytes required to encode an AsciiSring.
|
static int |
size(double num)
Computes the size of a variable-length encoded double
|
static int |
size(float num)
Computes the size of a variable-length encoded float
|
static int |
size(int num)
Computes the size of a variable-length encoded int
|
static int |
size(long num)
Computes the size of a variable-length encoded long.
|
static byte |
size(long hd,
long hr) |
static int |
size(String str) |
static int |
sizeUTF(String str)
Measures the number of bytes required to encode a string, taking multibyte characters into account.
|
static void |
writeAsciiString(AsciiString s,
ByteBuffer buf)
Writes an AsciiString to buf.
|
static void |
writeAsciiString(AsciiString s,
DataOutput out)
Writes an AsciiString to buf.
|
static void |
writeChar(char c,
byte[] buf,
int offset) |
static void |
writeDouble(double num,
byte[] buf,
int offset) |
static void |
writeDouble(double num,
ByteBuffer buf)
Writes a double to a ByteBuffer
|
static void |
writeDouble(double num,
DataOutput out)
Writes a double to an output stream
|
static void |
writeFloat(float num,
byte[] buf,
int offset) |
static void |
writeFloat(float num,
ByteBuffer buf)
Writes a float to a ByteBuffer
|
static void |
writeFloat(float num,
DataOutput out)
Writes a float to an output stream
|
static void |
writeInt(int num,
byte[] buf,
int offset) |
static void |
writeInt(int num,
ByteBuffer buf)
Writes an int to a ByteBuffer
|
static void |
writeInt(int num,
DataOutput out)
Writes an int to an output stream
|
static void |
writeIntCompressed(int num,
byte[] buf,
int offset) |
static void |
writeLong(long num,
byte[] buf,
int offset) |
static void |
writeLong(long num,
ByteBuffer buf)
Writes a long to a ByteBuffer
|
static void |
writeLong(long num,
DataOutput out)
Writes a long to out in variable-length encoding.
|
static void |
writeLongCompressed(long num,
byte[] buf,
int offset) |
static void |
writeLongSequence(long hd,
long hr,
ByteBuffer buf)
Writes 2 sequence numbers (seqnos) in compressed format to buf.
|
static void |
writeLongSequence(long hd,
long hr,
DataOutput out)
Writes 2 sequence numbers (seqnos) in compressed format to an output stream.
|
static void |
writeShort(short s,
byte[] buf,
int offset) |
static void |
writeString(String s,
ByteBuffer buf)
Writes a string to buf.
|
static void |
writeString(String s,
DataOutput out)
Writes a string to buf.
|
public static char makeChar(byte[] buf,
int offset)
public static void writeChar(char c,
byte[] buf,
int offset)
public static char readChar(byte[] buf,
int offset)
public static short makeShort(byte a,
byte b)
public static short makeShort(byte a)
public static void writeShort(short s,
byte[] buf,
int offset)
public static short readShort(byte[] buf,
int offset)
public static void writeInt(int num,
ByteBuffer buf)
num - the int to be writtenbuf - the bufferpublic static void writeInt(int num,
DataOutput out)
throws IOException
num - the int to be writtenout - the output streamIOExceptionpublic static void writeInt(int num,
byte[] buf,
int offset)
public static void writeIntCompressed(int num,
byte[] buf,
int offset)
public static int readInt(ByteBuffer buf)
buf - the bufferpublic static int readInt(DataInput in) throws IOException
in - the input streamIOExceptionpublic static int readInt(byte[] buf,
int offset)
public static int readIntCompressed(byte[] buf,
int offset)
public static int size(int num)
num - the intpublic static void writeLong(long num,
ByteBuffer buf)
num - the long to be writtenbuf - the bufferpublic static void writeLong(long num,
DataOutput out)
throws IOException
num - the longout - the output stream to write num toIOExceptionpublic static void writeLong(long num,
byte[] buf,
int offset)
public static void writeLongCompressed(long num,
byte[] buf,
int offset)
public static long readLong(ByteBuffer buf)
buf - the bufferpublic static long readLong(DataInput in) throws IOException
in - the input streamIOExceptionpublic static long readLong(byte[] buf,
int offset)
public static long readLongCompressed(byte[] buf,
int offset)
public static int size(long num)
num - the longpublic static void writeLongSequence(long hd,
long hr,
ByteBuffer buf)
hd - the highest delivered seqno. Guaranteed to be a positive numberhr - the highest received seqno. Guaranteed to be a positive number. Greater than or equal to hdbuf - the buffer to write topublic static void writeLongSequence(long hd,
long hr,
DataOutput out)
throws IOException
hd - the highest delivered seqno. Guaranteed to be a positive numberhr - the highest received seqno. Guaranteed to be a positive number. Greater than or equal to hdout - the output stream to write toIOExceptionpublic static long[] readLongSequence(ByteBuffer buf)
buf - the buffer to read frompublic static long[] readLongSequence(DataInput in) throws IOException
in - the input stream to read fromIOExceptionpublic static byte size(long hd,
long hr)
public static long makeLong(byte[] buf,
int offset,
int bytes_to_read)
public static int makeInt(byte[] buf,
int offset,
int bytes_to_read)
public static void writeFloat(float num,
ByteBuffer buf)
num - the float to be writtenbuf - the bufferpublic static void writeFloat(float num,
DataOutput out)
throws IOException
num - the float to be writtenout - the output streamIOExceptionpublic static void writeFloat(float num,
byte[] buf,
int offset)
public static float readFloat(ByteBuffer buf)
buf - the bufferpublic static float readFloat(DataInput in) throws IOException
in - the input streamIOExceptionpublic static float readFloat(byte[] buf,
int offset)
public static int size(float num)
num - the floatpublic static void writeDouble(double num,
ByteBuffer buf)
num - the double to be writtenbuf - the bufferpublic static void writeDouble(double num,
DataOutput out)
throws IOException
num - the double to be writtenout - the output streamIOExceptionpublic static void writeDouble(double num,
byte[] buf,
int offset)
public static double readDouble(ByteBuffer buf)
buf - the bufferpublic static double readDouble(DataInput in) throws IOException
in - the input streamIOExceptionpublic static double readDouble(byte[] buf,
int offset)
public static int size(double num)
num - the doublepublic static void writeString(String s, ByteBuffer buf)
DataOutput.writeChars(String).s - the stringbuf - the bufferpublic static void writeString(String s, DataOutput out) throws IOException
DataOutput.writeChars(String).s - the stringout - the output streamIOExceptionpublic static String readString(ByteBuffer buf)
buf - the bufferpublic static String readString(DataInput in) throws IOException
in - the input streamIOExceptionpublic static int sizeUTF(String str)
DataOutput.writeUTF(String).str - the stringpublic static int size(String str)
public static void writeAsciiString(AsciiString s, ByteBuffer buf)
s - the stringbuf - the bufferpublic static void writeAsciiString(AsciiString s, DataOutput out) throws IOException
s - the stringout - the output streamIOExceptionpublic static AsciiString readAsciiString(ByteBuffer buf)
buf - the bufferpublic static AsciiString readAsciiString(DataInput in) throws IOException
in - the input streamIOExceptionpublic static int size(AsciiString str)
str - the stringprotected static byte encodeLength(byte len1,
byte len2)
len1 - The number of bytes needed to store a long. Must be between 0 and 8len2 - The number of bytes needed to store a long. Must be between 0 and 8protected static byte[] decodeLength(byte len)
protected static byte bytesRequiredFor(long number)
protected static byte bytesRequiredFor(int number)
protected static byte getByteAt(long num,
int index)
Copyright © 2025 JBoss, a division of Red Hat. All rights reserved.