Class IOUtils

java.lang.Object
com.sshtools.common.util.IOUtils

public class IOUtils extends Object
Author:
$author$
  • Field Details

    • BUFFER_SIZE

      public static int BUFFER_SIZE
      Default buffer size for stream utility methods
  • Constructor Details

    • IOUtils

      public IOUtils()
  • Method Details

    • copy

      public static void copy(InputStream in, OutputStream out) throws IOException
      Copy from an input stream to an output stream. It is up to the caller to close the streams.
      Parameters:
      in - input stream
      out - output stream
      Throws:
      IOException - on any error
    • copy

      public static void copy(InputStream in, OutputStream out, boolean forceFlush) throws IOException
      Copy from an input stream to an output stream. It is up to the caller to close the streams.
      Parameters:
      in - input stream
      out - output stream
      forceFlush - force flush of the OutputStream on each block
      Throws:
      IOException - on any error
    • copy

      public static void copy(InputStream in, OutputStream out, long count) throws IOException
      Copy the specified number of bytes from an input stream to an output stream. It is up to the caller to close the streams.
      Parameters:
      in - input stream
      out - output stream
      count - number of bytes to copy
      Throws:
      IOException - on any error
    • copy

      public static void copy(InputStream in, OutputStream out, long count, int bufferSize, boolean forceFlush) throws IOException
      Copy the specified number of bytes from an input stream to an output stream. It is up to the caller to close the streams.
      Parameters:
      in - input stream
      out - output stream
      count - number of bytes to copy
      bufferSize - buffer size
      Throws:
      IOException - on any error
    • copyWithCount

      public static long copyWithCount(InputStream in, OutputStream out) throws IOException
      Copy from an input stream to an output stream. It is up to the caller to close the streams.
      Parameters:
      in - input stream
      out - output stream
      Throws:
      IOException - on any error
    • copyWithCount

      public static long copyWithCount(InputStream in, OutputStream out, boolean forceFlush) throws IOException
      Copy from an input stream to an output stream. It is up to the caller to close the streams.
      Parameters:
      in - input stream
      out - output stream
      forceFlush - force flush of the OutputStream on each block
      Throws:
      IOException - on any error
    • copyWithCount

      public static long copyWithCount(InputStream in, OutputStream out, long count) throws IOException
      Copy the specified number of bytes from an input stream to an output stream. It is up to the caller to close the streams.
      Parameters:
      in - input stream
      out - output stream
      count - number of bytes to copy
      Throws:
      IOException - on any error
    • copyWithCount

      public static long copyWithCount(InputStream in, OutputStream out, long count, int bufferSize, boolean forceFlush) throws IOException
      Copy the specified number of bytes from an input stream to an output stream. It is up to the caller to close the streams.
      Parameters:
      in - input stream
      out - output stream
      count - number of bytes to copy
      bufferSize - buffer size
      Throws:
      IOException - on any error
    • closeStream

      public static boolean closeStream(InputStream in)
      Parameters:
      in -
      Returns:
    • closeStream

      public static boolean closeStream(OutputStream out)
      Parameters:
      out -
      Returns:
    • recursiveContentsDelete

      public static void recursiveContentsDelete(File directory, FileVisitOption... options)
      Recursively delete all contents of the given directory. By default symbolic links will not be followed. The directory itself will NOT be deleted.
      Parameters:
      directory - directory containing files or directories
      options - options
      Throws:
      UncheckedIOException - on any error
    • recursiveContentsDelete

      public static void recursiveContentsDelete(Path directory, FileVisitOption... options)
      Recursively delete all contents of the given directory. By default symbolic links will not be followed. The directory itself will NOT be deleted.
      Parameters:
      directory - directory containing files or directories
      options - options
      Throws:
      UncheckedIOException - on any error
    • silentRecursiveDelete

      public static boolean silentRecursiveDelete(File fileOrDirectory, FileVisitOption... options)
      Recursively delete a file or directory, and all child files and directories if a directory. By default symbolic links will not be followed. An exception will NOT be thrown on error, instead false will be returned.
      Parameters:
      fileOrDirectory - directory containing files or directories
      options - options
      Returns:
      success
    • silentRecursiveDelete

      public static boolean silentRecursiveDelete(Path fileOrDirectory, FileVisitOption... options)
      Recursively delete a file or directory, and all child files and directories if a directory. By default symbolic links will not be followed. An exception will NOT be thrown on error, instead false will be returned.
      Parameters:
      fileOrDirectory - directory containing files or directories
      options - options
      Returns:
      success
    • recursiveDelete

      public static void recursiveDelete(Path fileOrDirectory, FileVisitOption... options)
      Recursively delete a file or directory, and all child files and directories if a directory. By default symbolic links will not be followed.
      Parameters:
      fileOrDirectory - directory containing files or directories
      options - options
      Throws:
      UncheckedIOException - on any error
    • delTree

      @Deprecated(since="3.1.0", forRemoval=true) public static boolean delTree(File file)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Deprecated and unsafe (follows symbolic links), use #recursiveDelete(File, FileVisitOption...).
      Parameters:
      file - file or directory to completely remove.
      Returns:
      success
    • recurseDeleteDirectory

      @Deprecated(since="3.1.0", forRemoval=true) public static void recurseDeleteDirectory(File dir)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Deprecated and unsafe (follows symbolic links), use recursiveContentsDelete(Path, FileVisitOption...).
      Parameters:
      dir - directory of contents to remove.
    • copyFile

      public static void copyFile(File from, File to) throws IOException
      Throws:
      IOException
    • readyFully

      public static int readyFully(InputStream in, byte[] buf) throws IOException
      Throws:
      IOException
    • writeUTF8StringToStream

      public static void writeUTF8StringToStream(OutputStream out, String string) throws UnsupportedEncodingException, IOException
      Throws:
      UnsupportedEncodingException
      IOException
    • writeStringToStream

      public static void writeStringToStream(OutputStream out, String string, String charset) throws UnsupportedEncodingException, IOException
      Throws:
      UnsupportedEncodingException
      IOException
    • writeUTF8StringToFile

      public static void writeUTF8StringToFile(File file, String string) throws UnsupportedEncodingException, IOException
      Throws:
      UnsupportedEncodingException
      IOException
    • writeStringToFile

      public static void writeStringToFile(File file, String string, String charset) throws UnsupportedEncodingException, IOException
      Throws:
      UnsupportedEncodingException
      IOException
    • fromByteSize

      public static Long fromByteSize(String val)
    • toByteSize

      public static String toByteSize(double t)
    • toByteSize

      public static String toByteSize(double t, int decimalPlaces)
    • sha1Digest

      public static byte[] sha1Digest(File file) throws NoSuchAlgorithmException, FileNotFoundException, IOException
      Throws:
      NoSuchAlgorithmException
      FileNotFoundException
      IOException
    • sha1Digest

      public static byte[] sha1Digest(InputStream in) throws NoSuchAlgorithmException, IOException
      Throws:
      NoSuchAlgorithmException
      IOException
    • closeStream

      public static void closeStream(Closeable obj)
    • getFilenameExtension

      public static String getFilenameExtension(String filename)
    • getFilenameWithoutExtension

      public static String getFilenameWithoutExtension(String filename)
    • rollover

      public static void rollover(File logFile, int maxFiles) throws IOException
      Throws:
      IOException
    • readUTF8StringFromFile

      public static String readUTF8StringFromFile(File file) throws IOException
      Throws:
      IOException
    • readUTF8StringFromStream

      public static String readUTF8StringFromStream(InputStream in) throws IOException
      Throws:
      IOException
    • readStringFromFile

      public static String readStringFromFile(File file, String charset) throws UnsupportedEncodingException, IOException
      Throws:
      UnsupportedEncodingException
      IOException
    • readStringFromStream

      public static String readStringFromStream(InputStream in, String charset) throws IOException
      Throws:
      IOException
    • toInputStream

      public static InputStream toInputStream(String value, String charset) throws IOException
      Throws:
      IOException
    • writeBytesToFile

      public static void writeBytesToFile(byte[] value, File file) throws IOException
      Throws:
      IOException
    • copy

      public static void copy(File file, OutputStream out) throws IOException
      Throws:
      IOException
    • isOlderThan

      public static boolean isOlderThan(File file, Duration duration)