org.opengts.util
Class Payload

java.lang.Object
  extended by org.opengts.util.Payload

public class Payload
extends java.lang.Object

For reading/writing binary fields


Field Summary
static int DEFAULT_MAX_PAYLOAD_LENGTH
           
static byte[] EMPTY_BYTE_ARRAY
           
 
Constructor Summary
  Payload()
          Destination Constructor
  Payload(boolean bigEndian)
          Destination Constructor
  Payload(byte[] b)
          Source Constuctor
  Payload(byte[] b, boolean bigEndian)
          Source Constuctor
  Payload(byte[] b, int ofs, int len)
          Source Constuctor
  Payload(byte[] n, int ofs, int len, boolean bigEndian)
          Source Constuctor
  Payload(int maxPayloadLen)
          Destination Constructor
  Payload(int maxPayloadLen, boolean bigEndian)
          Destination Constructor
protected Payload(int frame, byte[] n, int ofs, int len, boolean bigEndian)
          Source Constuctor
 
Method Summary
protected  byte[] _readBytes(int length, java.lang.String msg, int frame)
          Read length
protected  double _readDouble(int length, double dft, boolean bigEndian, int frame, java.lang.String msg)
          Read a double value from payload (with default), using IEEE 754 format
protected  long _readLong(int length, long dft, boolean bigEndian, int frame, java.lang.String msg)
          Read a long value from payload (with default)
protected  void _readSkip(int length, int frame, java.lang.String msg)
          Skip a specified number of bytes
protected  java.lang.String _readString(int length, boolean varLength, int frame, java.lang.String msg)
          Read a string from the payload.
protected  java.lang.String _readStringHex(int length, int frame, java.lang.String msg)
          Reads a fixed length hex string from the payload bytes.
protected  long _readULong(int length, long dft, boolean bigEndian, int frame, java.lang.String msg)
          Read an unsigned long value from payload (with default)
protected  int _writeBytes(byte[] n, int nOfs, int nLen, int wrtLen, int frame)
          Write an array of bytes to the payload
protected  int _writeDouble(double val, int wrtLen, boolean bigEndian, int frame)
          Write a double value to the payload
protected  int _writeLong(long val, int wrtLen, boolean bigEndian, int frame)
          Write a long value to the payload
protected  int _writeZeroFill(int wrtLen, int frame)
          Write a zero fill to the payload
 long calcChecksum(Checksum.CRC crc, int bOfs, int bLen)
          Calculate/Return CCITT CRC for specified offset/length
 int calcCrc16(int bOfs, int bLen)
          Calculate/Return standard CRC-16 (0x0000) for specified offset/length
 int calcCrc16Modbus(int bOfs, int bLen)
          Calculate/Return CRC-16 (0xFFFF Modbus variant) for specified offset/length
 int calcCrcCCITT(int bOfs, int bLen)
          Calculate/Return standard CCITT CRC (0xFFFF) for specified offset/length
 int calcCrcSum8(int bOfs, int bLen)
          Calculate/Return byte Sum8 CRC for specified offset/length
 int calcCrcXmodem(int bOfs, int bLen)
          Calculate/Return CCITT CRC (0x0000 Xmodem variant) for specified offset/length
protected static void checkOverflow(int length, int maxLen, int frame, java.lang.String msg)
          Check for read overflow
 void clear()
          Resets the payload to an empty state
static double decodeDouble(byte[] data, int ofs, int len, boolean bigEndian, double dft)
          Decodes a double value from bytes, using IEEE 754 format
static long decodeLong(byte[] data, int ofs, int len, boolean bigEndian, boolean signed, long dft)
          Decodes a long value from bytes
static int encodeDouble(byte[] data, int ofs, int len, boolean bigEndian, double val)
          Encodes a double value into bytes
static int encodeLong(byte[] data, int ofs, int len, boolean bigEndian, long val)
          Encodes a long value into bytes
static byte[] encodeLong(long val, int len, boolean bigEndian)
          Encodes a long value into bytes
static long extendSignBit(long val, int bitLen)
          Extends the sign-bit into a long value
 int getAvailableReadLength()
          Gets the number of remaining available to read
 int getAvailableWriteLength()
          Gets the nubmer of remaining availible bytes to write
 boolean getBigEndian()
          Gets the byte ordering of the payload
 byte[] getBytes()
          Return a byte array representing the data currently in the payload (may be a copy)
static boolean GetDebugLogging()
          Gets internal debug logging
 int getIndex()
          Gets the current read/write index
 int getMaximumReadLength()
          Return the maximum allowed read length (same as "getAvailableReadLength")
 int getMaximumReadLength(int length)
          Return the maximum allowed read length up to the specific length
 int getMaximumWriteLength(int wrtLen)
          Return the maximum allowed write length up to the specific length
 int getSize()
          For an output/write Payload, returns the number of bytes written.
 boolean hasAvailableRead()
          Returns true if there are bytes available for reading
 boolean hasAvailableWrite()
          Returns true if there are bytes available for writing
 boolean isBigEndian()
          Returns true if the payload is big-endian
 boolean isValidBCD(int length)
          Returns true if the length fields contains only BCD nybbles
 boolean isValidReadLength(int length)
          Returns true if there are at least length bytes that can be read from the payload
 boolean isValidWriteLength(int length)
          Returns true if there are at least length bytes that can be written to the payload
