Package org.medcare.igtl.util
Class BytesArray
java.lang.Object
org.medcare.igtl.util.BytesArray
** For reading/writing binary fields
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic long
crc64
(byte[] buffer, int len, long crc) Calculates CRC from a byte buffer based on the polynom specified in ECMA-182.long
crc64
(byte[] buf, long crc) ** Calculate acrc
value Adds from a complete byte array to the data checksum.double
decodeDouble
(byte[] data, int ofs) ** Decodes adouble
value from bytes, using IEEE 754 format **long
decodeLong
(byte[] data, int ofs, boolean signed) ** Decodes along
value from bytes **long
decodeTimeStamp
(byte[] array) Will read a 8 bytes array and return it as a milli sec time stamp.byte[]
encodeDouble
(int len, double val) ** Encodes adouble
value 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 along
value 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) ** Readlength
of bytes from the bytesArray **double
getDouble
(int length) ** Read adouble
value from bytesArray, using IEEE 754 format **int
getIndex()
** Gets the current read/write index return@ The indexlong
getLong
(int length) ** Read along
value from bytesArray **int
getSize()
** 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.long
getULong
(int length) ** Read an unsignedlong
value from bytesArray **long
next_crc
(long crc, byte ch) Calculates the next crc value.static void
printBytes
(byte[] b) int
putByte
(byte n) ** Write an array of bytes to the bytesArray **int
putBytes
(byte[] n) ** Write an array of bytes to the bytesArray **int
putBytes
(byte[] n, int nLen) ** Write an array of bytes to the bytesArray **int
putBytes
(byte[] n, int nOfs, int nLen) ** Write an array of bytes to the bytesArray **long
putCrc
(byte[] buf, int len, long crc) ** Calculate and Add acrc
value to the bytesArray **int
putDouble
(double val, int wrtLen) ** Add adouble
value to the bytesArray **int
putLong
(long val, int wrtLen) ** Add along
value to the bytesArray **int
putShort
(short val) ** Add ashort
value to the bytesArray **int
** Add a string to the bytesArray.long
** Calculate Add atimestamp
value to the bytesArray ** ** @return The long value of timeStampint
putTimeStamp
(float t) ** Add atimestamp
value to the bytesArray ** ** @param t float timestampn
** @return The number of bytes writtenint
putULong
(long val, int wrtLen) ** Add a unsignedlong
value to the bytesArray.void
setIndex
(int ndx) ** Resets the read/write index to the specified value **toString()
short
unsignedByteToShort
(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 inton
to 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) ** Readlength
of 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 along
value 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 unsignedlong
value 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 adouble
value 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):length
bytes 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 along
value 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 unsignedlong
value 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 atimestamp
value to the bytesArray ** ** @return The long value of timeStamp -
putTimeStamp
public int putTimeStamp(float t) ** Add atimestamp
value 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 acrc
value 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 adouble
value 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 ashort
value 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 along
value 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 along
value from bytes **- Parameters:
data
- The byte array to decode the value from ** @param ofs The offset intodata
to 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 adouble
value 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 adouble
value from bytes, using IEEE 754 format **- Parameters:
data
- The byte array from which to decode thedouble
value ** @param ofs The offset intodata
to 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
crc
value 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 acrc
value 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)
-