Class BytesArray

java.lang.Object
org.medcare.igtl.util.BytesArray

public class BytesArray extends Object
** For reading/writing binary fields
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final long
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    ** Destination Constructor **
  • Method Summary

    Modifier and Type
    Method
    Description
    static 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 a crc value Adds from a complete byte array to the data checksum.
    double
    decodeDouble(byte[] data, int ofs)
    ** Decodes a double value from bytes, using IEEE 754 format **
    long
    decodeLong(byte[] data, int ofs, boolean signed)
    ** Decodes a long 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 a double 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 a long 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[]
    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[]
    ** Return a byte array representing the data
    byte[]
    getBytes(int length)
    ** Read length of bytes from the bytesArray **
    double
    getDouble(int length)
    ** Read a double value from bytesArray, using IEEE 754 format **
    int
    ** Gets the current read/write index return@ The index
    long
    getLong(int length)
    ** Read a long value from bytesArray **
    int
    ** 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 unsigned long 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 a crc value to the bytesArray **
    int
    putDouble(double val, int wrtLen)
    ** Add a double value to the bytesArray **
    int
    putLong(long val, int wrtLen)
    ** Add a long value to the bytesArray **
    int
    putShort(short val)
    ** Add a short value to the bytesArray **
    int
    ** Add a string to the bytesArray.
    long
    ** Calculate Add a timestamp value to the bytesArray ** ** @return The long value of timeStamp
    int
    putTimeStamp(float t)
    ** Add a timestamp value to the bytesArray ** ** @param t float timestamp n ** @return The number of bytes written
    int
    putULong(long val, int wrtLen)
    ** Add a unsigned long value to the bytesArray.
    void
    setIndex(int ndx)
    ** Resets the read/write index to the specified value **
     
    short
    Converts an unsigned byte to a short.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • 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 into n to start reading from ** @param nLen The number of bytes to write from n ** @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 from n ** @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 from n ** @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)
      ** Read length 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 a long 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 unsigned long 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 a double 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

      public String getString(int length, boolean varLength)
      ** Read a string from the bytesArray. 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 ** @param varLength Boolean telling if the string can be variable in length (stop on a null) ** @return The read String
    • getString

      public String getString(int len)
      ** 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 a long 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 unsigned long 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 a timestamp value to the bytesArray ** ** @return The long value of timeStamp
    • putTimeStamp

      public int putTimeStamp(float t)
      ** Add a timestamp value to the bytesArray ** ** @param t float timestamp n ** @return The number of bytes written
    • putCrc

      public long putCrc(byte[] buf, int len, long crc)
      ** Calculate and Add a crc 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 value n ** @return The long value of crc
    • putDouble

      public int putDouble(double val, int wrtLen)
      ** Add a double 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 a short 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

      public int putString(String s)
      ** Add a string to the bytesArray. Writes until either wrtLen ** 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 a long 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 a long value from bytes **
      Parameters:
      data - The byte array to decode the value from ** @param ofs The offset into data 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 a double 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 a double value from bytes, using IEEE 754 format **
      Parameters:
      data - The byte array from which to decode the double value ** @param ofs The offset into data 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 value
      f - 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 value n ** @return The long crc64 value
    • crc64

      public long crc64(byte[] buf, long crc)
      ** Calculate a crc value Adds from a complete byte array to the data checksum. **
      Parameters:
      buf - The bytes to callculate the crc from ** @param crc actual crc value n ** @return The long crc64 value
    • next_crc

      public long next_crc(long crc, byte ch)
      Calculates the next crc value. ** ** @param crc actual crc value n ** @param ch The next byte ** @return The long crc64 value
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • printBytes

      public static void printBytes(byte[] b)