static void main(java.lang.String[] argv)
          Main entry point for testing/debugging
 int peekByte()
          Read the next byte without moving the read pointer
 byte[] peekBytes(int length)
          Reads the specified number of bytes from the stream The read pointer is left as is
 byte[] peekBytes(int offset, int length)
          Reads the specified number of bytes from the stream The read pointer is left as is
 byte[] readBytes(int length)
          Read length
 byte[] readBytes(int length, java.lang.String msg)
          Read length
 double readDouble(int length, double dft)
          Read a double value from payload (with default), using IEEE 754 format
 double readDouble(int length, double dft, boolean bigEndian)
          Read a double value from payload (with default), using IEEE 754 format
 double readDouble(int length, double dft, boolean bigEndian, java.lang.String msg)
          Read a double value from payload (with default), using IEEE 754 format
 double readDouble(int length, double dft, java.lang.String msg)
          Read a double value from payload (with default), using IEEE 754 format
 GeoPoint readGPS(int length)
          Reads an encoded GPS point (latitude,longitude) from the payload
 GeoPoint readGPS(int length, java.lang.String msg)
          Reads an encoded GPS point (latitude,longitude) from the payload
 int readInt(int length, int dft)
          Read a int value from payload (with default)
 int readInt(int length, int dft, java.lang.String msg)
          Read a int value from payload (with default)
 int readIntBCD(int length, int dft)
          Read an unsigned BCD (hex) encoded int value from the payload.
 int readIntBCD(int length, int dft, java.lang.String msg)
          Read an unsigned BCD (hex) encoded int value from the payload.
 long readLong(int length, long dft)
          Read a long value from payload (with default)
 long readLong(int length, long dft, boolean bigEndian)
          Read a long value from payload (with default)
 long readLong(int length, long dft, boolean bigEndian, java.lang.String msg)
          Read a long value from payload (with default)
 long readLong(int length, long dft, java.lang.String msg)
          Read a long value from payload (with default)
 long readLongBCD(int length, long dft)
          Read an unsigned BCD (hex) encoded long value from the payload.
 long readLongBCD(int length, long dft, java.lang.String msg)
          Read an unsigned BCD (hex) encoded long value from the payload.
 void readSkip(int length)
          Skip a specified number of bytes
 void readSkip(int length, java.lang.String msg)
          Skip a specified number of bytes
 java.lang.String readString(int length)
          Reads a variable length string from the payload
 java.lang.String readString(int length, boolean varLength)
          Read a string from the payload.
 java.lang.String readString(int length, boolean varLength, java.lang.String msg)
          Read a string from the payload.
 java.lang.String readString(int length, java.lang.String msg)
          Reads a variable length string from the payload
 java.lang.String readStringHex(int length)
          Reads a fixed length hex string from the payload bytes.
 java.lang.String readStringHex(int length, java.lang.String msg)
          Reads a fixed length hex string from the payload bytes.
 int readUInt(int length, int dft)
          Read an unsigned int value from payload (with default)
 int readUInt(int length, int dft, boolean bigEndian)
          Read an unsigned int value from payload (with default)
 int readUInt(int length, int dft, boolean bigEndian, java.lang.String msg)
          Read an unsigned int value from payload (with default)
 int readUInt(int length, int dft, java.lang.String msg)
          Read an unsigned int value from payload (with default)
 long readULong(int length, long dft)
          Read an unsigned long value from payload (with default)
 long readULong(int length, long dft, boolean bigEndian)
          Read an unsigned long value from payload (with default)
 long readULong(int length, long dft, boolean bigEndian, java.lang.String msg)
          Read an unsigned long value from payload (with default)
 long readULong(int length, long dft, java.lang.String msg)
          Read an unsigned long value from payload (with default)
 void resetIndex()
          Resets the read/write index to '0'
 void resetIndex(int ndx)
          Resets the read/write index to the specified value
 boolean restoreIndex()
          Restores the current index (restore to marker).
static long reverseByteOrder(long value, int byteCount)
          Reverses the byte order of the specified value.
IE: Convert Bit-Endian to Little-Endian, and visa-versa.
 boolean saveIndex()
          Saves the current index (set marker).
 int scanForPattern(byte[] p)
          Scans the remainder of the payload for the specified byte pattern and returns the offset from the current pointer to the beginning of the matching pattern.
 int scanForPattern(byte[] p, int fromNdx)
          Scans the remainder of the payload for the specified byte pattern and returns the offset from the current pointer to the beginning of the matching pattern.
 void setBigEndian()
          Sets the byte ordering of the payload to "Big-Endian"
Also called "Network Byte Order", "MSB first", ...
 void setBigEndian(boolean bigEndFirst)
          Sets the byte ordering of the payload
static void SetDebugLogging(boolean debug)
          Sets internal debug logging
 void setLittleEndian()
          Sets the byte ordering of the payload to "Little-Endian"
