|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--netbyte.NetByte
Converting to and from network byte streams for primitive data types.
Author: Saleem N. Bhatti <s.bhatti@cs.ucl.ac.uk>
Version: 1.1 (revision history is
here)
Date: Oct 2002
All this software is distributed under the
GNU Lesser General
Public License
http://www.gnu.org/copyleft/lesser.html
This class consists of a set of public static methods to help with conversion to and from a network byte stream for the primitive Java types:
char short int long float double
The byte stream produced is in network byte order:
char
, short
,
int
and long
, the most-significant bit of
the value is transmitted firstfloat
and double
, the
encoding is ANSI/IEEE 754 (1985), with the sign-bit transmitted firstSome constants are also defined for the sizes of these types and a couple of methods to hexdump a byte stream.
The constants used for the size of the primitive types are taken from the definitions of the value ranges in the Java Language Specification - see here for the value ranges of:
byte char short int long
and see here for the value ranges of:
float double
The sizes are summarised in the table below.
primitive type | size (bytes) |
---|---|
byte | 1 |
char | 2 |
short | 2 |
int | 4 |
long | 8 |
float | 4 |
double | 8 |
Field Summary | |
static int |
byteSize_
Size of a byte (1). |
static int |
charSize_
Size of a char (2). |
static int |
doubleSize_
Size of a doublne (8). |
static int |
floatSize_
Size of a float (4). |
static int |
intSize_
Size of an int (4). |
static int |
longSize_
Size of a long (8). |
static int |
shortSize_
Size of a short (2). |
Constructor Summary | |
NetByte()
|
Method Summary | |
static java.lang.String |
bytesToHexString(byte[] buffer,
int length,
int offset)
Gives the hexadecimal representation of the values in a byte array. |
static java.lang.String |
byteToHexString(byte b)
Gives the hexadecimal representation of a byte value. |
static char |
decodeChar(byte[] b)
Turns an encoding of 2 bytes into a char value. |
static char |
decodeChar(byte[] b,
int offset)
Turns an encoding of 2 bytes into a char value. |
static double |
decodeDouble(byte[] b)
Turns an encoding of 8 bytes into a double value. |
static double |
decodeDouble(byte[] b,
int offset)
Turns an encoding of 8 bytes into a double value. |
static float |
decodeFloat(byte[] b)
Turns an encoding of 4 bytes into a float value. |
static float |
decodeFloat(byte[] b,
int offset)
Turns an encoding of 4 bytes into a float value. |
static int |
decodeInt(byte[] b)
Turns an encoding of 4 bytes into an int value. |
static int |
decodeInt(byte[] b,
int offset)
Turns an encoding of 4 bytes into an int value. |
static long |
decodeLong(byte[] b)
Turns an encoding of 8 bytes into a long value. |
static long |
decodeLong(byte[] b,
int offset)
Turns an encoding of 8 bytes into a long value. |
static short |
decodeShort(byte[] b)
Turns an encoding of 2 bytes into a short value. |
static short |
decodeShort(byte[] b,
int offset)
Turns an encoding of 2 bytes into a short value. |
static byte[] |
encodeChar(char c)
Turns a char into a byte stream. |
static void |
encodeChar(char c,
byte[] b,
int offset)
Turns a char into a byte stream. |
static byte[] |
encodeDouble(double d)
Turns a double into a byte stream. |
static void |
encodeDouble(double d,
byte[] b,
int offset)
Turns a double into a byte stream. |
static byte[] |
encodeFloat(float f)
Turns a float into a byte stream. |
static void |
encodeFloat(float f,
byte[] b,
int offset)
Turns a float into a byte stream. |
static byte[] |
encodeInt(int i)
Turns an int into a byte stream. |
static void |
encodeInt(int i,
byte[] b,
int offset)
Turns an int into a byte stream. |
static byte[] |
encodeLong(long l)
Turns a long into a byte stream. |
static void |
encodeLong(long l,
byte[] b,
int offset)
Turns a long into a byte stream. |
static byte[] |
encodeShort(short s)
Turns a short into a byte stream. |
static void |
encodeShort(short s,
byte[] b,
int offset)
Turns a short into a byte stream. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
public static final int byteSize_
byte
(1).
public static final int shortSize_
short
(2).
public static final int charSize_
char
(2).
public static final int intSize_
int
(4).
public static final int longSize_
long
(8).
public static final int floatSize_
float
(4).
public static final int doubleSize_
doublne
(8).
Constructor Detail |
public NetByte()
Method Detail |
public static byte[] encodeShort(short s)
short
into a byte stream.
s
- the value to encode.
byte
array of size 2 with the value
in network byte order, most-significant bit first.public static void encodeShort(short s, byte[] b, int offset) throws NetByteEncodingException
short
into a byte stream.
s
- the value to encode.b
- the byte array in which to write the encoded value,
with the value in network byte order, most-significant bit first.offset
- the offset at which to start writing in the byte array.
NetByteEncodingException
- if the byte array is too small.public static short decodeShort(byte[] b) throws NetByteDecodingException
short
value.
b
- the byte array in which contains the encoded value,
with the value in network byte order, most-significant bit first.
short
value formed from the byte array.
NetByteDecodingException
- if the byte array is too small.public static short decodeShort(byte[] b, int offset) throws NetByteDecodingException
short
value.
b
- the byte array from which to read the encoded value.offset
- the offset at which to start reading from the byte array.
short
value formed from the byte array.
NetByteDecodingException
- if the byte array is too small.public static byte[] encodeChar(char c)
char
into a byte stream.
c
- the value to encode.
byte
array of size 2 with the value
in network byte order, most-significant bit first.public static void encodeChar(char c, byte[] b, int offset) throws NetByteEncodingException
char
into a byte stream.
c
- the value to encode.b
- the byte array in which to write the encoded value,
with the value in network byte order, most-significant bit first.offset
- the offset at which to start writing in the byte array.
NetByteEncodingException
- if the byte array is too small.public static char decodeChar(byte[] b) throws NetByteDecodingException
char
value.
b
- the byte array in which contains the encoded value.
char
value formed from the byte array.
NetByteDecodingException
- if the byte array is too small.public static char decodeChar(byte[] b, int offset) throws NetByteDecodingException
char
value.
b
- the byte array from which to read the encoded value.offset
- the offset at which to start reading from the byte array.
short
value formed from the byte array.
NetByteDecodingException
- if the byte array is too small.public static byte[] encodeInt(int i)
int
into a byte stream.
i
- the value to encode.
byte
array of size 4 with the value
in network byte order, most-significant bit first.public static void encodeInt(int i, byte[] b, int offset) throws NetByteEncodingException
int
into a byte stream.
i
- the value to encode.b
- the byte array in which to write the encoded value,
with the value in network byte order, most-significant bit first.offset
- the offset at which to start writing in the byte array.
NetByteEncodingException
- if the byte array is too small.public static int decodeInt(byte[] b) throws NetByteDecodingException
int
value.
b
- the byte array in which contains the encoded value.
int
value formed from the byte array.
NetByteDecodingException
- if the byte array is too small.public static int decodeInt(byte[] b, int offset) throws NetByteDecodingException
int
value.
b
- the byte array from which to read the encoded value.offset
- the offset at which to start reading from the byte array.
int
value formed from the byte array.
NetByteDecodingException
- if the byte array is too small.public static byte[] encodeLong(long l)
long
into a byte stream.
l
- the value to encode.
byte
array of size 8 with the value
in network byte order, most-significant bit first.public static void encodeLong(long l, byte[] b, int offset) throws NetByteEncodingException
long
into a byte stream.
l
- the value to encode.b
- the byte array in which to write the encoded value,
with the value in network byte order, most-significant bit first.offset
- the offset at which to start writing in the byte array.
NetByteEncodingException
- if the byte array is too small.public static long decodeLong(byte[] b) throws NetByteDecodingException
long
value.
b
- the byte array in which contains the encoded value.
long
value formed from the byte array.
NetByteDecodingException
- if the byte array is too small.public static long decodeLong(byte[] b, int offset) throws NetByteDecodingException
long
value.
b
- the byte array from which to read the encoded value.offset
- the offset at which to start reading from the byte array.
long
value formed from the byte array.
NetByteDecodingException
- if the byte array is too small.public static byte[] encodeFloat(float f)
float
into a byte stream.
f
- the value to encode.
byte
array of size 4 with the value,
in ANSI/IEE 754 (1985) format, sign-bit first.public static void encodeFloat(float f, byte[] b, int offset) throws NetByteEncodingException
float
into a byte stream.
f
- the value to encode.b
- the byte array in which to write the encoded value,
in ANSI/IEE 754 (1985) format, sign-bit first.offset
- the offset at which to start writing in the byte array.
NetByteEncodingException
- if the byte array is too small.public static float decodeFloat(byte[] b) throws NetByteDecodingException
float
value.
b
- the byte array in which contains the encoded value.
float
value formed from the byte array.
NetByteDecodingException
- if the byte array is too small.public static float decodeFloat(byte[] b, int offset) throws NetByteDecodingException
float
value.
b
- the byte array from which to read the encoded value.offset
- the offset at which to start reading from the byte array.
float
value formed from the byte array.
NetByteDecodingException
- if the byte array is too small.public static byte[] encodeDouble(double d)
double
into a byte stream.
d
- the value to encode.
byte
array of size 8 with the value,
in ANSI/IEE 754 (1985) format, sign-bit first.public static void encodeDouble(double d, byte[] b, int offset) throws NetByteEncodingException
double
into a byte stream.
d
- the value to encode.b
- the byte array in which to write the encoded value,
in ANSI/IEE 754 (1985) format, sign-bit first..offset
- the offset at which to start writing in the byte array.
NetByteEncodingException
- if the byte array is too small.public static double decodeDouble(byte[] b) throws NetByteDecodingException
double
value.
b
- the byte array in which contains the encoded value.
double
value formed from the byte array.
NetByteDecodingException
- if the byte array is too small.public static double decodeDouble(byte[] b, int offset) throws NetByteDecodingException
double
value.
b
- the byte array from which to read the encoded value.offset
- the offset at which to start reading from the byte array.
double
value formed from the byte array.
NetByteDecodingException
- if the byte array is too small.public static java.lang.String byteToHexString(byte b)
byte
value.
b
- the byte value.
public static java.lang.String bytesToHexString(byte[] buffer, int length, int offset)
byte
array.
buffer
- the byte array.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |