netbyte
Class NetByte

java.lang.Object
  |
  +--netbyte.NetByte

public class NetByte
extends java.lang.Object

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:

Some 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
Summary of primitive data type sizes


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

byteSize_

public static final int byteSize_
Size of a byte (1).

See Also:
Constant Field Values

shortSize_

public static final int shortSize_
Size of a short (2).

See Also:
Constant Field Values

charSize_

public static final int charSize_
Size of a char (2).

See Also:
Constant Field Values

intSize_

public static final int intSize_
Size of an int (4).

See Also:
Constant Field Values

longSize_

public static final int longSize_
Size of a long (8).

See Also:
Constant Field Values

floatSize_

public static final int floatSize_
Size of a float (4).

See Also:
Constant Field Values

doubleSize_

public static final int doubleSize_
Size of a doublne (8).

See Also:
Constant Field Values
Constructor Detail

NetByte

public NetByte()
Method Detail

encodeShort

public static byte[] encodeShort(short s)
Turns a short into a byte stream.

Parameters:
s - the value to encode.
Returns:
a byte array of size 2 with the value in network byte order, most-significant bit first.

encodeShort

public static void encodeShort(short s,
                               byte[] b,
                               int offset)
                        throws NetByteEncodingException
Turns a short into a byte stream.

Parameters:
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.
Throws:
NetByteEncodingException - if the byte array is too small.

decodeShort

public static short decodeShort(byte[] b)
                         throws NetByteDecodingException
Turns an encoding of 2 bytes into a short value.

Parameters:
b - the byte array in which contains the encoded value, with the value in network byte order, most-significant bit first.
Returns:
a short value formed from the byte array.
Throws:
NetByteDecodingException - if the byte array is too small.

decodeShort

public static short decodeShort(byte[] b,
                                int offset)
                         throws NetByteDecodingException
Turns an encoding of 2 bytes into a short value.

Parameters:
b - the byte array from which to read the encoded value.
offset - the offset at which to start reading from the byte array.
Returns:
a short value formed from the byte array.
Throws:
NetByteDecodingException - if the byte array is too small.

encodeChar

public static byte[] encodeChar(char c)
Turns a char into a byte stream.

Parameters:
c - the value to encode.
Returns:
a byte array of size 2 with the value in network byte order, most-significant bit first.

encodeChar

public static void encodeChar(char c,
                              byte[] b,
                              int offset)
                       throws NetByteEncodingException
Turns a char into a byte stream.

Parameters:
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.
Throws:
NetByteEncodingException - if the byte array is too small.

decodeChar

public static char decodeChar(byte[] b)
                       throws NetByteDecodingException
Turns an encoding of 2 bytes into a char value.

Parameters:
b - the byte array in which contains the encoded value.
Returns:
a char value formed from the byte array.
Throws:
NetByteDecodingException - if the byte array is too small.

decodeChar

public static char decodeChar(byte[] b,
                              int offset)
                       throws NetByteDecodingException
Turns an encoding of 2 bytes into a char value.

Parameters:
b - the byte array from which to read the encoded value.
offset - the offset at which to start reading from the byte array.
Returns:
a short value formed from the byte array.
Throws:
NetByteDecodingException - if the byte array is too small.

encodeInt

public static byte[] encodeInt(int i)
Turns an int into a byte stream.

Parameters:
i - the value to encode.
Returns:
a byte array of size 4 with the value in network byte order, most-significant bit first.

encodeInt

public static void encodeInt(int i,
                             byte[] b,
                             int offset)
                      throws NetByteEncodingException
Turns an int into a byte stream.

Parameters:
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.
Throws:
NetByteEncodingException - if the byte array is too small.

decodeInt

public static int decodeInt(byte[] b)
                     throws NetByteDecodingException
Turns an encoding of 4 bytes into an int value.

Parameters:
b - the byte array in which contains the encoded value.
Returns:
a int value formed from the byte array.
Throws:
NetByteDecodingException - if the byte array is too small.

decodeInt

public static int decodeInt(byte[] b,
                            int offset)
                     throws NetByteDecodingException
Turns an encoding of 4 bytes into an int value.

