org.mobicents.protocols.smpp.util
Class SMPPIO

java.lang.Object
  extended by org.mobicents.protocols.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 String readCString(byte[] bytes, int offset)
          Read a C-String (a string terminated by a nul-byte) from the byte array.
static int readInt4(byte[] b, int offset)
          Read a 4-byte integer from a byte array.
static long readInt8(byte[] b, int offset)
          Read an 8-byte integer from a byte array.
static String readString(byte[] bytes, int offset, int length)
          Read a fixed-length String from bytes.
static int readUInt1(byte[] b, int offset)
          Read an unsigned 1-byte integer from a byte array.
static int readUInt2(byte[] b, int offset)
          Read a 2-byte unsigned integer from a byte array.
static long readUInt4(byte[] b, int offset)
          Read a 4-byte unsigned integer from a byte array, returning a long.
static void writeByte(int b, OutputStream out)
          Write a byte value to the output stream;
static void writeCString(String s, OutputStream out)
          Write a String to an OutputStream followed by a NUL byte.
static void writeInt(int value, OutputStream out)
          Write a 4-byte integer value to the output stream.
static void writeLong(long value, OutputStream out)
          Write an 8-byte integer value to the output stream.
static void writeLongInt(long value, OutputStream out)
          Write a 4-byte integer value to the output stream.
static void writeShort(int value, OutputStream out)
          Write a 2-byte integer value to the output stream.
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

readCString

public static String readCString(byte[] bytes,
                                 int offset)
Read a C-String (a string terminated by a nul-byte) from the byte array. The bytes will be interpreted as US-ASCII characters.

Parameters:
bytes - The bytes to decode the string from.
offset - The offset in bytes to begin reading the string from.
Returns:
The decoded string.
Throws:
ArrayIndexOutOfBoundsException - if no nul byte is detected in the array.

readString

public static String readString(byte[] bytes,
                                int offset,
                                int length)
Read a fixed-length String from bytes. The bytes will be interpreted as US-ASCII characters.

Parameters:
bytes - The bytes to decode the string from.
offset - The offset in the array to begin parsing from.
length - The number of bytes to parse for the string.
Returns:
The decoded String.
Throws:
ArrayIndexOutOfBoundsException - if there are insufficient bytes to parse the string.

readUInt1

public static int readUInt1(byte[] b,
                            int offset)
Read an unsigned 1-byte integer from a byte array.

Parameters:
b - The byte array to read the integer from.
offset - The offset in the array to read the integer from.
Returns:
The decoded integer value.
Throws:
ArrayIndexOutOfBoundsException - If there are not enough bytes in the array.

readUInt2

public static int readUInt2(byte[] b,
                            int offset)
Read a 2-byte unsigned integer from a byte array.

Parameters:
b - The byte array to read the integer from.
offset - The offset at which the (most significant) first byte of the integer resides.
Returns:
The decoded integer value.
Throws:
ArrayIndexOutOfBoundsException - If there are not enough bytes in the array.

readInt4

public static int readInt4(byte[] b,
                           int offset)
Read a 4-byte integer from a byte array. Warning: SMPP integers are unsigned, whereas Java integers are signed. This method will take the 4 bytes as they appear in the array and assemble them into a Java int. If the unsigned value exceeds Integer.MAX_VALUE , then the Java value will actually be a negative number.

If you are more interested in the actual value, then use the readUInt4(byte[], int) method, which will return the real value in a Java long primitive.

Parameters:
b - The byte array to read the integer from.
offset - The offset at which the (most significant) first byte of the integer resides.
Returns:
The decoded integer value.
Throws:
ArrayIndexOutOfBoundsException - If there are not enough bytes in the array.

readUInt4

public static long readUInt4(byte[] b,
                             int offset)
Read a 4-byte unsigned integer from a byte array, returning a long. This method is provided as SMPP integers are unsigned, so a long primitive is needed in Java to support the full range of unsigned values.

Parameters:
b - The byte array to read the integer from.
offset - The offset at which the (most significant) first byte of the integer resides.
Returns:
The decoded integer value.
Throws:
ArrayIndexOutOfBoundsException - If there are not enough bytes in the array.

readInt8

public static long readInt8(byte[] b,
                            int offset)
Read an 8-byte integer from a byte array. Care should be taken as values read from the wire that exceed Long.MAX_VALUE will be interpreted by Java as negative numbers.

Parameters:
b - The byte array to read the integer from.
offset - The offset at which the (most significant) first byte of the int resides.
Returns:
The decoded integer value.
Throws:
ArrayIndexOutOfBoundsException - If there are not enough bytes in the array.

writeByte

public static void writeByte(int b,
                             OutputStream out)
                      throws IOException
Write a byte value to the output stream;

Parameters:
b - The value to write.
out - The output stream to write to.
Throws:
IOException - If an error occurs writing to the stream.

writeShort

public static void writeShort(int value,
                              OutputStream out)
                       throws IOException
Write a 2-byte integer value to the output stream.

Parameters:
value - The value to write.
out - The output stream to write to.
Throws:
IOException - If an error occurs writing to the stream.

writeInt

public static void writeInt(int value,
                            OutputStream out)
                     throws IOException
Write a 4-byte integer value to the output stream.

Parameters:
value - The value to write.
out - The output stream to write to.
Throws:
IOException - If an error occurs writing to the stream.

writeLongInt

public static void writeLongInt(long value,
                                OutputStream out)
                         throws IOException
Write a 4-byte integer value to the output stream. This method is provided as SMPP integers are unsigned, so we need a long to contain one in Java.

Parameters:
value - The value to write.
out - The output stream to write to.
Throws:
IOException - If an error occurs writing to the stream.

writeLong

public static void writeLong(long value,
                             OutputStream out)
                      throws IOException
Write an 8-byte integer value to the output stream.

Parameters:
value - The value to write.
out - The output stream to write to.
Throws:
IOException - If an error occurs writing to the stream.

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 number of bytes to write. If len is greater than the number of characters in the string, an exception will be thrown.
out - The OutputStream to write to.
Throws:
IOException - If an I/O error occurs
ArrayIndexOutOfBoundsException - If there are not enough characters to satisfy the len parameter.

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 Mobicents. All Rights Reserved.