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 Modbus data.

### Field Summary

Fields
`static boolean` `DEBUG_MODE`
`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.Object object, java.lang.String method, java.lang.String msg)`
`static void` `DEBUG(java.lang.String className, java.lang.String method, java.lang.String msg)`
`static void` `DEBUG(java.lang.String packageName, java.lang.String className, java.lang.String method, java.lang.String msg)`
`static java.io.BufferedReader` `fileParser(java.lang.String pathName, 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` `getHexStringForDebug(byte[] b, int offset, int length)` Utility method to convert a byte array in a string made up of hex (0,.. 9, a,..f) and format the string with `0xCC` where CC is the string in HEX
`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 void` `pDebug(java.lang.String msg)`
`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 java.util.List<java.lang.String>` `readLine(java.io.InputStream in)` Reads lines of text from the given input stream.
`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` `threadWait(java.lang.Thread thread, long millis)` To be used to make a thread waiting for a certain amount of time
`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

#### DEBUG_MODE

    public static final boolean DEBUG_MODE

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

#### 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.

#### 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)

Parameters:
  `b` - The array of bytes to be convert in HEX characters.
Returns:
  A string representing the given array of byte in HEX values.

#### 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.

#### getHexStringForDebug

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

Utility method to convert a byte array in a string made up of hex (0,.. 9, a,..f) and format the string with `0xCC` where CC is the string in HEX

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.

#### threadWait

    public static void threadWait(java.lang.Thread thread,
                                  long millis)

To be used to make a thread waiting for a certain amount of time

Parameters:
  `thread` - The thread to be stopped
  `millis` - The amount of time in [ms] to stop the thread.

#### waitSomeTime

    public static void waitSomeTime(int ms)

To wait some times ...

#### 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.

#### 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. This method is The method is blocking !

Parameters:
  `in` - The input Stream where to read the data coming from.
  `len` - The number of bytes to be read from the input stream.
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 java.util.List<java.lang.String> readLine(java.io.InputStream in)
                                                     throws java.io.IOException

Reads lines of text from the given input stream. 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:
  A list of String, without any line-termination characters, corresponding to each line read from the input stream, or an empty list 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. It raises an IOException if something goes wrong. The method returns the amount of bytes sent.

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

#### fileParser

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

Method to use to access a file in your resources folder...

Parameters:
  `pathName` - folder's name (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)

#### DEBUG

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

#### DEBUG

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

#### pDebug

    public static void pDebug(java.lang.String msg)