public final class Bits
extends java.lang.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(java.nio.ByteBuffer buf)
Reads an AsciiString from buf.
|
static AsciiString |
readAsciiString(java.io.DataInput in)
Reads an AsciiString from buf.
|
static char |
readChar(byte[] buf,
int offset) |
static double |
readDouble(byte[] buf,
int offset) |
static double |
readDouble(java.nio.ByteBuffer buf)
Reads a double from a buffer.
|
static double |
readDouble(java.io.DataInput in)
Reads a double from an input stream
|
static float |
readFloat(byte[] buf,
int offset) |
static float |
readFloat(java.nio.ByteBuffer buf)
Reads a a float from a buffer.
|
static float |
readFloat(java.io.DataInput in)
Reads a a float from an input stream.
|
static int |
readInt(byte[] buf,
int offset) |
static int |
readInt(java.nio.ByteBuffer buf)
Reads an int from a buffer.
|
static int |
readInt(java.io.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(java.nio.ByteBuffer buf)
Reads a long from a buffer.
|
static long |
readLong(java.io.DataInput in)
Reads a variable-length encoded long from an input stream.
|
static long |
readLongCompressed(byte[] buf,
int offset) |
static long[] |
readLongSequence(java.nio.ByteBuffer buf)
Reads 2 compressed longs from buf.
|
static long[] |
readLongSequence(java.io.DataInput in)
Reads 2 compressed longs from in.
|
static short |
readShort(byte[] buf,
int offset) |
static java.lang.String |
readString(java.nio.ByteBuffer buf)
Reads a string from buf.
|
static java.lang.String |
readString(java.io.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(java.lang.String str) |
static int |
sizeUTF(java.lang.String str)
Measures the number of bytes required to encode a string, taking multibyte characters into account.
|
static void |
writeAsciiString(AsciiString s,
java.nio.ByteBuffer buf)
Writes an AsciiString to buf.
|
static void |
writeAsciiString(AsciiString s,
java.io.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,
java.nio.ByteBuffer buf)
Writes a double to a ByteBuffer
|
static void |
writeDouble(double num,
java.io.DataOutput out)
Writes a double to an output stream
|
static void |
writeFloat(float num,
byte[] buf,
int offset) |
static void |
writeFloat(float num,
java.nio.ByteBuffer buf)
Writes a float to a ByteBuffer
|
static void |
writeFloat(float num,
java.io.DataOutput out)
Writes a float to an output stream
|
static void |
writeInt(int num,
byte[] buf,
int offset) |
static void |
writeInt(int num,
java.nio.ByteBuffer buf)
Writes an int to a ByteBuffer
|
static void |
writeInt(int num,
java.io.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,
java.nio.ByteBuffer buf)
Writes a long to a ByteBuffer
|
static void |
writeLong(long num,
java.io.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,
java.nio.ByteBuffer buf)
Writes 2 sequence numbers (seqnos) in compressed format to buf.
|
static void |
writeLongSequence(long hd,
long hr,
java.io.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(java.lang.String s,
java.nio.ByteBuffer buf)
Writes a string to buf.
|
static void |
writeString(java.lang.String s,
java.io.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, java.nio.ByteBuffer buf)
num
- the int to be writtenbuf
- the bufferpublic static void writeInt(int num, java.io.DataOutput out) throws java.io.IOException
num
- the int to be writtenout
- the output streamjava.io.IOException
public static void writeInt(int num, byte[] buf, int offset)
public static void writeIntCompressed(int num, byte[] buf, int offset)
public static int readInt(java.nio.ByteBuffer buf)
buf
- the bufferpublic static int readInt(java.io.DataInput in) throws java.io.IOException
in
- the input streamjava.io.IOException
public 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, java.nio.ByteBuffer buf)
num
- the long to be writtenbuf
- the bufferpublic static void writeLong(long num, java.io.DataOutput out) throws java.io.IOException
num
- the longout
- the output stream to write num tojava.io.IOException
public static void writeLong(long num, byte[] buf, int offset)
public static void writeLongCompressed(long num, byte[] buf, int offset)
public static long readLong(java.nio.ByteBuffer buf)
buf
- the bufferpublic static long readLong(java.io.DataInput in) throws java.io.IOException
in
- the input streamjava.io.IOException
public 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, java.nio.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, java.io.DataOutput out) throws java.io.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 tojava.io.IOException
public static long[] readLongSequence(java.nio.ByteBuffer buf)
buf
- the buffer to read frompublic static long[] readLongSequence(java.io.DataInput in) throws java.io.IOException
in
- the input stream to read fromjava.io.IOException
public 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, java.nio.ByteBuffer buf)
num
- the float to be writtenbuf
- the bufferpublic static void writeFloat(float num, java.io.DataOutput out) throws java.io.IOException
num
- the float to be writtenout
- the output streamjava.io.IOException
public static void writeFloat(float num, byte[] buf, int offset)
public static float readFloat(java.nio.ByteBuffer buf)
buf
- the bufferpublic static float readFloat(java.io.DataInput in) throws java.io.IOException
in
- the input streamjava.io.IOException
public static float readFloat(byte[] buf, int offset)
public static int size(float num)
num
- the floatpublic static void writeDouble(double num, java.nio.ByteBuffer buf)
num
- the double to be writtenbuf
- the bufferpublic static void writeDouble(double num, java.io.DataOutput out) throws java.io.IOException
num
- the double to be writtenout
- the output streamjava.io.IOException
public static void writeDouble(double num, byte[] buf, int offset)
public static double readDouble(java.nio.ByteBuffer buf)
buf
- the bufferpublic static double readDouble(java.io.DataInput in) throws java.io.IOException
in
- the input streamjava.io.IOException
public static double readDouble(byte[] buf, int offset)
public static int size(double num)
num
- the doublepublic static void writeString(java.lang.String s, java.nio.ByteBuffer buf)
DataOutput.writeChars(String)
.s
- the stringbuf
- the bufferpublic static void writeString(java.lang.String s, java.io.DataOutput out) throws java.io.IOException
DataOutput.writeChars(String)
.s
- the stringout
- the output streamjava.io.IOException
public static java.lang.String readString(java.nio.ByteBuffer buf)
buf
- the bufferpublic static java.lang.String readString(java.io.DataInput in) throws java.io.IOException
in
- the input streamjava.io.IOException
public static int sizeUTF(java.lang.String str)
DataOutput.writeUTF(String)
.str
- the stringpublic static int size(java.lang.String str)
public static void writeAsciiString(AsciiString s, java.nio.ByteBuffer buf)
s
- the stringbuf
- the bufferpublic static void writeAsciiString(AsciiString s, java.io.DataOutput out) throws java.io.IOException
s
- the stringout
- the output streamjava.io.IOException
public static AsciiString readAsciiString(java.nio.ByteBuffer buf)
buf
- the bufferpublic static AsciiString readAsciiString(java.io.DataInput in) throws java.io.IOException
in
- the input streamjava.io.IOException
public 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 © 1998-2020 Red Hat. All Rights Reserved.