Difference between revisions of "BinaryUtils"
(Created page with "Class FQN: '''it.greenvulcano.util.bin.BinaryUtils''' ==Following the BinaryUtils public static methods== <syntaxhighlight lang="java5"> /** * Dumps the content of a byte array...") |
|||
(4 intermediate revisions by one other user not shown) | |||
Line 1: | Line 1: | ||
Class FQN: '''it.greenvulcano.util.bin.BinaryUtils''' | Class FQN: '''it.greenvulcano.util.bin.BinaryUtils''' | ||
+ | This class contains static utility methods to manage binary buffers. | ||
− | == | + | ==BinaryUtils public static methods== |
− | |||
<syntaxhighlight lang="java5"> | <syntaxhighlight lang="java5"> | ||
/** | /** | ||
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 44: | Line 44: | ||
/** | /** | ||
* This method tokenizes a given string using a given byte sequence as | * This method tokenizes a given string using a given byte sequence as | ||
− | * separator and returns a | + | * separator and returns a List containing found tokens. The |
− | * returned | + | * returned List is NEVER null (it may have zero components, |
* anyway). | * anyway). | ||
* | * | ||
* @param theString | * @param theString | ||
− | * the | + | * the String to be tokenized. |
* @param separatorByteSequence | * @param separatorByteSequence | ||
* the byte sequence separator between tokens. | * the byte sequence separator between tokens. | ||
− | * @return a | + | * @return a List containing found tokens. |
*/ | */ | ||
public static List<String> splitByByteSequenceSeparator(String theString, byte[] separatorByteSequence) | public static List<String> splitByByteSequenceSeparator(String theString, byte[] separatorByteSequence) | ||
/** | /** | ||
− | * Loads the full content of a file. | + | * Loads the full content of a file. |
− | * The file must be reachable via the | + | * The file must be reachable via the parent classloader, or at least |
− | * via the | + | * via the system classloader. |
− | * The full content of a file is then stored into a | + | * The full content of a file is then stored into a byte array. |
* | * | ||
* @param filename | * @param filename | ||
* the name of the file to be read | * the name of the file to be read | ||
− | * @return the full content of the file, stored into a | + | * @return the full content of the file, stored into a byte |
* array | * array | ||
* @throws IOException | * @throws IOException | ||
Line 72: | Line 72: | ||
/** | /** | ||
− | * Loads the full content of a file into a | + | * Loads the full content of a file into a byte array. |
* | * | ||
* @param filename | * @param filename | ||
Line 84: | Line 84: | ||
/** | /** | ||
− | * Loads the full content of a file into a | + | * Loads the full content of a file into a byte array. |
* | * | ||
* @param file | * @param file | ||
* the file to be read | * the file to be read | ||
− | * @return the full content of the file, stored into a | + | * @return the full content of the file, stored into a byte |
* array | * array | ||
* @throws IOException | * @throws IOException | ||
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. |
− | |||
* | * | ||
* @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 137: | ||
/** | /** | ||
− | * | + | * 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 151: | ||
/** | /** | ||
− | * | + | * Writes the content of a byte array into a new file on the 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 163: | ||
/** | /** | ||
− | * | + | * 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 file | * @param file | ||
* the file to be written to | * the file to be written to | ||
Line 179: | Line 175: | ||
/** | /** | ||
− | * 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> |
Latest revision as of 17:54, 21 December 2012
Class FQN: it.greenvulcano.util.bin.BinaryUtils This class contains static utility methods to manage binary buffers.
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 byte array.
*
* @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 byte array.
*
* @param file
* 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[] 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)