ch.hevs.isi.utils

## Class Utility

- java.lang.Object
- - ch.hevs.isi.utils.Utility

    public class Utility
    extends java.lang.Object

This class contains some useful Java methods to manipulate data.

### Field Summary

Fields
`static java.util.Random` `rnd` Object to get some random values...
`static int` `TCP_BUFFER_SIZE` Default size for the TCP input stream

### Constructor Summary

Constructors
Constructor and Description
`Utility()`

### Method Summary

All Methods Static Methods Concrete Methods
`static java.lang.Float` `bytesToFloat(byte[] bytes, int offset, int size)` Returns a `float` value from array of bytes.
`static byte[]` `calculateCRC(byte[] data, int offset, int len)` Calculates and returns the CRC using the data passed in parameters.
`static boolean` `checkCRC(byte[] data, int offset, int len, byte[] crc)` Checks the CRC and returns true if it is correct, otherwise false.
`static void` `DEBUG(java.lang.String className, java.lang.String method, java.lang.String msg)`
`static java.io.BufferedReader` `fileParser(java.lang.String path, java.lang.String fileName)` Method to use to access a file in your resources folder...
`static java.lang.String` `getHexString(byte[] b)` Utility method to convert a byte array in a string made up of hex (0,.. 9, a,..f).
`static java.lang.String` `getHexString(byte[] b, int offset, int length)` Utility method to convert a byte array in a string made up of hex (0,.. 9, a,..f)
`static java.lang.String` `getStringRndVal(int factor)` Retrieves a random value rounded to 2 decimal...
`static java.lang.String` `md5sum(java.lang.String msg)` Returns the md5 of any input...
`static byte[]` `readBytes(java.io.InputStream in)` Reads the incoming data from an input stream as long as there is something to read and saved the data in an array of bytes.
`static byte[]` `readLine(java.io.InputStream in)` Reads a line of text.
`static byte[]` `readNBytes(java.io.InputStream in, int len)` Reads from the given input stream an amount of bytes and retrieves these data as an array of bytes.
`static void` `sendBytes(java.io.OutputStream out, byte[] toSend, int offset, int length)` This method sends the content of the `buffer` to the given `OutputStream`.
`static int` `unsignedByteToSignedInt(byte from)` Converts an unsigned byte to a signed integer.
`static void` `waitSomeTime(int ms)` To wait some times ...
`static void` `writeLine(java.io.OutputStream out, byte[] toSend)` Send the data contained in the given array of bytes through the output stream.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### TCP_BUFFER_SIZE

    public static final int TCP_BUFFER_SIZE

Default size for the TCP input stream

See Also:
  Constant Field Values

#### rnd

    public static java.util.Random rnd

Object to get some random values...

### Constructor Detail

#### Utility

    public Utility()

### Method Detail

#### calculateCRC

    public static byte[] calculateCRC(byte[] data,
                                      int offset,
                                      int len)

Calculates and returns the CRC using the data passed in parameters.

Parameters:
  `data` - a byte array containing the data to send
  `offset` - the offset
  `len` - the data length
Returns:
  byte[] the CRC

#### checkCRC

    public static boolean checkCRC(byte[] data,
                                   int offset,
                                   int len,
                                   byte[] crc)

Checks the CRC and returns true if it is correct, otherwise false.

Parameters:
  `data` - a byte array containing the data to send
  `offset` - the offset
  `len` - the data length
  `crc` - a byte array containing the CRC to check
Returns:
  boolean true if the CRC is correct, otherwise false

#### unsignedByteToSignedInt

    public static int unsignedByteToSignedInt(byte from)

Converts an unsigned byte to a signed integer.

Parameters:
  `from` - an unsigned byte to convert to a signed integer
Returns:
  int a signed integer

#### getHexString

    public static java.lang.String getHexString(byte[] b)

Utility method to convert a byte array in a string made up of hex (0,.. 9, a,..f). The array of bytes is converted from offset 0 to its end.

Parameters:
  `b` - Array of bytes to be converted in HEX string.
Returns:
  A String representing the HEX values of the given array of bytes.

#### getHexString

    public static java.lang.String getHexString(byte[] b,
                                                int offset,
                                                int length)

Utility method to convert a byte array in a string made up of hex (0,.. 9, a,..f)

Parameters:
  `b` - The byte array to convert in HEX string
  `offset` - The index where we start to convert from.
  `length` - The amount of bytes to convert in HEX string
Returns:
  A String representing the HEX values of the selected bytes of the array.

