Difference between revisions of "BinaryUtils"
Line 33: | Line 33: | ||
/** | /** | ||
− | * | + | * Converts sequences of characters (into an input string) representing an |
* escape sequence into the corresponding escape sequences | * escape sequence into the corresponding escape sequences | ||
* | * | ||
Line 96: | Line 96: | ||
/** | /** | ||
− | * | + | * Reads all data from a source InputStream and stores them into |
− | * a | + | * a byte array. |
− | * It is assumed that the source | + | * It is assumed that the source InputStream will be closed by |
* the caller of this method. | * the caller of this method. | ||
* | * | ||
* @param in | * @param in | ||
− | * the | + | * the InputStream to read from |
− | * @return the read bytes, stored into a | + | * @return the read bytes, stored into a byte array |
* @throws IOException | * @throws IOException | ||
* if any I/O error occurs | * if any I/O error occurs | ||
Line 110: | Line 110: | ||
/** | /** | ||
− | * | + | * Reads all data from a source InputStream and stores them into |
− | * a file on the local filesystem. | + | * a file on the local filesystem. |
− | * It is assumed that the source | + | * It is assumed that the source InputStream will be closed by |
* the caller of this method. | * the caller of this method. | ||
* | * | ||
* @param in | * @param in | ||
− | * the | + | * the InputStream to read from |
* @param filename | * @param filename | ||
* the name of the file to be written to | * the name of the file to be written to | ||
Line 125: | Line 125: | ||
/** | /** | ||
− | * | + | * Writes the content of a byte array into a new file on the |
* local filesystem. | * local filesystem. | ||
* | * | ||
* @param contentArray | * @param contentArray | ||
− | * the | + | * the byte array to be written to file |
* @param filename | * @param filename | ||
* the name of the file to be written to | * the name of the file to be written to | ||
Line 138: | Line 138: | ||
/** | /** | ||
− | * | + | * Writes the content of a byte array into a file on the local filesystem. |
− | |||
* | * | ||
* @param contentArray | * @param contentArray | ||
− | * the | + | * the byte array to be written to file |
* @param filename | * @param filename | ||
* the name of the file to be written to | * the name of the file to be written to | ||
Line 153: | Line 152: | ||
/** | /** | ||
− | * | + | * Writes the content of a byte array into a new file on the |
* local filesystem. | * local filesystem. | ||
* | * | ||
* @param contentArray | * @param contentArray | ||
− | * the | + | * the byte array to be written to file |
* @param file | * @param file | ||
* the file to be written to | * the file to be written to | ||
Line 166: | Line 165: | ||
/** | /** | ||
− | * | + | * Writes the content of a byte array into a file on the local |
* filesystem. | * filesystem. | ||
* | * | ||
* @param contentArray | * @param contentArray | ||
− | * the | + | * the byte array to be written to file |
* @param file | * @param file | ||
* the file to be written to | * the file to be written to | ||
Line 179: | Line 178: | ||
/** | /** | ||
− | * Returns | + | * Returns true if the passed byte value is an |
− | * ASCII printable character, | + | * ASCII printable character, false otherwise. |
* | * | ||
* @param b | * @param b | ||
− | * the passed | + | * the passed byte value |
− | * @return | + | * @return true if the passed byte value is an |
− | * ASCII printable character, | + | * ASCII printable character, false otherwise. |
*/ | */ | ||
public static boolean isASCIIPrintableChar(byte b) | public static boolean isASCIIPrintableChar(byte b) | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 13:26, 18 December 2012
Class FQN: it.greenvulcano.util.bin.BinaryUtils
Following the BinaryUtils public static methods
/**
* Dumps the content of a byte array as a sequence of integers
*
* @param arr
* the array to convert
* @return the converted value
*/
public static String dumpByteArrayAsInts(byte[] arr)
/**
* Dumps the content of a byte array as a sequence of integers (Hex format)
*
* @param arr
* The byte array
* @return The String conversion of the buffer bytes as integer values in
* 2-Hex-digits output format
*/
public static String dumpByteArrayAsHexInts(byte[] arr)
/**
* Dumps the Hex formatted sequence as a byte array
*
* @param hexArr
* The 2-Hex-digits string to convert
* @return the byte array, or an empy array if input is malformed
*/
public static byte[] dumpHexIntsAsByteArray(String hexArr)
/**
* Converts sequences of characters (into an input string) representing an
* escape sequence into the corresponding escape sequences
*
* @param input
* the input string
* @return the converted string
*/
public static String unescapeString(String input)
/**
* This method tokenizes a given string using a given byte sequence as
* separator and returns a List containing found tokens. The
* returned List is NEVER null (it may have zero components,
* anyway).
*
* @param theString
* the String to be tokenized.
* @param separatorByteSequence
* the byte sequence separator between tokens.
* @return a List containing found tokens.
*/
public static List<String> splitByByteSequenceSeparator(String theString, byte[] separatorByteSequence)
/**
* Loads the full content of a file.
* The file must be reachable via the parent classloader, or at least
* via the system classloader.
* The full content of a file is then stored into a byte array.
*
* @param filename
* the name of the file to be read
* @return the full content of the file, stored into a byte
* array
* @throws IOException
* if any I/O error occurs
*/
public static byte[] readFileAsBytesFromCP(String filename) throws IOException
/**
* Loads the full content of a file into a <code>byte</code> array.<br>
*
* @param filename
* the name of the file to be read
* @return the full content of the file, stored into a <code>byte</code>
* array
* @throws IOException
* if any I/O error occurs
*/
public static byte[] readFileAsBytes(String filename) throws IOException
/**
* Loads the full content of a file into a <code>byte</code> array.<br>
*
* @param file
* the file to be read
* @return the full content of the file, stored into a <code>byte</code>
* array
* @throws IOException
* if any I/O error occurs
*/
public static byte[] readFileAsBytes(File file) throws IOException
/**
* Reads all data from a source InputStream and stores them into
* a byte array.
* It is assumed that the source InputStream will be closed by
* the caller of this method.
*
* @param in
* the InputStream to read from
* @return the read bytes, stored into a byte array
* @throws IOException
* if any I/O error occurs
*/
public static byte[] inputStreamToBytes(InputStream in) throws IOException
/**
* Reads all data from a source InputStream and stores them into
* a file on the local filesystem.
* It is assumed that the source InputStream will be closed by
* the caller of this method.
*
* @param in
* the InputStream to read from
* @param filename
* the name of the file to be written to
* @throws IOException
* if any I/O error occurs
*/
public static void inputStreamToFile(InputStream in, String filename) throws IOException
/**
* Writes the content of a byte array into a new file on the
* local filesystem.
*
* @param contentArray
* the byte array to be written to file
* @param filename
* the name of the file to be written to
* @throws IOException
* if any I/O error occurs
*/
public static void writeBytesToFile(byte[] contentArray, String filename) throws IOException
/**
* Writes the content of a byte array into a file on the local filesystem.
*
* @param contentArray
* the byte array to be written to file
* @param filename
* the name of the file to be written to
* @param append
* If true the data are appended to existent file
* @throws IOException
* if any I/O error occurs
*/
public static void writeBytesToFile(byte[] contentArray, String filename, boolean append) throws IOException
/**
* Writes the content of a byte array into a new file on the
* local filesystem.
*
* @param contentArray
* the byte array to be written to file
* @param file
* the file to be written to
* @throws IOException
* if any I/O error occurs
*/
public static void writeBytesToFile(byte[] contentArray, File file) throws IOException
/**
* Writes the content of a byte array into a file on the local
* filesystem.
*
* @param contentArray
* the byte array to be written to file
* @param file
* the file to be written to
* @throws IOException
* if any I/O error occurs
*/
public static void writeBytesToFile(byte[] contentArray, File file, boolean append) throws IOException
/**
* Returns true if the passed byte value is an
* ASCII printable character, false otherwise.
*
* @param b
* the passed byte value
* @return true if the passed byte value is an
* ASCII printable character, false otherwise.
*/
public static boolean isASCIIPrintableChar(byte b)