Also called "LSB first", ...
 java.lang.String toString()
          Returns a hex string representation of the payload
 int writeByte(byte val)
          Write a byte value to the payload
 int writeBytes(byte[] n)
          Write an array of bytes to the payload
 int writeBytes(byte[] n, int wrtLen)
          Write an array of bytes to the payload
 int writeBytes(byte[] n, int nOfs, int nLen, int wrtLen)
          Write an array of bytes to the payload
 int writeDouble(double val, int wrtLen)
          Write a double value to the payload
 int writeDouble(double val, int wrtLen, boolean bigEndian)
          Write a double value to the payload
 int writeGPS(double lat, double lon, int length)
          Encode a new GPS point into the payload
 int writeGPS(GeoPoint gp, int wrtLen)
          Encode a GPS point into the payload
 int writeInt(int val, int length)
          Write a int value to the payload
 int writeLong(long val, int length)
          Write a long value to the payload
 int writeLong(long val, int length, boolean bigEndian)
          Write a long value to the payload
 int writeRandomBytes(java.util.Random rand, int wrtLen)
          Write random bytes to the payload
 int writeString(java.lang.String s, int wrtLen)
          Write a string to the payload.
 int writeString(java.lang.String s, int wrtLen, boolean varLength)
          Write a string to the payload.
 int writeUInt(int val, int length)
          Write a unsigned int value to the payload.
 int writeULong(long val, int length)
          Write a unsigned long value to the payload.
 int writeULong(long val, int length, boolean bigEndian)
          Write a unsigned long value to the payload.
 int writeZeroFill(int wrtLen)
          Write a zero fill to the payload
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_MAX_PAYLOAD_LENGTH

public static final int DEFAULT_MAX_PAYLOAD_LENGTH
See Also:
Constant Field Values

EMPTY_BYTE_ARRAY

public static final byte[] EMPTY_BYTE_ARRAY
Constructor Detail

Payload

public Payload()
Destination Constructor


Payload

public Payload(int maxPayloadLen)
Destination Constructor

Parameters:
maxPayloadLen - The maximum payload length

Payload

public Payload(boolean bigEndian)
Destination Constructor


Payload

public Payload(int maxPayloadLen,
               boolean bigEndian)
Destination Constructor

Parameters:
maxPayloadLen - The maximum payload length
bigEndian - If the payload uses big-endian byte ordering

Payload

public Payload(byte[] b)
Source Constuctor

Parameters:
b - The payload (default big-endian byte ordering)

Payload

public Payload(byte[] b,
               boolean bigEndian)
Source Constuctor

Parameters:
b - The payload
bigEndian - If the payload uses big-endian byte ordering

Payload

public Payload(byte[] b,
               int ofs,
               int len)
Source Constuctor

