Class IOUtil

java.lang.Object
org.jlab.jlog.util.IOUtil

public final class IOUtil extends Object
IO Utilities.
  • Method Details

    • closeQuietly

      public static void closeQuietly(Closeable c)
      Closes a Closeable without generating any checked Exceptions. If an IOException does occur while closing it is logged as a WARNING.
      Parameters:
      c - The Closeable
    • deleteQuietly

      public static void deleteQuietly(File file)
      Deletes a file without generating any checked Exceptions. If unable to delete then a WARNING will be logged.
      Parameters:
      file - The file
    • fileToBytes

      public static byte[] fileToBytes(File file) throws IOException
      Fully reads in a file and returns an array of the bytes representing the file. Be careful reading in large files because they may result in an OutOfMemoryError. This method uses the File length to efficiently allocate memory.
      Parameters:
      file - The file to load into memory.
      Returns:
      The bytes
      Throws:
      IOException - If an error occurs reading in the file.
    • copy

      public static long copy(InputStream in, OutputStream out) throws IOException
      Copies all of the bytes from the InputStream into the OutputStream using a buffer of 4096 bytes.
      Parameters:
      in - The InputStream
      out - The OutputStream
      Returns:
      The number of bytes copied
      Throws:
      IOException - If unable to copy
    • streamToString

      public static String streamToString(InputStream is, String encoding)
      Reads in an InputStream fully and returns the result as a String.
      Parameters:
      is - The InputStream
      encoding - The character encoding of the String
      Returns:
      The String representation of the data
    • arrayToCSV

      public static String arrayToCSV(String[] values)
      Converts an array of String values to a comma-separated-values String.
      Parameters:
      values - The array
      Returns:
      The comma-separated-values
    • csvToArray

      public static String[] csvToArray(String values)
      Converts a comma-separated-values String to an array of String values. Whitespace on either side of commas are trimmed off.
      Parameters:
      values - The comma-separated-values
      Returns:
      The array of String values
    • encodeBase64

      public static String encodeBase64(byte[] data)
      Encodes an array of bytes to base64.
      Parameters:
      data - The bytes
      Returns:
      A base64 encoded String
    • decodeBase64

      public static byte[] decodeBase64(String data)
      Decodes a base64 String to an array of bytes.
      Parameters:
      data - The base64 encoded String
      Returns:
      The bytes