ie.omk.smpp.util
Class SMPPIO

java.lang.Object
  extended by ie.omk.smpp.util.SMPPIO

public final class SMPPIO
extends Object

Class that provides input and output methods for writing Java types encoded as SMPP types. This class cannot be instantiated...all it's methods are static.


Method Summary
static int bytesToInt(byte[] b, int offset, int size)
          Convert a byte array (or part thereof) into an integer.
static long bytesToLong(byte[] b, int offset, int size)
          Convert a byte array (or part thereof) into a long.
static byte[] intToBytes(int num, int len)
          Convert an integer to a byte array in MSB first order
static byte[] intToBytes(int num, int len, byte[] array, int offset)
          Convert an integer to a byte array in MSB first order.
static byte[] longToBytes(long num, int len)
          Convert a long to a byte array in MSB first order.
static byte[] longToBytes(long num, int len, byte[] b, int offset)
          Convert a long to a byte array in MSB first order.
static String readCString(byte[] b, int offset)
          Read a nul-terminated ASCII string from a byte array.
static String readCString(InputStream in)
          Read in a NUL-terminated string from an InputStream
static int readInt(InputStream in, int len)
          Read an Integer from an InputStream.
static String readString(byte[] b, int offset, int len)
          Read an ASCII string from a byte array.
static String readString(InputStream in, int len)
          Read in a string of specified length from an InputStream.
static void writeCString(String s, OutputStream out)
          Write a String to an OutputStream followed by a NUL byte.
static void writeInt(int x, int len, OutputStream out)
          Write the byte representation of an integer to an OutputStream in MSB order.
static void writeString(String s, int len, OutputStream out)
          Write a String of specified length to an OutputStream.
static void writeString(String s, OutputStream out)
          Write a String in it's entirety to an OutputStream
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

readInt

public static int readInt(InputStream in,
                          int len)
                   throws IOException
Read an Integer from an InputStream. The integer read from the stream is assumed to be in network byte order (ie Big Endian).

Parameters:
in - The InputStream to read from
len - The number of bytes to form the integer from (usually either 1 or 4, limited to 1 <= len <= 8)
Returns:
An integer representation of the len bytes read from in.
Throws:
IOException - If EOS is reached before len bytes are read.
See Also:
InputStream

readCString

public static String readCString(InputStream in)
                          throws IOException
Read in a NUL-terminated string from an InputStream

Parameters:
in - The InputStream to read from
Returns:
A String representation with the NUL byte removed.
Throws:
IOException - If EOS is reached before a NUL byte
See Also:
InputStream

readCString

public static String readCString(byte[] b,
                                 int offset)
Read a nul-terminated ASCII string from a byte array.

Returns:
A java String object representing the read string without the terminating nul.

readString

public static String readString(InputStream in,
                                int len)
                         throws IOException
Read in a string of specified length from an InputStream. The String may contain NUL bytes.

Parameters:
in - The InputStream to read from
len - The number of bytes to read in from the InputStream
Returns:
A String of length len. null if len is less than 1.
Throws:
IOException - If EOS is reached before a NUL byte
See Also:
InputStream

readString

public static String readString(byte[] b,
                                int offset,
                                int len)
Read an ASCII string from a byte array.

Parameters:
b - The byte array to read from.
offset - The offset into b to begin reading from.
len - The length of the string to read.
Returns:
A string decoded from b of length len. ASCII is used to convert the bytes into characters.

intToBytes

public static byte[] intToBytes(int num,
                                int len)
Convert an integer to a byte array in MSB first order

Parameters:
num - The number to store
len - The length of the integer to convert
Returns:
An array of length len containing the byte representation of num.

intToBytes

public static byte[] intToBytes(int num,
                                int len,
                                byte[] array,
                                int offset)
Convert an integer to a byte array in MSB first order. This method exists as well as the longToBytes method for performance reasons. More often than not, a 4-byte value is the largest being converted...doing that using ints instead of longs will offer a slight performance increase. The code for the two methods is identical except for using ints instead of longs to hold mask, shiftwidth and number values.

Parameters:
num - The number to store
len - The length of the integer to convert (that is, the number of bytes to generate).
array - the byte array to write the integer to.
offset - the offset in b to write the integer to.
Returns:
An array of length len containing the byte representation of num.

longToBytes

public static byte[] longToBytes(long num,
                                 int len)
Convert a long to a byte array in MSB first order.

Parameters:
num - The number to store
len - The length of the integer to convert (that is, the number of bytes to generate).
Returns:
An array of length len containing the byte representation of num.

longToBytes

public static byte[] longToBytes(long num,
                                 int len,
                                 byte[] b,
                                 int offset)
Convert a long to a byte array in MSB first order.

Parameters:
num - The number to store
len - The length of the integer to convert (that is, the number of bytes to generate).
b - the byte array to write the integer to.
offset - the offset in b to write the integer to.
Returns:
An array of length len containing the byte representation of num.

bytesToInt

public static int bytesToInt(byte[] b,
                             int offset,
                             int size)
Convert a byte array (or part thereof) into an integer. The byte array should be in big-endian form. That is, the byte at index 'offset' should be the MSB.

Parameters:
b - The array containing the bytes
offset - The array index of the MSB
size - The number of bytes to convert into the integer
Returns:
An integer value represented by the specified bytes.

bytesToLong

public static long bytesToLong(byte[] b,
                               int offset,
                               int size)
Convert a byte array (or part thereof) into a long. The byte array should be in big-endian form. That is, the byte at index 'offset' should be the MSB.

Parameters:
b - The array containing the bytes
offset - The array index of the MSB
size - The number of bytes to convert into the long
Returns:
An long value represented by the specified bytes.

writeInt

public static void writeInt(int x,
                            int len,
                            OutputStream out)
                     throws IOException
Write the byte representation of an integer to an OutputStream in MSB order.

Parameters:
x - The integer to write
len - The number of bytes in this integer (usually either 1 or 4)
out - The OutputStream to write the integer to
Throws:
IOException - If an I/O error occurs.
See Also:
OutputStream

writeCString

public static void writeCString(String s,
                                OutputStream out)
                         throws IOException
Write a String to an OutputStream followed by a NUL byte.

Parameters:
s - The string to write
out - The output stream to write to
Throws:
IOException - If an I/O error occurs
See Also:
OutputStream

writeString

public static void writeString(String s,
                               int len,
                               OutputStream out)
                        throws IOException
Write a String of specified length to an OutputStream.

Parameters:
s - The String to write
len - The length of the String to write. If this is longer than the length of the String, the whole String will be sent.
out - The OutputStream to use
Throws:
IOException - If an I/O error occurs
See Also:
OutputStream

writeString

public static void writeString(String s,
                               OutputStream out)
                        throws IOException
Write a String in it's entirety to an OutputStream

Parameters:
s - The String to write
out - The OutputStream to write to
Throws:
IOException - If an I/O error occurs
See Also:
OutputStream


Copyright © 2011 smppapi at Sourceforge. All Rights Reserved.