Parameters:
b - The byte array to copy the payload from (defalt big-endian byte ordering
ofs - The offset at which copying of b should begin
len - The length of the resultant payload

Payload

public Payload(byte[] n,
               int ofs,
               int len,
               boolean bigEndian)
Source Constuctor

Parameters:
n - The byte array to copy the payload from (defalt big-endian byte ordering
ofs - The offset at which copying of b should begin
len - The length of the resultant payload
bigEndian - If the payload uses big-endian byte ordering

Payload

protected Payload(int frame,
                  byte[] n,
                  int ofs,
                  int len,
                  boolean bigEndian)
Source Constuctor

Parameters:
frame - The caller stackfram
n - The byte array to copy the payload from (defalt big-endian byte ordering
ofs - The offset at which copying of b should begin
len - The length of the resultant payload
bigEndian - If the payload uses big-endian byte ordering
Method Detail

SetDebugLogging

public static void SetDebugLogging(boolean debug)
Sets internal debug logging


GetDebugLogging

public static boolean GetDebugLogging()
Gets internal debug logging


checkOverflow

protected static void checkOverflow(int length,
                                    int maxLen,
                                    int frame,
                                    java.lang.String msg)
Check for read overflow


setBigEndian

public void setBigEndian()
Sets the byte ordering of the payload to "Big-Endian"
Also called "Network Byte Order", "MSB first", ... ("Big-Endian" is the default format)


setLittleEndian

public void setLittleEndian()
Sets the byte ordering of the payload to "Little-Endian"
Also called "LSB first", ...


setBigEndian

public void setBigEndian(boolean bigEndFirst)
Sets the byte ordering of the payload

Parameters:
bigEndFirst - True for big-endian, false for little-endian numeric encoding

getBigEndian

public boolean getBigEndian()
Gets the byte ordering of the payload

Returns:
True if big-endian, false if little-endian

isBigEndian

public boolean isBigEndian()
Returns true if the payload is big-endian

Returns:
True if big-endian, false if little-endian

getSize

public int getSize()
For an output/write Payload, returns the number of bytes written. For an input/read Payload, return the total number of bytes contained in this Payload.

Returns:
The current size of the payload

clear

public void clear()
Resets the payload to an empty state


getBytes

public byte[] getBytes()
Return a byte array representing the data currently in the payload (may be a copy)

Returns:
The byte array currently in the payload (as-is)

calcChecksum

public long calcChecksum(Checksum.CRC crc,
                         int bOfs,
                         int bLen)
Calculate/Return CCITT CRC for specified offset/length

Parameters:
crc - The CRC algorithm to use
bOfs - The offset
bLen - The length
Returns:
The calculated CRC

calcCrcCCITT

public int calcCrcCCITT(int bOfs,
                        int bLen)
Calculate/Return standard CCITT CRC (0xFFFF) for specified offset/length

Parameters:
bOfs - The offset
bLen - The length
Returns:
The calculated CCITT CRC

calcCrcXmodem

public int calcCrcXmodem(int bOfs,
                         int bLen)
Calculate/Return CCITT CRC (0x0000 Xmodem variant) for specified offset/length

Parameters:
bOfs - The offset
bLen - The length
Returns:
The calculated CCITT:Xmodem CRC

calcCrc16

public int calcCrc16(int bOfs,
                     int bLen)
Calculate/Return standard CRC-16 (0x0000) for specified offset/length

Parameters:
bOfs - The offset
bLen - The length
Returns:
The calculated CRC-16

calcCrc16Modbus

public int calcCrc16Modbus(int bOfs,
                           int bLen)
Calculate/Return CRC-16 (0xFFFF Modbus variant) for specified offset/length

Parameters:
bOfs - The offset
bLen - The length
Returns:
The calculated Modbus CRC-16

calcCrcSum8

public int calcCrcSum8(int bOfs,
                       int bLen)
Calculate/Return byte Sum8 CRC for specified offset/length

Parameters:
bOfs - The offset
bLen - The length
Returns:
The calculated Sum8 CRC

getIndex

public int getIndex()
Gets the current read/write index

Returns:
The index

resetIndex

public void resetIndex()
Resets the read/write index to '0'


resetIndex

public void resetIndex(int ndx)
Resets the read/write index to the specified value

Parameters:
ndx - The value to set the index

saveIndex

public boolean saveIndex()
Saves the current index (set marker).

Returns:
True if the operation was successful

restoreIndex

public boolean restoreIndex()
Restores the current index (restore to marker).

Returns:
True if the operation was successful

getMaximumReadLength

public int getMaximumReadLength(int length)
Return the maximum allowed read length up to the specific length

Parameters:
length - The preferred length
Returns:
The maximum length

getMaximumReadLength

public int getMaximumReadLength()
Return the maximum allowed read length (same as "getAvailableReadLength")

Returns:
The maximum length
See Also:
getAvailableReadLength()

getAvailableReadLength

public int getAvailableReadLength()
Gets the number of remaining available to read

Returns:
The number of availible bytes to read

isValidReadLength

public boolean isValidReadLength(int length)
Returns true if there are at least length bytes that can be read from the payload

Returns:
True if there at least length readable bytes in the payload

hasAvailableRead

public boolean hasAvailableRead()
Returns true if there are bytes available for reading

Returns:
True if there are bytes availible for reading

getMaximumWriteLength

public int getMaximumWriteLength(int wrtLen)
Return the maximum allowed write length up to the specific length

Parameters:
wrtLen - The preferred length
Returns:
The maximum length

getAvailableWriteLength

public int getAvailableWriteLength()
Gets the nubmer of remaining availible bytes to write

Returns:
The remaining available bytes to write

isValidWriteLength

public boolean isValidWriteLength(int length)
Returns true if there are at least length bytes that can be written to the payload

Returns:
True if there at least length writeable bytes in the payload

hasAvailableWrite

public boolean hasAvailableWrite()
Returns true if there are bytes available for writing


readSkip

public void readSkip(int length)
Skip a specified number of bytes

Parameters:
length - The number of bytes to skip

readSkip

public void readSkip(int length,
                     java.lang.String msg)
Skip a specified number of bytes

Parameters:
length - The number of bytes to skip
msg - Debug message (used during "DebugLogging")

_readSkip

protected void _readSkip(int length,
                         int frame,
                         java.lang.String msg)
Skip a specified number of bytes

Parameters:
length - The number of bytes to skip
msg - Debug message (used during "DebugLogging")

peekByte

public int peekByte()
Read the next byte without moving the read pointer

Returns:
The next byte (-1 if no bytes are available)

peekBytes

public byte[] peekBytes(int length)
Reads the specified number of bytes from the stream The read pointer is left as is

Parameters:
length - The number of bytes to read
Returns:
The byte array

peekBytes

public byte[] peekBytes(int offset,
                        int length)
Reads the specified number of bytes from the stream The read pointer is left as is

Parameters:
offset - The offset from the current index
length - The number of bytes to read
Returns:
The byte array

scanForPattern

public int scanForPattern(byte[] p)
Scans the remainder of the payload for the specified byte pattern and returns the offset from the current pointer to the beginning of the matching pattern. Returns -1 if the pattern was not found.

Parameters:
p - The byte pattern array
Returns:
The byte offset, or -1 if not found

scanForPattern

public int scanForPattern(byte[] p,
                          int fromNdx)
Scans the remainder of the payload for the specified byte pattern and returns the offset from the current pointer to the beginning of the matching pattern. Returns -1 if the pattern was not found.

Parameters:
p - The byte pattern array
fromNdx - The the starting index relative to the current payload index
Returns:
The byte offset, or -1 if not found

readBytes

public byte[] readBytes(int length)
Read length
Parameters:
length - The number fo bytes to read from the payload
Returns:
The read byte array

readBytes

public byte[] readBytes(int length,
                        java.lang.String msg)
Read length
Parameters:
length - The number fo bytes to read from the payload
msg - Debug message (used during "DebugLogging")
Returns:
The read byte array

_readBytes

protected byte[] _readBytes(int length,
                            java.lang.String msg,
                            int frame)
Read length
Parameters:
length - The number fo bytes to read from the payload
msg - Debug message (used during "DebugLogging")
Returns:
The read byte array

extendSignBit

public static long extendSignBit(long val,
                                 int bitLen)
Extends the sign-bit into a long value


reverseByteOrder

public static long reverseByteOrder(long value,
                                    int byteCount)
Reverses the byte order of the specified value.
IE: Convert Bit-Endian to Little-Endian, and visa-versa.

Parameters:
value - The Long value to reverse
byteCount - The number of bytes to reverse
Returns:
The reversed value

decodeLong

public static long decodeLong(byte[] data,
                              int ofs,
                              int len,
                              boolean bigEndian,
                              boolean signed,
                              long dft)
Decodes a long value from bytes

Parameters:
data - The byte array to decode the value from
ofs - The offset into data to start decoding from
len - The number of bytes to decode into the long value
bigEndian - True if the bytes are Big-Endian ordered, false if Little-Endian
signed - If the encoded bytes represent a signed value
dft - The default value if a value cannot be decoded
Returns:
The decoded value, or the default

_readLong

protected long _readLong(int length,
                         long dft,
                         boolean bigEndian,
                         int frame,
                         java.lang.String msg)
Read a long value from payload (with default)

Parameters:
length - The number of bytes to decode the value from
dft - The default value if a value could not be decoded
bigEndian - True to read bytes in big-endian order, otherwise little-endian
frame - The current stackframe index
msg - Debug message (used during "DebugLogging")
Returns:
The decoded value, or the default value

readLong

public long readLong(int length,
                     long dft,
                     boolean bigEndian)
Read a long value from payload (with default)

Parameters:
length - The number of bytes to decode the value from
dft - The default value if a value could not be decoded
bigEndian - True to read bytes in big-endian order, otherwise little-endian
Returns:
The decoded value, or the default value

readLong

public long readLong(int length,
                     long dft,
                     boolean bigEndian,
                     java.lang.String msg)
Read a long value from payload (with default)

Parameters:
length - The number of bytes to decode the value from
dft - The default value if a value could not be decoded
bigEndian - True to read bytes in big-endian order, otherwise little-endian
msg - Debug message (used during "DebugLogging")
Returns:
The decoded value, or the default value

readLong

public long readLong(int length,
                     long dft)
Read a long value from payload (with default)

Parameters:
length - The number of bytes to decode the value from
dft - The default value if a value could not be decoded
Returns:
The decoded value, or the default value

readLong

public long readLong(int length,
                     long dft,
                     java.lang.String msg)
Read a long value from payload (with default)

Parameters:
length - The number of bytes to decode the value from
dft - The default value if a value could not be decoded
msg - Debug message (used during "DebugLogging")
Returns:
The decoded value, or the default value

readInt

public int readInt(int length,
                   int dft)
Read a int value from payload (with default)

Parameters:
length - The number of bytes to decode the value from
dft - The default value if a value could not be decoded
Returns:
The decoded value, or the default value

readInt

public int readInt(int length,
                   int dft,
                   java.lang.String msg)
Read a int value from payload (with default)

Parameters:
length - The number of bytes to decode the value from
dft - The default value if a value could not be decoded
msg - Debug message (used during "DebugLogging")
Returns:
The decoded value, or the default value

_readULong

protected long _readULong(int length,
                          long dft,
                          boolean bigEndian,
                          int frame,
                          java.lang.String msg)
Read an unsigned long value from payload (with default)

Parameters:
length - The number of bytes to decode the value from
dft - The default value if a value could not be decoded
bigEndian - True to read bytes in big-endian order, otherwise little-endian
frame - The current stackframe index
msg - Debug message (used during "DebugLogging")
Returns:
The decoded value, or the default value

readULong

public long readULong(int length,
                      long dft,
                      boolean bigEndian)
Read an unsigned long value from payload (with default)

Parameters:
length - The number of bytes to decode the value from
dft - The default value if a value could not be decoded
bigEndian - True to read bytes in big-endian order, otherwise little-endian
Returns:
The decoded value, or the default value

readULong

public long readULong(int length,
                      long dft,
                      boolean bigEndian,
                      java.lang.String msg)
Read an unsigned long value from payload (with default)

Parameters:
length - The number of bytes to decode the value from
dft - The default value if a value could not be decoded
bigEndian - True to read bytes in big-endian order, otherwise little-endian
msg - Debug message (used during "DebugLogging")
Returns:
The decoded value, or the default value

readULong

public long readULong(int length,
                      long dft)
Read an unsigned long value from payload (with default)

Parameters:
length - The number of bytes to decode the value from
dft - The default value if a value could not be decoded
Returns:
The decoded value, or the default value

readULong

public long readULong(int length,
                      long dft,
                      java.lang.String msg)
Read an unsigned long value from payload (with default)

Parameters:
length - The number of bytes to decode the value from
dft - The default value if a value could not be decoded
msg - Debug message (used during "DebugLogging")
Returns:
The decoded value, or the default value

readUInt

public int readUInt(int length,
                    int dft,
                    boolean bigEndian)
Read an unsigned int value from payload (with default)

Parameters:
length - The number of bytes to decode the value from
dft - The default value if a value could not be decoded
bigEndian - True to read bytes in big-endian order, otherwise little-endian
Returns:
The decoded value, or the default value

readUInt

public int readUInt(int length,
                    int dft,
                    boolean bigEndian,
                    java.lang.String msg)
Read an unsigned int value from payload (with default)

Parameters:
length - The number of bytes to decode the value from
dft - The default value if a value could not be decoded
bigEndian - True to read bytes in big-endian order, otherwise little-endian
msg - Debug message (used during "DebugLogging")
Returns:
The decoded value, or the default value

readUInt

public int readUInt(int length,
                    int dft)
Read an unsigned int value from payload (with default)

Parameters:
length - The number of bytes to decode the value from
dft - The default value if a value could not be decoded
Returns:
The decoded value, or the default value

readUInt

public int readUInt(int length,
                    int dft,
                    java.lang.String msg)
Read an unsigned int value from payload (with default)

Parameters:
length - The number of bytes to decode the value from
dft - The default value if a value could not be decoded
msg - Debug message (used during "DebugLogging")
Returns:
The decoded value, or the default value

isValidBCD

public boolean isValidBCD(int length)
Returns true if the length fields contains only BCD nybbles

Parameters:
length - The number of bytes to check for BCD nybbles
Returns:
True if all nybbles contain valid BCD digits

readLongBCD

public long readLongBCD(int length,
                        long dft)
Read an unsigned BCD (hex) encoded long value from the payload.

Parameters:
length - The number of bytes from which to decode the value
dft - The default value if a value could not be decoded
Returns:
The decoded value, or the default value

readLongBCD

public long readLongBCD(int length,
                        long dft,
                        java.lang.String msg)
Read an unsigned BCD (hex) encoded long value from the payload.

Parameters:
length - The number of bytes from which to decode the value
dft - The default value if a value could not be decoded
msg - Debug message (used during "DebugLogging")
Returns:
The decoded value, or the default value

readIntBCD

public int readIntBCD(int length,
                      int dft)
Read an unsigned BCD (hex) encoded int value from the payload.

Parameters:
length - The number of bytes from which to decode the value
dft - The default value if a value could not be decoded
Returns:
The decoded value, or the default value

readIntBCD

public int readIntBCD(int length,
                      int dft,
                      java.lang.String msg)
Read an unsigned BCD (hex) encoded int value from the payload.

Parameters:
length - The number of bytes from which to decode the value
dft - The default value if a value could not be decoded
msg - Debug message (used during "DebugLogging")
Returns:
The decoded value, or the default value

decodeDouble

public static double decodeDouble(byte[] data,
                                  int ofs,
                                  int len,
                                  boolean bigEndian,
                                  double dft)
Decodes a double value from bytes, using IEEE 754 format

Parameters:
data - The byte array from which to decode the double value
ofs - The offset into data to start decoding
len - The number of bytes from which the value is decoded
bigEndian - True if the bytes are in big-endian order, false if little-endian
dft - The default value if a value cannot be decoded
Returns:
The decoded value, or the default

_readDouble

protected double _readDouble(int length,
                             double dft,
                             boolean bigEndian,
                             int frame,
                             java.lang.String msg)
Read a double value from payload (with default), using IEEE 754 format

Parameters:
length - The number of bytes from which the value is decoded
dft - The default value if a value could not be decoded
bigEndian - True if the bytes are in big-endian order, false if little-endian
frame - The current stackframe index
msg - Debug message (used during "DebugLogging")
Returns:
The decoded value, or the default value

readDouble

public double readDouble(int length,
                         double dft,
                         boolean bigEndian)
Read a double value from payload (with default), using IEEE 754 format

Parameters:
length - The number of bytes from which the value is decoded
dft - The default value if a value could not be decoded
bigEndian - True if the bytes are in big-endian order, false if little-endian
Returns:
The decoded value, or the default value

readDouble

public double readDouble(int length,
                         double dft,
                         boolean bigEndian,
                         java.lang.String msg)
Read a double value from payload (with default), using IEEE 754 format

Parameters:
length - The number of bytes from which the value is decoded
dft - The default value if a value could not be decoded
bigEndian - True if the bytes are in big-endian order, false if little-endian
msg - Debug message (used during "DebugLogging")
Returns:
The decoded value, or the default value

readDouble

public double readDouble(int length,
                         double dft)
Read a double value from payload (with default), using IEEE 754 format

Parameters:
length - The number of bytes from which the value is decoded
dft - The default value if a value could not be decoded
Returns:
The decoded value, or the default value

readDouble

public double readDouble(int length,
                         double dft,
                         java.lang.String msg)
Read a double value from payload (with default), using IEEE 754 format

Parameters:
length - The number of bytes from which the value is decoded
dft - The default value if a value could not be decoded
msg - Debug message (used during "DebugLogging")
Returns:
The decoded value, or the default value

_readString

protected java.lang.String _readString(int length,
                                       boolean varLength,
                                       int frame,
                                       java.lang.String msg)
Read a string from the payload. The string is read until (whichever comes first):
  1. length bytes have been read
  2. a null (0x00) byte is found (if varLength==true)
  3. end of data is reached

Parameters:
length - The maximum length to read
varLength - If the string can be variable in length (stop on a null)
frame - The current stackframe index
msg - Debug message (used during "DebugLogging")
Returns:
The read String

readString

public java.lang.String readString(int length,
                                   boolean varLength)
Read a string from the payload. The string is read until (whichever comes first):
  1. length bytes have been read
  2. a null (0x00) byte is found (if varLength==true)
  3. end of data is reached

Parameters:
length - The maximum length to read
varLength - If the string can be variable in length (stop on a null)
Returns:
The read String

readString

public java.lang.String readString(int length,
                                   boolean varLength,
                                   java.lang.String msg)
Read a string from the payload. The string is read until (whichever comes first):
  1. length bytes have been read
  2. a null (0x00) byte is found (if varLength==true)
  3. end of data is reached

Parameters:
length - The maximum length to read
varLength - If the string can be variable in length (stop on a null)
msg - Debug message (used during "DebugLogging")
Returns:
The read String

readString

public java.lang.String readString(int length)
Reads a variable length string from the payload

Parameters:
length - The maximum length of the string to read
Returns:
The read String
See Also:
readString(int length, boolean varLength)

readString

public java.lang.String readString(int length,
                                   java.lang.String msg)
Reads a variable length string from the payload

Parameters:
length - The maximum length of the string to read
msg - Debug message (used during "DebugLogging")
Returns:
The read String
See Also:
readString(int length, boolean varLength)

_readStringHex

protected java.lang.String _readStringHex(int length,
                                          int frame,
                                          java.lang.String msg)
Reads a fixed length hex string from the payload bytes. The bytes read will be returned as a hex string, so the length of the returned String will be twice that of the number of bytes specified.

Parameters:
length - The maximum number of bytes to read into a hex string
msg - Debug message (used during "DebugLogging")
Returns:
The read hex String

readStringHex

public java.lang.String readStringHex(int length)
Reads a fixed length hex string from the payload bytes. The bytes read will be returned as a hex string, so the length of the returned String will be twice that of the number of bytes specified.

Parameters:
length - The maximum number of bytes to read into a hex string
Returns:
The read hex String

readStringHex

public java.lang.String readStringHex(int length,
                                      java.lang.String msg)
Reads a fixed length hex string from the payload bytes. The bytes read will be returned as a hex string, so the length of the returned String will be twice that of the number of bytes specified.

Parameters:
length - The maximum number of bytes to read into a hex string
msg - Debug message (used during "DebugLogging")
Returns:
The read hex String

readGPS

public GeoPoint readGPS(int length)
Reads an encoded GPS point (latitude,longitude) from the payload

Parameters:
length - The number of bytes to decode the GeoPoint from
Returns:
The decoded GeoPoint
See Also:
GeoPoint.decodeGeoPoint(byte[], int, int), GeoPoint.encodeGeoPoint(org.opengts.util.GeoPoint, byte[], int, int)

readGPS

public GeoPoint readGPS(int length,
                        java.lang.String msg)
Reads an encoded GPS point (latitude,longitude) from the payload

Parameters:
length - The number of bytes to decode the GeoPoint from
msg - Debug message (used during "DebugLogging")
Returns:
The decoded GeoPoint
See Also:
GeoPoint.decodeGeoPoint(byte[], int, int), GeoPoint.encodeGeoPoint(org.opengts.util.GeoPoint, byte[], int, int)

encodeLong

public static int encodeLong(byte[] data,
                             int ofs,
                             int len,
                             boolean bigEndian,
                             long val)
Encodes a long value into bytes

Parameters:
data - The byte array into which the value is encoded
ofs - The offset into data to start encoding
len - The number of bytes to encode
bigEndian - True if the bytes are to be big-endian ordered, false if little-endian
val - The value to encode
Returns:
The number of bytes written to data.

encodeLong

public static byte[] encodeLong(long val,
                                int len,
                                boolean bigEndian)
Encodes a long value into bytes

Parameters:
val - The value to encode
len - The number of bytes to encode
bigEndian - True if the bytes are to be Big-Endian ordered, false if Little-Endian
Returns:
The encoded byte array

_writeLong

protected int _writeLong(long val,
                         int wrtLen,
                         boolean bigEndian,
                         int frame)
Write a long value to the payload

Parameters:
val - The value to write
wrtLen - The number of bytes into which the value is written
bigEndian - True if the bytes are to be big-endian ordered, false if little-endian
Returns:
The number of bytes written

writeLong

public int writeLong(long val,
                     int length,
                     boolean bigEndian)
Write a long value to the payload

Parameters:
val - The value to write
length - The number of bytes into which the value is written
bigEndian - True if the bytes are to be big-endian ordered, false if little-endian
Returns:
The number of bytes written

writeLong

public int writeLong(long val,
                     int length)
Write a long value to the payload

Parameters:
val - The value to write
length - The number of bytes into which the value is written
Returns:
The number of bytes written

writeULong

public int writeULong(long val,
                      int length,
                      boolean bigEndian)
Write a unsigned long value to the payload.

Parameters:
val - The value to write
length - The number of bytes into which the value is written
bigEndian - True if the bytes are to be big-endian ordered, false if little-endian
Returns:
The number of bytes written
See Also:
writeLong(long, int, boolean)

writeULong

public int writeULong(long val,
                      int length)
Write a unsigned long value to the payload.

Parameters:
val - The value to write
length - The number of bytes into which the value is written
Returns:
The number of bytes written
See Also:
writeLong(long, int, boolean)

writeInt

public int writeInt(int val,
                    int length)
Write a int value to the payload

Parameters:
val - The value to write
length - The number of bytes into which the value is written
Returns:
The number of bytes written

writeUInt

public int writeUInt(int val,
                     int length)
Write a unsigned int value to the payload.

Parameters:
val - The value to write
length - The number of bytes into which the value is written
Returns:
The number of bytes written
See Also:
writeInt(int, int)

writeByte

public int writeByte(byte val)
Write a byte value to the payload

Parameters:
val - The value to write
Returns:
The number of bytes written ("1")

encodeDouble

public static int encodeDouble(byte[] data,
                               int ofs,
                               int len,
                               boolean bigEndian,
                               double val)
Encodes a double value into bytes

Parameters:
data - The byte array to encode the value to
ofs - The offset into data to start encoding to
len - The number of bytes to encode the value to
bigEndian - True if the bytes are to be big-endian ordered, false if little-endian
val - The value to encode
Returns:
The number of bytes written to data. 0 or len

_writeDouble

protected int _writeDouble(double val,
                           int wrtLen,
                           boolean bigEndian,
                           int frame)
Write a double value to the payload

Parameters:
val - The value to write
wrtLen - The number of bytes to write the value into
bigEndian - True if the bytes are to be big-endian ordered, false if little-endian
Returns:
The number of bytes written

writeDouble

public int writeDouble(double val,
                       int wrtLen,
                       boolean bigEndian)
Write a double value to the payload

Parameters:
val - The value to write
wrtLen - The number of bytes to write the value into
bigEndian - True if the bytes are to be big-endian ordered, false if little-endian
Returns:
The number of bytes written

writeDouble

public int writeDouble(double val,
                       int wrtLen)
Write a double value to the payload

Parameters:
val - The value to write
wrtLen - The number of bytes to write the value into
Returns:
The number of bytes written

_writeZeroFill

protected int _writeZeroFill(int wrtLen,
                             int frame)
Write a zero fill to the payload

Parameters:
wrtLen - The number of bytes to write
frame - The current stackframe index
Returns:
The number of bytes written

writeZeroFill

public int writeZeroFill(int wrtLen)
Write a zero fill to the payload

Parameters:
wrtLen - The number of bytes to write
Returns:
The number of bytes written

_writeBytes

protected int _writeBytes(byte[] n,
                          int nOfs,
                          int nLen,
                          int wrtLen,
                          int frame)
Write an array of bytes to the payload

Parameters:
n - The bytes to write to the payload
nOfs - The offset into n to start reading from
nLen - The number of bytes to write from n
wrtLen - The total number of bytes to write. (remaining bytes filled with '0' if greater than nLen
frame - The current stackframe index
Returns:
The number of bytes written

writeBytes

public int writeBytes(byte[] n,
                      int nOfs,
                      int nLen,
                      int wrtLen)
Write an array of bytes to the payload

Parameters:
n - The bytes to write to the payload
nOfs - The offset into n to start reading from
nLen - The number of bytes to write from n
wrtLen - The total number of bytes to write. (remaining bytes filled with '0' if greater than nLen
Returns:
The number of bytes written

writeBytes

public int writeBytes(byte[] n,
                      int wrtLen)
Write an array of bytes to the payload

Parameters:
n - The bytes to write to the payload
wrtLen - The total number of bytes to write. (remaining bytes filled with '0' if greater than n.length
Returns:
The number of bytes written

writeBytes

public int writeBytes(byte[] n)
Write an array of bytes to the payload

Parameters:
n - The bytes to write to the payload
Returns:
The number of bytes written

writeRandomBytes

public int writeRandomBytes(java.util.Random rand,
                            int wrtLen)
Write random bytes to the payload

Parameters:
rand - The randomizer from this the random byte are retrieved
wrtLen - The number of bytes to write to the payload
Returns:
The number of bytes written

writeString

public int writeString(java.lang.String s,
                       int wrtLen)
Write a string to the payload. Writes until either wrtLen bytes are written or the string terminates

Parameters:
s - The string to write
wrtLen - The maximum number of bytes to write to the payload
Returns:
The number of bytes written

writeString

public int writeString(java.lang.String s,
                       int wrtLen,
                       boolean varLength)
Write a string to the payload. Writes until either wrtLen bytes are written or the string terminates

Parameters:
s - The string to write
wrtLen - The maximum number of bytes to write
Returns:
The number of bytes written

writeGPS

public int writeGPS(double lat,
                    double lon,
                    int length)
Encode a new GPS point into the payload

Parameters:
lat - The latitude of the GPS point
lon - The longitude of the GPS point
length - The total number of bytes to write. Defaults to a minimum of 6
Returns:
The total number of bytes written
See Also:
GeoPoint.encodeGeoPoint(org.opengts.util.GeoPoint, byte[], int, int), GeoPoint.decodeGeoPoint(byte[], int, int)

writeGPS

public int writeGPS(GeoPoint gp,
                    int wrtLen)
Encode a GPS point into the payload

Parameters:
gp - The GPS point to encode
wrtLen - The total number of bytes to write. Defaults to a minimum of 6
Returns:
The total number of bytes written
See Also:
GeoPoint.encodeGeoPoint(org.opengts.util.GeoPoint, byte[], int, int), GeoPoint.decodeGeoPoint(byte[], int, int)

toString

public java.lang.String toString()
Returns a hex string representation of the payload

Overrides:
toString in class java.lang.Object
Returns:
A hex string representation

main

public static void main(java.lang.String[] argv)
Main entry point for testing/debugging

Parameters:
argv - Comand-line arguments