public class Utility
extends java.lang.Object
Modifier and Type | Field and Description |
---|---|
static java.util.Random |
rnd
Object to get some random values...
|
static int |
TCP_BUFFER_SIZE
Default size for the TCP input stream
|
Constructor and Description |
---|
Utility() |
Modifier and Type | Method and Description |
---|---|
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.
|
public static final int TCP_BUFFER_SIZE
public static java.util.Random rnd
public static byte[] calculateCRC(byte[] data, int offset, int len)
data
- a byte array containing the data to sendoffset
- the offsetlen
- the data lengthpublic static boolean checkCRC(byte[] data, int offset, int len, byte[] crc)
data
- a byte array containing the data to sendoffset
- the offsetlen
- the data lengthcrc
- a byte array containing the CRC to checkpublic static int unsignedByteToSignedInt(byte from)
from
- an unsigned byte to convert to a signed integerpublic static java.lang.String getHexString(byte[] b)
b
- Array of bytes to be converted in HEX string.public static java.lang.String getHexString(byte[] b, int offset, int length)
b
- The byte array to convert in HEX stringoffset
- The index where we start to convert from.length
- The amount of bytes to convert in HEX stringpublic static java.lang.String getStringRndVal(int factor)
factor
- A coefficient which the random value is multiplied with.public static byte[] readBytes(java.io.InputStream in) throws java.io.IOException
in
- The input Stream where to read the data coming from.array of bytes
. Or null if the has been closed by the peer while
waiting for incoming data.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.public static byte[] readNBytes(java.io.InputStream in, int len) throws java.io.IOException
in
- The input stream where to read the data coming from.len
- The amount of data to readarray of bytes
.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.public static byte[] readLine(java.io.InputStream in) throws java.io.IOException
in
- The Input Stream to read from.java.io.IOException
- If an I/O error occurspublic static void writeLine(java.io.OutputStream out, byte[] toSend) throws java.io.IOException
out
- The Output Stream to send the data to.toSend
- The data to sendjava.io.IOException
- If an I/O error occurspublic static void sendBytes(java.io.OutputStream out, byte[] toSend, int offset, int length) throws java.io.IOException
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.out
- The Output Stream to send the data to.toSend
- The data to sendoffset
- The starting position in the buffer of datalength
- The amount of bytes to be sent.java.io.IOException
- If an I/O error occurspublic static void waitSomeTime(int ms)
ms
- The amount of time to wait expressed in milli-seconds [ms].public static java.lang.String md5sum(java.lang.String msg)
msg
- The input string to processpublic static java.lang.Float bytesToFloat(byte[] bytes, int offset, int size)
float
value from array of bytes. This byte's array can only be 2 or 4 bytes long.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.Float
value or null
if the process failed.public static java.io.BufferedReader fileParser(java.lang.String path, java.lang.String fileName)
path
- folder's hierarchy (if exists) from `resources`fileName
- Name of the file to accessBufferedReader
related to the file you want to work with, or null if the file could not
be reached.public static void DEBUG(java.lang.String className, java.lang.String method, java.lang.String msg)