Class IO

java.lang.Object
org.eclipse.jetty.util.IO

public class IO extends Object
IO Utilities. Provides stream handling utilities in singleton Threadpool implementation accessed by static members.
  • Field Details

  • Method Details

    • copy

      public static void copy(InputStream in, OutputStream out) throws IOException
      Copy Stream in to Stream out until EOF or exception.
      Parameters:
      in - the input stream to read from (until EOF)
      out - the output stream to write to
      Throws:
      IOException - if unable to copy streams
    • copy

      public static void copy(Reader in, Writer out) throws IOException
      Copy Reader to Writer out until EOF or exception.
      Parameters:
      in - the read to read from (until EOF)
      out - the writer to write to
      Throws:
      IOException - if unable to copy the streams
    • copy

      public static void copy(InputStream in, OutputStream out, long byteCount) throws IOException
      Copy Stream in to Stream for byteCount bytes or until EOF or exception.
      Parameters:
      in - the stream to read from
      out - the stream to write to
      byteCount - the number of bytes to copy
      Throws:
      IOException - if unable to copy the streams
    • copy

      public static void copy(Reader in, Writer out, long byteCount) throws IOException
      Copy Reader to Writer for byteCount bytes or until EOF or exception.
      Parameters:
      in - the Reader to read from
      out - the Writer to write to
      byteCount - the number of bytes to copy
      Throws:
      IOException - if unable to copy streams
    • copy

      public static void copy(File from, File to) throws IOException
      Copy files or directories
      Parameters:
      from - the file to copy
      to - the destination to copy to
      Throws:
      IOException - if unable to copy
    • copyDir

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

      public static void copyDir(Path srcDir, Path destDir) throws IOException
      Copy the contents of a source directory to destination directory.

      This version does not use the standard Files.copy(Path, Path, CopyOption...) technique to copy files, as that technique might incur a "foreign target" behavior when the FileSystem types of the srcDir and destDir are different. Instead, this implementation uses the copyFile(Path, Path) method instead.

      Parameters:
      srcDir - the source directory
      destDir - the destination directory
      Throws:
      IOException - if unable to copy the file
    • copyDir

      @Deprecated(since="12.0.8", forRemoval=true) public static void copyDir(Path srcDir, Path destDir, CopyOption... copyOptions) throws IOException
      Deprecated, for removal: This API element is subject to removal in a future version.
      use copyDir(Path, Path) instead to avoid foreign target behavior across FileSystems.
      Copy the contents of a source directory to destination directory.

      Copy the contents of srcDir to the destDir using Files.copy(Path, Path, CopyOption...) to copy individual files.

      Parameters:
      srcDir - the source directory
      destDir - the destination directory (must exist)
      copyOptions - the options to use on the Files.copy(Path, Path, CopyOption...) commands.
      Throws:
      IOException - if unable to copy the file
    • resolvePath

      public static Path resolvePath(Path basePath, Path relative)
      Perform a resolve of a basePath Path against a relative Path in a way that ignores FileSystem differences between the two Path parameters.

      This implementation is intended to be a replacement for Path.resolve(Path) in cases where the the FileSystem might be different, avoiding a ProviderMismatchException from occurring.

      Parameters:
      basePath - the base Path
      relative - the relative Path to resolve against base Path
      Returns:
      the new Path object relative to the base Path
    • ensureDirExists

      public static void ensureDirExists(Path dir) throws IOException
      Ensure that the given path exists, and is a directory.

      Uses Files.createDirectories(Path, FileAttribute[]) when the provided path needs to be created as directories.

      Parameters:
      dir - the directory to check and/or create.
      Throws:
      IOException - if the dir exists, but isn't a directory, or if unable to create the directory.
    • copyFile

      public static void copyFile(Path srcFile, Path destFile) throws IOException
      Copy the contents of a source file to destination file.

      Copy the contents of srcFile to the destFile using Files.copy(Path, OutputStream). The destFile is opened with the OpenOption of StandardOpenOption.CREATE,StandardOpenOption.WRITE,StandardOpenOption.TRUNCATE_EXISTING.

      Unlike Files.copy(Path, Path, CopyOption...), this implementation will not perform a "foreign target" behavior (a special mode that kicks in when the srcFile and destFile are on different FileSystems) which will attempt to delete the destination file before creating a new file and then copying the contents over.

      In this implementation if the file exists, it will just be opened and written to from the start of the file.

      Parameters:
      srcFile - the source file (must exist)
      destFile - the destination file
      Throws:
      IOException - if unable to copy the file
    • copyFile

      public static void copyFile(File from, File to) throws IOException
      Copy the contents of a source file to destination file.

      Copy the contents of from File to the to File using standard InputStream / OutputStream behaviors.

      Parameters:
      from - the source file (must exist)
      to - the destination file
      Throws:
      IOException - if unable to copy the file
    • rethrow

      public static IOException rethrow(Throwable cause)
    • toString

      public static String toString(Path path, Charset charset) throws IOException
      Read Path to string.
      Parameters:
      path - the path to read from (until EOF)
      charset - the charset to read with
      Returns:
      the String parsed from path (default Charset)
      Throws:
      IOException - if unable to read the path (or handle the charset)
    • toString

      public static String toString(InputStream in) throws IOException
      Read input stream to string.
      Parameters:
      in - the stream to read from (until EOF)
      Returns:
      the String parsed from stream (default Charset)
      Throws:
      IOException - if unable to read the stream (or handle the charset)
    • toString

      public static String toString(InputStream in, String encoding) throws IOException
      Read input stream to string.
      Parameters:
      in - the stream to read from (until EOF)
      encoding - the encoding to use (can be null to use default Charset)
      Returns:
      the String parsed from the stream
      Throws:
      IOException - if unable to read the stream (or handle the charset)
    • toString

      public static String toString(InputStream in, Charset encoding) throws IOException
      Read input stream to string.
      Parameters:
      in - the stream to read from (until EOF)
      encoding - the Charset to use (can be null to use default Charset)
      Returns:
      the String parsed from the stream
      Throws:
      IOException - if unable to read the stream (or handle the charset)
    • toString

      public static String toString(Reader in) throws IOException
      Read input stream to string.
      Parameters:
      in - the reader to read from (until EOF)
      Returns:
      the String parsed from the reader
      Throws:
      IOException - if unable to read the stream (or handle the charset)
    • delete

      public static boolean delete(File file)
      Delete File. This delete will recursively delete directories - BE CAREFUL
      Parameters:
      file - The file (or directory) to be deleted.
      Returns:
      true if file was deleted, or directory referenced was deleted. false if file doesn't exist, or was null.
    • delete

      public static boolean delete(Path path)
      Delete the path, recursively.
      Parameters:
      path - the path to delete
      Returns:
      true if able to delete the path, false if unable to delete the path.
    • isEmptyDir

      public static boolean isEmptyDir(File dir)
      Test if directory is empty.
      Parameters:
      dir - the directory
      Returns:
      true if directory is null, doesn't exist, or has no content. false if not a directory, or has contents
    • close

      public static void close(AutoCloseable closeable)
      Closes an arbitrary closable, and logs exceptions at ignore level
      Parameters:
      closeable - the closeable to close
    • close

      public static void close(Closeable closeable)
      Closes an arbitrary closable, and logs exceptions at ignore level
      Parameters:
      closeable - the closeable to close
    • close

      public static void close(InputStream is)
      closes an input stream, and logs exceptions
      Parameters:
      is - the input stream to close
    • close

      public static void close(OutputStream os)
      closes an output stream, and logs exceptions
      Parameters:
      os - the output stream to close
    • close

      public static void close(Reader reader)
      closes a reader, and logs exceptions
      Parameters:
      reader - the reader to close
    • close

      public static void close(Writer writer)
      closes a writer, and logs exceptions
      Parameters:
      writer - the writer to close
    • readBytes

      public static byte[] readBytes(InputStream in) throws IOException
      Throws:
      IOException
    • write

      public static long write(GatheringByteChannel out, ByteBuffer[] buffers, int offset, int length) throws IOException
      A gathering write utility wrapper.

      This method wraps a gather write with a loop that handles the limitations of some operating systems that have a limit on the number of buffers written. The method loops on the write until either all the content is written or no progress is made.

      Parameters:
      out - The GatheringByteChannel to write to
      buffers - The buffers to write
      offset - The offset into the buffers array
      length - The length in buffers to write
      Returns:
      The total bytes written
      Throws:
      IOException - if unable write to the GatheringByteChannel
    • asFile

      public static File asFile(Object fileObject)

      Convert an object to a File if possible.

      Parameters:
      fileObject - A File, String, Path or null to be converted into a File
      Returns:
      A File representation of the passed argument or null.