#### getStringRndVal

    public static java.lang.String getStringRndVal(int factor)

Retrieves a random value rounded to 2 decimal...

Parameters:
  `factor` - A coefficient which the random value is multiplied with.
Returns:
  A random value rounded to 2 decimal converted to a String.

#### readBytes

    public static byte[] readBytes(java.io.InputStream in)
                            throws java.io.IOException

Reads the incoming data from an input stream as long as there is something to read and saved the data in an array of bytes. The method is blocking ! This method blocks until input data is available, end of file is detected, or an exception is thrown. If the length of of read data is zero, then no bytes are read and an empty array of bytes is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at the end of the file, the value -1 is returned; otherwise, at least one byte is read and returned as an array of bytes.

Parameters:
  `in` - The input Stream where to read the data coming from.
Returns:
  The read data as an `array of bytes`. Or null if the has been closed by the peer while waiting for incoming data.
Throws:
  `java.io.IOException` - If the first byte cannot be read for any reason other than the end of the file, if the input stream has been closed, or if some other I/O error occurs.

#### readNBytes

    public static byte[] readNBytes(java.io.InputStream in,
                                    int len)
                             throws java.io.IOException

Reads from the given input stream an amount of bytes and retrieves these data as an array of bytes. The method is blocking !

Parameters:
  `in` - The input stream where to read the data coming from.
  `len` - The amount of data to read
Returns:
  The read data as an `array of bytes`.
Throws:
  `java.io.IOException` - If the first byte cannot be read for any reason other than the end of the file, if the input stream has been closed, or if some other I/O error occurs.

#### readLine

    public static byte[] readLine(java.io.InputStream in)
                           throws java.io.IOException

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a line feed.

Parameters:
  `in` - The Input Stream to read from.
Returns:
  An array of bytes containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached.
Throws:
  `java.io.IOException` - If an I/O error occurs

#### writeLine

    public static void writeLine(java.io.OutputStream out,
                                 byte[] toSend)
                          throws java.io.IOException

Send the data contained in the given array of bytes through the output stream. If the data to send do not end with a carriage return, the method adds '\r\n' at the end.

Parameters:
  `out` - The Output Stream to send the data to.
  `toSend` - The data to send
Throws:
  `java.io.IOException` - If an I/O error occurs

#### sendBytes

    public static void sendBytes(java.io.OutputStream out,
                                 byte[] toSend,
                                 int offset,
                                 int length)
                          throws java.io.IOException

This method sends the content of the `buffer` to the given `OutputStream`. This content is specified by the starting position, defined by `offset`, and the amount of bytes,defined by `length`. If an error occurs during this process, a exception will be raised.

Parameters:
  `out` - The Output Stream to send the data to.
  `toSend` - The data to send
  `offset` - The starting position in the buffer of data
  `length` - The amount of bytes to be sent.
Throws:
  `java.io.IOException` - If an I/O error occurs

#### waitSomeTime

    public static void waitSomeTime(int ms)

To wait some times ... Simply give the delay you want to wait for and that's it !

Parameters:
  `ms` - The amount of time to wait expressed in milli-seconds [ms].

#### md5sum

    public static java.lang.String md5sum(java.lang.String msg)

Returns the md5 of any input...

Parameters:
  `msg` - The input string to process
Returns:
  The md5 of the input string.

#### bytesToFloat

    public static java.lang.Float bytesToFloat(byte[] bytes,
                                               int offset,
                                               int size)

Returns a `float` value from array of bytes. This byte's array can only be 2 or 4 bytes long.

Parameters:
  `bytes` - The array of bytes to convert.
  `offset` - The position where the method has to start to get the bytes from.
  `size` - The amount of bytes to convert.
Returns:
  A `Float` value or `null` if the process failed.

#### fileParser

    public static java.io.BufferedReader fileParser(java.lang.String path,
                                                    java.lang.String fileName)

Method to use to access a file in your resources folder... The file you want to access has to be under the `resources` folder of your project. You can create as many sub-folders as you want, they will become the path of the file to work with. This path is given to the method by the parameter `path` and the file is identified by its name and extension.

Parameters:
  `path` - folder's hierarchy (if exists) from `resources`
  `fileName` - Name of the file to access
Returns:
  A `BufferedReader` related to the file you want to work with, or null if the file could not be reached.

#### DEBUG

    public static void DEBUG(java.lang.String className,
                             java.lang.String method,
                             java.lang.String msg)