public 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.| 构造器和说明 |
|---|
Bits() |
| 限定符和类型 | 方法和说明 |
|---|---|
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 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(java.nio.ByteBuffer buf)
Reads a char from a buffer.
|
static char |
readChar(java.io.DataInput in)
Reads a char from an input stream
|
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(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(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 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[] |
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(java.nio.ByteBuffer buf)
Reads a short from a buffer.
|
static short |
readShort(java.io.DataInput in)
Reads a short from an input stream
|
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 |
readUnsignedShort(java.nio.ByteBuffer buf)
Reads an unsigned short from a buffer.
|
static int |
readUnsignedShort(java.io.DataInput in)
Reads an unsigned short from an input stream
|
static int |
size(AsciiString str)
Measures the number of bytes required to encode an AsciiSring.
|
static int |
size(char ch)
Computes the size of a variable-length encoded character
|
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(short num)
Computes the size of a variable-length encoded short
|
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 ch,
java.nio.ByteBuffer buf)
Writes a char to a ByteBuffer
|
static void |
writeChar(char ch,
java.io.DataOutput out)
Writes a char to an output stream
|
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,
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,
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 |
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 |
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 num,
java.nio.ByteBuffer buf)
Writes a short to a ByteBuffer
|
static void |
writeShort(short num,
java.io.DataOutput out)
Writes a short to an output stream
|
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 void writeChar(char ch,
java.nio.ByteBuffer buf)
ch - the character to be writtenbuf - the bufferpublic static void writeChar(char ch,
java.io.DataOutput out)
throws java.io.IOException
ch - the character to be writtenout - the output streamjava.io.IOExceptionpublic static char readChar(java.nio.ByteBuffer buf)
buf - the bufferpublic static char readChar(java.io.DataInput in)
throws java.io.IOException
in - the input streamjava.io.IOExceptionpublic static int size(char ch)
ch - the characterpublic static void writeShort(short num,
java.nio.ByteBuffer buf)
num - the short to be writtenbuf - the bufferpublic static void writeShort(short num,
java.io.DataOutput out)
throws java.io.IOException
num - the short to be writtenout - the output streamjava.io.IOExceptionpublic static short readShort(java.nio.ByteBuffer buf)
buf - the bufferpublic static short readShort(java.io.DataInput in)
throws java.io.IOException
in - the input streamjava.io.IOExceptionpublic static int readUnsignedShort(java.nio.ByteBuffer buf)
buf - the bufferpublic static int readUnsignedShort(java.io.DataInput in)
throws java.io.IOException
in - the input streamjava.io.IOExceptionpublic static int size(short num)
num - the shortpublic static short makeShort(byte a,
byte b)
public static short makeShort(byte a)
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.IOExceptionpublic 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.IOExceptionpublic 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.IOExceptionpublic 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.IOExceptionpublic 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.IOExceptionpublic 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.IOExceptionpublic 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.IOExceptionpublic 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.IOExceptionpublic 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.IOExceptionpublic 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.IOExceptionpublic 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.IOExceptionpublic 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.IOExceptionpublic static int sizeUTF(java.lang.String str)
DataOutput.writeUTF(String).str - the stringpublic 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.IOExceptionpublic 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.IOExceptionpublic 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)