Class Bits
The write methods write a type (e.g. an int or a char) to a buffer (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.
- Since:
- 3.5
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected static bytebytesRequiredFor(int number) protected static bytebytesRequiredFor(long number) protected static byteencodeLength(byte len1, byte len2) Encodes the number of bytes needed into a single byte.protected static bytefirstNibble(byte len) protected static bytegetByteAt(long num, int index) private static charmakeChar(byte b1, byte b0) static intmakeInt(byte[] buf, int offset, int bytes_to_read) static intstatic intmakeInt(ByteBuffer buffer, int bytes_to_read) static longmakeLong(byte[] buf, int offset, int bytes_to_read) static longstatic longmakeLong(ByteBuffer buffer, int bytes_to_read) static shortmakeShort(byte a, byte b) static AsciiStringReads an AsciiString from buf.static AsciiStringReads an AsciiString from buf.static charreadChar(byte[] buf, int offset) static charreadChar(ByteBuffer buf) static doublereadDouble(byte[] buf, int offset) static doublereadDouble(DataInput in) static doublereadDouble(ByteBuffer buf) static floatreadFloat(byte[] buf, int offset) static floatstatic floatreadFloat(ByteBuffer buf) static intreadInt(byte[] buf, int offset) static intreadInt(ByteBuffer buf) static intreadIntCompressed(byte[] buf, int offset) static intstatic intstatic longreadLong(byte[] buf, int offset) static longreadLong(ByteBuffer buf) static longreadLongCompressed(byte[] buf, int offset) static longstatic longstatic voidreadLongSequence(DataInput in, long[] seqnos, int index) Reads 2 compressed longs into an array of 2 longs.static voidreadLongSequence(ByteBuffer buf, long[] seqnos) Reads 2 compressed longs from buf into seqnosstatic shortreadShort(byte[] buf, int offset) static shortreadShort(ByteBuffer buf) static StringreadString(DataInput in) Reads a string from buf.static StringreadString(ByteBuffer buf) Reads a string from buf.protected static bytesecondNibble(byte len) static intsize(double num) Computes the size of a variable-length encoded doublestatic intsize(float ignored) static intsize(int num) Computes the size of a variable-length encoded intstatic intsize(long num) Computes the size of a variable-length encoded long.static bytesize(long hd, long hr) static intstatic intsize(AsciiString str) Measures the number of bytes required to encode an AsciiSring.static intMeasures the number of bytes required to encode a string, taking multibyte characters into account.static voidwriteAsciiString(AsciiString s, DataOutput out) Writes an AsciiString to buf.static voidwriteAsciiString(AsciiString s, ByteBuffer buf) Writes an AsciiString to buf.static voidwriteChar(char c, byte[] buf, int offset) static voidwriteDouble(double num, byte[] buf, int offset) static voidwriteDouble(double num, DataOutput out) static voidwriteDouble(double num, ByteBuffer buf) static voidwriteFloat(float num, byte[] buf, int offset) static voidwriteFloat(float num, DataOutput out) static voidwriteFloat(float num, ByteBuffer buf) static voidwriteInt(int num, byte[] buf, int offset) static voidwriteIntCompressed(int num, byte[] buf, int offset) static voidwriteIntCompressed(int num, DataOutput out) static voidwriteIntCompressed(int num, ByteBuffer buf) static voidwriteLong(long num, byte[] buf, int offset) static voidwriteLongCompressed(long num, byte[] buf, int offset) static voidwriteLongCompressed(long num, DataOutput out) Writes a long to out in variable-length encodingstatic voidwriteLongCompressed(long num, ByteBuffer buf) static voidwriteLongSequence(long hd, long hr, DataOutput out) Writes 2 sequence numbers (seqnos) in compressed format to an output stream.static voidwriteLongSequence(long hd, long hr, ByteBuffer buf) Writes 2 sequence numbers (seqnos) in compressed format to buf.static voidwriteShort(short s, byte[] buf, int offset) static voidwriteString(String s, DataOutput out) Writes a string to buf.static voidwriteString(String s, ByteBuffer buf) Writes a string to buf.
-
Constructor Details
-
Bits
private Bits()
-
-
Method Details
-
writeChar
public static void writeChar(char c, byte[] buf, int offset) -
readChar
public static char readChar(byte[] buf, int offset) -
readChar
-
makeChar
private static char makeChar(byte b1, byte b0) -
writeShort
public static void writeShort(short s, byte[] buf, int offset) -
readShort
public static short readShort(byte[] buf, int offset) -
readShort
-
makeShort
public static short makeShort(byte a, byte b) -
writeInt
public static void writeInt(int num, byte[] buf, int offset) -
readInt
public static int readInt(byte[] buf, int offset) -
readInt
-
writeIntCompressed
public static void writeIntCompressed(int num, byte[] buf, int offset) -
readIntCompressed
public static int readIntCompressed(byte[] buf, int offset) -
writeIntCompressed
-
readIntCompressed
-
writeIntCompressed
- Throws:
IOException
-
readIntCompressed
- Throws:
IOException
-
makeInt
-
makeInt
- Throws:
IOException
-
makeInt
public static int makeInt(byte[] buf, int offset, int bytes_to_read) -
size
public static int size(int num) Computes the size of a variable-length encoded int- Parameters:
num- the int- Returns:
- the number of bytes needed to variable-length encode num
-
writeLong
public static void writeLong(long num, byte[] buf, int offset) -
readLong
public static long readLong(byte[] buf, int offset) -
readLong
-
writeLongCompressed
public static void writeLongCompressed(long num, byte[] buf, int offset) -
readLongCompressed
public static long readLongCompressed(byte[] buf, int offset) -
writeLongCompressed
-
readLongCompressed
-
writeLongCompressed
Writes a long to out in variable-length encoding- Throws:
IOException
-
readLongCompressed
- Throws:
IOException
-
size
public static int size(long num) Computes the size of a variable-length encoded long. Note that this is not currently using variable-length encoding (will be implemented later).- Parameters:
num- the long- Returns:
- the number of bytes needed to variable-length encode num
-
makeLong
public static long makeLong(byte[] buf, int offset, int bytes_to_read) -
makeLong
-
makeLong
- Throws:
IOException
-
writeLongSequence
Writes 2 sequence numbers (seqnos) in compressed format to buf. The seqnos are non-negative and hr is guaranteed to be >= hd.Once variable-length encoding has been implemented, this method will probably get dropped as we can simply write the 2 longs individually.
- Parameters:
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 to
-
writeLongSequence
Writes 2 sequence numbers (seqnos) in compressed format to an output stream. The seqnos are non-negative and hr is guaranteed to be >= hd.Once variable-length encoding has been implemented, this method will probably get dropped as we can simply write the 2 longs individually.
- Parameters:
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 to- Throws:
IOException
-
readLongSequence
Reads 2 compressed longs from buf into seqnosOnce variable-length encoding has been implemented, this method will probably get dropped as we can simply read the 2 longs individually.
- Parameters:
buf- the buffer to read fromseqnos- the array to read the seqnos into, needs to have a length of 2
-
readLongSequence
Reads 2 compressed longs into an array of 2 longs.Once variable-length encoding has been implemented, this method will probably get dropped as we can simply read the 2 longs individually.
- Parameters:
in- the input stream to read fromseqnos- the array to read the seqnos into, needs to have a length of 2index- the index of the first element to be written; the seqnos are written to seqnos[index] and seqnos[index+1]- Throws:
IOException
-
size
public static byte size(long hd, long hr) -
writeFloat
public static void writeFloat(float num, byte[] buf, int offset) -
readFloat
public static float readFloat(byte[] buf, int offset) -
writeFloat
-
readFloat
-
writeFloat
- Throws:
IOException
-
readFloat
- Throws:
IOException
-
size
public static int size(float ignored) -
writeDouble
public static void writeDouble(double num, byte[] buf, int offset) -
readDouble
public static double readDouble(byte[] buf, int offset) -
writeDouble
-
readDouble
-
writeDouble
- Throws:
IOException
-
readDouble
- Throws:
IOException
-
size
public static int size(double num) Computes the size of a variable-length encoded double- Parameters:
num- the double- Returns:
- the number of bytes needed to variable-length encode num
-
writeString
Writes a string to buf. The length of the string is written first, followed by the chars (as single-byte values). Multi-byte values are truncated: only the lower byte of each multi-byte char is written, similar toDataOutput.writeChars(String).- Parameters:
s- the stringbuf- the buffer
-
writeString
Writes a string to buf. The length of the string is written first, followed by the chars (as single-byte values). Multi-byte values are truncated: only the lower byte of each multi-byte char is written, similar toDataOutput.writeChars(String).- Parameters:
s- the stringout- the output stream- Throws:
IOException
-
readString
Reads a string from buf. The length is read first, followed by the chars. Each char is a single byte- Parameters:
buf- the buffer- Returns:
- the string read from buf
-
readString
Reads a string from buf. The length is read first, followed by the chars. Each char is a single byte- Parameters:
in- the input stream- Returns:
- the string read from buf
- Throws:
IOException
-
sizeUTF
Measures the number of bytes required to encode a string, taking multibyte characters into account. Measures strings written byDataOutput.writeUTF(String).- Parameters:
str- the string- Returns:
- the number of bytes required for encoding str
-
size
-
writeAsciiString
Writes an AsciiString to buf. The length of the string is written first, followed by the chars (as single-byte values).- Parameters:
s- the stringbuf- the buffer
-
writeAsciiString
Writes an AsciiString to buf. The length of the string is written first, followed by the chars (as single-byte values).- Parameters:
s- the stringout- the output stream- Throws:
IOException
-
readAsciiString
Reads an AsciiString from buf. The length is read first, followed by the chars. Each char is a single byte- Parameters:
buf- the buffer- Returns:
- the string read from buf
-
readAsciiString
Reads an AsciiString from buf. The length is read first, followed by the chars. Each char is a single byte- Parameters:
in- the input stream- Returns:
- the string read from buf
- Throws:
IOException
-
size
Measures the number of bytes required to encode an AsciiSring.- Parameters:
str- the string- Returns:
- the number of bytes required for encoding str
-
encodeLength
protected static byte encodeLength(byte len1, byte len2) Encodes the number of bytes needed into a single byte. The first number is encoded in the first nibble (the first 4 bits), the second number in the second nibble- Parameters:
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 8- Returns:
- The byte storing the 2 numbers len1 and len2
-
firstNibble
protected static byte firstNibble(byte len) -
secondNibble
protected static byte secondNibble(byte len) -
bytesRequiredFor
protected static byte bytesRequiredFor(long number) -
bytesRequiredFor
protected static byte bytesRequiredFor(int number) -
getByteAt
protected static byte getByteAt(long num, int index)
-