Package org.medcare.igtl.util
Class BytesArray
java.lang.Object
org.medcare.igtl.util.BytesArray
** For reading/writing binary fields
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic longcrc64(byte[] buffer, int len, long crc) Calculates CRC from a byte buffer based on the polynom specified in ECMA-182.longcrc64(byte[] buf, long crc) ** Calculate acrcvalue Adds from a complete byte array to the data checksum.doubledecodeDouble(byte[] data, int ofs) ** Decodes adoublevalue from bytes, using IEEE 754 format **longdecodeLong(byte[] data, int ofs, boolean signed) ** Decodes alongvalue from bytes **longdecodeTimeStamp(byte[] array) Will read a 8 bytes array and return it as a milli sec time stamp.byte[]encodeDouble(int len, double val) ** Encodes adoublevalue into bytes ** ** @param len The number of bytes to encode the value to ** @param val The value to encode **byte[]encodeLong(int len, long val, boolean signed) ** Encodes alongvalue into bytes ** ** @param len The number of bytes to encode the value to ** @param val The value to encode ** @param signed true if signed false if unsigned **byte[]encodeTimeStamp(long t) Encodes a time in millisec in a 8 bytes array **byte[]encodeTimeStamp(long s, long f) Encodes a time in millisec in a 8 bytes array **byte[]getBytes()** Return a byte array representing the databyte[]getBytes(int length) ** Readlengthof bytes from the bytesArray **doublegetDouble(int length) ** Read adoublevalue from bytesArray, using IEEE 754 format **intgetIndex()** Gets the current read/write index return@ The indexlonggetLong(int length) ** Read alongvalue from bytesArray **intgetSize()** For an output/write bytesArray, returns the number of bytes written.getString(int len) ** Converts the byte array to a String based on the US-ASCII character set.getString(int length, boolean varLength) ** Read a string from the bytesArray.longgetULong(int length) ** Read an unsignedlongvalue from bytesArray **longnext_crc(long crc, byte ch) Calculates the next crc value.static voidprintBytes(byte[] b) intputByte(byte n) ** Write an array of bytes to the bytesArray **intputBytes(byte[] n) ** Write an array of bytes to the bytesArray **intputBytes(byte[] n, int nLen) ** Write an array of bytes to the bytesArray **intputBytes(byte[] n, int nOfs, int nLen) ** Write an array of bytes to the bytesArray **longputCrc(byte[] buf, int len, long crc) ** Calculate and Add acrcvalue to the bytesArray **intputDouble(double val, int wrtLen) ** Add adoublevalue to the bytesArray **intputLong(long val, int wrtLen) ** Add alongvalue to the bytesArray **intputShort(short val) ** Add ashortvalue to the bytesArray **int** Add a string to the bytesArray.long** Calculate Add atimestampvalue to the bytesArray ** ** @return The long value of timeStampintputTimeStamp(float t) ** Add atimestampvalue to the bytesArray ** ** @param t float timestampn** @return The number of bytes writtenintputULong(long val, int wrtLen) ** Add a unsignedlongvalue to the bytesArray.voidsetIndex(int ndx) ** Resets the read/write index to the specified value **toString()shortunsignedByteToShort(byte b) Converts an unsigned byte to a short.
-
Field Details
-
MAX_UINT
public static final long MAX_UINT- See Also:
-
-
Constructor Details
-
BytesArray
public BytesArray()** Destination Constructor **
-
-
Method Details
-
putBytes
public int putBytes(byte[] n, int nOfs, int nLen) ** Write an array of bytes to the bytesArray **- Parameters:
n- The bytes to write to the bytesArray ** @param nOfs The offset intonto start reading from ** @param nLen The number of bytes to write fromn** @return The number of bytes written
-
putBytes
public int putBytes(byte[] n, int nLen) ** Write an array of bytes to the bytesArray **- Parameters:
n- The bytes to write to the bytesArray ** @param nLen The number of bytes to write fromn** @return The number of bytes written
-
putByte
public int putByte(byte n) ** Write an array of bytes to the bytesArray **- Parameters:
n- The bytes to write to the bytesArray ** @param nLen The number of bytes to write fromn** @return The number of bytes written
-
putBytes
public int putBytes(byte[] n) ** Write an array of bytes to the bytesArray **- Parameters:
n- The bytes to write to the bytesArray ** @return The number of bytes writen
-
getSize
public int getSize()** For an output/write bytesArray, returns the number of bytes written. For an input/read bytesArray, return the total number of bytes contained in this bytesArray. **- Returns:
- The current size of the bytesArray
-
getIndex
public int getIndex()** Gets the current read/write index return@ The index -
setIndex
public void setIndex(int ndx) ** Resets the read/write index to the specified value **- Parameters:
ndx- The value to set the index
-
getBytes
public byte[] getBytes()** Return a byte array representing the data**
- Returns:
- A copy of the current bytesArray (as-is)
-
getBytes
public byte[] getBytes(int length) ** Readlengthof bytes from the bytesArray **- Parameters:
length- The number fo bytes to read from the bytesArray **- Returns:
- A copy of the current bytesArray or byte[0]
-
getLong
public long getLong(int length) ** Read alongvalue from bytesArray **- Parameters:
length- The number of bytes to decode the value from ** @return The decoded long value, or 0L
-
getULong
public long getULong(int length) ** Read an unsignedlongvalue from bytesArray **- Parameters:
length- The number of bytes to decode the value from ** @return The decoded long value, or 0L
-
getDouble
public double getDouble(int length) ** Read adoublevalue from bytesArray, using IEEE 754 format **- Parameters:
length- The number of bytes from which the value is decoded ** @return The decoded double value, or 0L
-
getString
** Read a string from the bytesArray. The string is read until (whichever comes first):lengthbytes have been read- a null (0x00) byte is found (if
varLength==true) - end of data is reached
- Parameters:
length- The maximum length to read ** @param varLength Boolean telling if the string can be variable in length (stop on a null) ** @return The read String
-
getString
** Converts the byte array to a String based on the US-ASCII character set. ** @param len The number of bytes to convert to a String ** @return The String representation of the specified byte array -
putLong
public int putLong(long val, int wrtLen) ** Add alongvalue to the bytesArray **- Parameters:
val- The value to write ** @param wrtLen The number of bytes to write the value into ** @return The number of bytes written
-
putULong
public int putULong(long val, int wrtLen) ** Add a unsignedlongvalue to the bytesArray. Same as writeLong() **- Parameters:
val- The value to write ** @param wrtLen The number of bytes to write the value into ** @return The number of bytes written
-
putTimeStamp
public long putTimeStamp()** Calculate Add atimestampvalue to the bytesArray ** ** @return The long value of timeStamp -
putTimeStamp
public int putTimeStamp(float t) ** Add atimestampvalue to the bytesArray ** ** @param t float timestampn** @return The number of bytes written -
putCrc
public long putCrc(byte[] buf, int len, long crc) ** Calculate and Add acrcvalue to the bytesArray **- Parameters:
buf- The bytes to callculate the crc from ** @param len The number of bytes to calaculate from ** @param crc actual crc valuen** @return The long value of crc
-
putDouble
public int putDouble(double val, int wrtLen) ** Add adoublevalue to the bytesArray **- Parameters:
val- The value to write ** @param wrtLen The number of bytes to write the value into ** @return The number of bytes written
-
putShort
public int putShort(short val) ** Add ashortvalue to the bytesArray **- Parameters:
val- The value to write ** @param wrtLen The number of bytes to write the value into ** @return The number of bytes written
-
putString
** Add a string to the bytesArray. Writes until eitherwrtLen** bytes are written or the string terminates **- Parameters:
s- The string to write ** @return The number of bytes written
-
encodeLong
public byte[] encodeLong(int len, long val, boolean signed) ** Encodes alongvalue into bytes ** ** @param len The number of bytes to encode the value to ** @param val The value to encode ** @param signed true if signed false if unsigned **- Returns:
- the bytes array created from the value
-
decodeLong
public long decodeLong(byte[] data, int ofs, boolean signed) ** Decodes alongvalue from bytes **- Parameters:
data- The byte array to decode the value from ** @param ofs The offset intodatato start decoding from ** @param signed If the encoded bytes represent a signed value ** @return The decoded value, or 0L
-
encodeDouble
public byte[] encodeDouble(int len, double val) ** Encodes adoublevalue into bytes ** ** @param len The number of bytes to encode the value to ** @param val The value to encode **- Returns:
- the bytes array created from the value
-
decodeDouble
public double decodeDouble(byte[] data, int ofs) ** Decodes adoublevalue from bytes, using IEEE 754 format **- Parameters:
data- The byte array from which to decode thedoublevalue ** @param ofs The offset intodatato start decoding ** @return The decoded value, or 0L
-
encodeTimeStamp
public byte[] encodeTimeStamp(long t) Encodes a time in millisec in a 8 bytes array **- Parameters:
t- the time in millis from 00:00:00 January 1, 1970, UTC value ** @return The byte array containing the timestamp
-
encodeTimeStamp
public byte[] encodeTimeStamp(long s, long f) Encodes a time in millisec in a 8 bytes array **- Parameters:
s- the seconds from time in millis from 00:00:00 January 1, 1970, UTC valuef- the fraction of time in millis from 00:00:00 January 1, 1970, UTC value ** @return The byte array containing the timestamp
-
decodeTimeStamp
public long decodeTimeStamp(byte[] array) Will read a 8 bytes array and return it as a milli sec time stamp. **- Parameters:
array- the array to decode TimeStamp from** @return The long time stamp value
-
unsignedByteToShort
public short unsignedByteToShort(byte b) Converts an unsigned byte to a short. By default, Java assumes that a byte is signed.- Parameters:
b- the byte to get short from** @return The short value
-
crc64
public static long crc64(byte[] buffer, int len, long crc) Calculates CRC from a byte buffer based on the polynom specified in ECMA-182.** Calculate a
crcvalue Adds from a partial byte array to the data checksum. **- Parameters:
buffer- The bytes to callculate the crc from ** @param len The number of bytes to calculate from ** @param crc actual crc valuen** @return The long crc64 value
-
crc64
public long crc64(byte[] buf, long crc) ** Calculate acrcvalue Adds from a complete byte array to the data checksum. **- Parameters:
buf- The bytes to callculate the crc from ** @param crc actual crc valuen** @return The long crc64 value
-
next_crc
public long next_crc(long crc, byte ch) Calculates the next crc value. ** ** @param crc actual crc valuen** @param ch The next byte ** @return The long crc64 value -
toString
-
printBytes
public static void printBytes(byte[] b)
-