Difference between revisions of "BinaryUtils"

From GreenVulcano Wiki
Jump to: navigation, search
 
(3 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.
  
==Following the BinaryUtils public static methods==
+
==BinaryUtils public static methods==
 
 
 
<syntaxhighlight lang="java5">
 
<syntaxhighlight lang="java5">
 
/**
 
/**
Line 33: Line 33:
  
 
/**
 
/**
* Convert sequences of characters (into an input string) representing an
+
* 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 72: Line 72:
  
 
/**
 
/**
* Loads the full content of a file into a <code>byte</code> array.<br>
+
* 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 <code>byte</code> array.<br>
+
* 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 <code>byte</code>
+
* @return the full content of the file, stored into a byte
 
*        array
 
*        array
 
* @throws IOException
 
* @throws IOException
Line 96: Line 96:
  
 
/**
 
/**
* Read all data from a source <code>InputStream</code> and stores them into
+
* Reads all data from a source InputStream and stores them into
* a <code>byte</code> array.<br>
+
* a byte array.
* It is assumed that the source <code>InputStream</code> will be closed by
+
* 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 <code>InputStream</code> to read from
+
*        the InputStream to read from
* @return the read bytes, stored into a <code>byte</code> array
+
* @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:
  
 
/**
 
/**
* Read all data from a source <code>InputStream</code> and stores them into
+
* Reads all data from a source InputStream and stores them into
* a file on the local filesystem.<br>
+
* a file on the local filesystem.
* It is assumed that the source <code>InputStream</code> will be closed by
+
* 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 <code>InputStream</code> to read from
+
*        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:
  
 
/**
 
/**
* Write the content of a <code>byte</code> array into a new file on the
+
* Writes the content of a byte array into a new file on the local filesystem.
* local filesystem.
 
 
*  
 
*  
 
* @param contentArray
 
* @param contentArray
*        the <code>byte</code> array to be written to file
+
*        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:
  
 
/**
 
/**
* Write the content of a <code>byte</code> array into a file on the local
+
* Writes the content of a byte array into a file on the local filesystem.
* filesystem.
 
 
*  
 
*  
 
* @param contentArray
 
* @param contentArray
*        the <code>byte</code> array to be written to file
+
*        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:
  
 
/**
 
/**
* Write the content of a <code>byte</code> array into a new file on the
+
* Writes the content of a byte array into a new file on the local filesystem.
* local filesystem.
 
 
*  
 
*  
 
* @param contentArray
 
* @param contentArray
*        the <code>byte</code> array to be written to file
+
*        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:
  
 
/**
 
/**
* Write the content of a <code>byte</code> array into a file on the local
+
* Writes the content of a byte array into a file on the local filesystem.
* filesystem.
 
 
*  
 
*  
 
* @param contentArray
 
* @param contentArray
*        the <code>byte</code> array to be written to file
+
*        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 <code>true</code> if the passed <code>byte</code> value is an
+
* Returns true if the passed byte value is an
* ASCII printable character, <code>false</code> otherwise.
+
* ASCII printable character, false otherwise.
 
*  
 
*  
 
* @param b
 
* @param b
*        the passed <code>byte</code> value
+
*        the passed byte value
* @return <code>true</code> if the passed <code>byte</code> value is an
+
* @return true if the passed byte value is an
*        ASCII printable character, <code>false</code> otherwise.
+
*        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)