Parameters:
b - the byte array from which to read the encoded value.
offset - the offset at which to start reading from the byte array.
Returns:
a int value formed from the byte array.
Throws:
NetByteDecodingException - if the byte array is too small.

encodeLong

public static byte[] encodeLong(long l)
Turns a long into a byte stream.

Parameters:
l - the value to encode.
Returns:
a byte array of size 8 with the value in network byte order, most-significant bit first.

encodeLong

public static void encodeLong(long l,
                              byte[] b,
                              int offset)
                       throws NetByteEncodingException
Turns a long into a byte stream.

Parameters:
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.
Throws:
NetByteEncodingException - if the byte array is too small.

decodeLong

public static long decodeLong(byte[] b)
                       throws NetByteDecodingException
Turns an encoding of 8 bytes into a long value.

Parameters:
b - the byte array in which contains the encoded value.
Returns:
a long value formed from the byte array.
Throws:
NetByteDecodingException - if the byte array is too small.

decodeLong

public static long decodeLong(byte[] b,
                              int offset)
                       throws NetByteDecodingException
Turns an encoding of 8 bytes into a long value.

Parameters:
b - the byte array from which to read the encoded value.
offset - the offset at which to start reading from the byte array.
Returns:
a long value formed from the byte array.
Throws:
NetByteDecodingException - if the byte array is too small.

encodeFloat

public static byte[] encodeFloat(float f)
Turns a float into a byte stream.

Parameters:
f - the value to encode.
Returns:
a byte array of size 4 with the value, in ANSI/IEE 754 (1985) format, sign-bit first.

encodeFloat

public static void encodeFloat(float f,
                               byte[] b,
                               int offset)
                        throws NetByteEncodingException
Turns a float into a byte stream.

Parameters:
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.
Throws:
NetByteEncodingException - if the byte array is too small.

decodeFloat

public static float decodeFloat(byte[] b)
                         throws NetByteDecodingException
Turns an encoding of 4 bytes into a float value.

Parameters:
b - the byte array in which contains the encoded value.
Returns:
a float value formed from the byte array.
Throws:
NetByteDecodingException - if the byte array is too small.

decodeFloat

public static float decodeFloat(byte[] b,
                                int offset)
                         throws NetByteDecodingException
Turns an encoding of 4 bytes into a float value.

Parameters:
b - the byte array from which to read the encoded value.
offset - the offset at which to start reading from the byte array.
Returns:
a float value formed from the byte array.
Throws:
NetByteDecodingException - if the byte array is too small.

encodeDouble

public static byte[] encodeDouble(double d)
Turns a double into a byte stream.

Parameters:
d - the value to encode.
Returns:
a byte array of size 8 with the value, in ANSI/IEE 754 (1985) format, sign-bit first.

encodeDouble

public static void encodeDouble(double d,
                                byte[] b,
                                int offset)
                         throws NetByteEncodingException
Turns a double into a byte stream.

Parameters:
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.
Throws:
NetByteEncodingException - if the byte array is too small.

decodeDouble

public static double decodeDouble(byte[] b)
                           throws NetByteDecodingException
Turns an encoding of 8 bytes into a double value.

Parameters:
b - the byte array in which contains the encoded value.
Returns:
a double value formed from the byte array.
Throws:
NetByteDecodingException - if the byte array is too small.

decodeDouble

public static double decodeDouble(byte[] b,
                                  int offset)
                           throws NetByteDecodingException
Turns an encoding of 8 bytes into a double value.

Parameters:
b - the byte array from which to read the encoded value.
offset - the offset at which to start reading from the byte array.
Returns:
a double value formed from the byte array.
Throws:
NetByteDecodingException - if the byte array is too small.

byteToHexString

public static java.lang.String byteToHexString(byte b)
Gives the hexadecimal representation of a byte value.

Parameters:
b - the byte value.
Returns:
a String object containing the hexadecimal representation.

bytesToHexString

public static java.lang.String bytesToHexString(byte[] buffer,
                                                int length,
                                                int offset)
Gives the hexadecimal representation of the values in a byte array.

Parameters:
buffer - the byte array.
Returns:
a String object containing the hexadecimal representation.

Note: if the length or offset arguments would result in accessing past the end of the byte array, the returned String object will contain representations of the byte values up to the end of the byte array only, and no error will be generated.