Class IOUtils

java.lang.Object
org.apache.juneau.common.internal.IOUtils

public final class IOUtils extends Object
Various I/O related utility methods.
  • Field Details

  • Constructor Details

  • Method Details

    • pipe

      public static long pipe(Reader in, File out) throws IOException
      Pipes the contents of the specified Reader to the specified file.
      Parameters:
      in - The reader to pipe from.
      Can be null.
      Reader is automatically closed.
      out - The file to write the output to.
      Can be null.
      Returns:
      The number of characters piped.
      Throws:
      IOException - Thrown by underlying stream.
    • pipe

      public static long pipe(Reader in, Writer out) throws IOException
      Pipes the contents of the specified Reader to the specified Writer.
      Parameters:
      in - The reader to pipe from.
      Can be null.
      Reader is automatically closed.
      out - The file to write the output to.
      Can be null.
      Writer is flushed but not automatically closed.
      Returns:
      The number of characters piped.
      Throws:
      IOException - Thrown by underlying stream.
    • pipe

      public static long pipe(Reader in, Writer out, Consumer<IOException> onException)
      Pipes the contents of the specified Reader to the specified Writer.
      Parameters:
      in - The reader to pipe from.
      Can be null.
      Reader is automatically closed.
      out - The file to write the output to.
      Can be null.
      Writer is flushed but not automatically closed.
      onException - Consumer of any I/O exceptions.
      Returns:
      The number of characters piped.
    • pipeLines

      public static long pipeLines(Reader in, Writer out) throws IOException
      Pipes the contents of the specified Reader to the specified Writer a line at a time.

      Writer is flushed after every line. Typically useful when writing to consoles.

      Parameters:
      in - The reader to pipe from.
      Can be null.
      Reader is automatically closed.
      out - The file to write the output to.
      Can be null.
      Writer is flushed but not automatically closed.
      Returns:
      The number of characters piped.
      Throws:
      IOException - Thrown by underlying stream.
    • pipe

      public static long pipe(InputStream in, Writer out) throws IOException
      Pipes the contents of the specified input stream to the writer.
      Parameters:
      in - The stream to pipe from.
      Can be null.
      Streams is automatically closed.
      out - The writer to pipe to.
      Can be null.
      Stream is not automatically closed.
      Returns:
      The number of bytes written.
      Throws:
      IOException - Thrown by underlying stream.
    • pipe

      public static long pipe(InputStream in, Writer out, Consumer<IOException> onException)
      Pipes the contents of the specified input stream to the writer.
      Parameters:
      in - The stream to pipe from.
      Can be null.
      Streams is automatically closed.
      out - The writer to pipe to.
      Can be null.
      Stream is not automatically closed.
      onException - Consumer of any I/O exceptions.
      Returns:
      The number of bytes written.
    • pipe

      public static long pipe(InputStream in, OutputStream out) throws IOException
      Pipes the specified input stream to the specified output stream.

      Either stream is not automatically closed.

      Parameters:
      in - The input stream.
      Can be null.
      Stream is automatically closed.
      out - The output stream.
      Can be null.
      Stream is not automatically closed.
      Returns:
      The number of bytes written.
      Throws:
      IOException - If thrown from either stream.
    • pipe

      public static long pipe(InputStream in, OutputStream out, Consumer<IOException> onException)
      Pipes the specified input stream to the specified output stream.

      Either stream is not automatically closed.

      Parameters:
      in - The input stream.
      Can be null.
      Stream is automatically closed.
      out - The output stream.
      Can be null.
      Stream is not automatically closed.
      onException - Consumer of any I/O exceptions.
      Returns:
      The number of bytes written.
    • pipe

      public static long pipe(InputStream in, OutputStream out, long maxBytes) throws IOException
      Pipes the specified input stream to the specified output stream.

      Either stream is not automatically closed.

      Parameters:
      in - The input stream.
      Can be null.
      Stream is not automatically closed.
      out - The output stream.
      Can be null.
      Stream is not automatically closed.
      maxBytes - The maximum number of bytes or -1 to read the entire input stream.
      Returns:
      The number of bytes written.
      Throws:
      IOException - If thrown from either stream.
    • pipe

      public static long pipe(Reader in, OutputStream out) throws IOException
      Pipes the specified reader to the specified output stream.
      Parameters:
      in - The input reader.
      Can be null.
      Stream is automatically closed.
      out - The output stream.
      Can be null.
      Stream is not automatically closed.
      Returns:
      The number of bytes written.
      Throws:
      IOException - If thrown from output stream.
    • pipe

      public static long pipe(Reader in, OutputStream out, Consumer<IOException> onException)
      Pipes the specified reader to the specified output stream.
      Parameters:
      in - The input reader.
      Can be null.
      Stream is automatically closed.
      out - The output stream.
      Can be null.
      Stream is not automatically closed.
      onException - Consumer of any I/O exceptions.
      Returns:
      The number of bytes written.
    • pipe

      public static long pipe(byte[] in, OutputStream out, int maxBytes) throws IOException
      Pipes the specified byte array to the specified output stream.
      Parameters:
      in - The input byte array.
      Can be null.
      out - The output stream.
      Can be null.
      Stream is not automatically closed.
      maxBytes - The maximum number of bytes or -1 to read the entire byte array.
      Returns:
      The number of bytes written.
      Throws:
      IOException - If thrown from output stream.
    • read

      public static String read(Object in) throws IOException
      Pipes the specified object to the specified output stream.
      Parameters:
      in - The input byte array.
      Can be null or any of the following types:
      Returns:
      The input converted to a string.
      Throws:
      IOException - If thrown from output stream.
    • read

      public static String read(byte[] in)
      Reads the specified byte array containing UTF-8 into a string.
      Parameters:
      in - The input.
      Can be null.
      Returns:
      The new string, or null if the input was null.
    • read

      public static String read(byte[] in, Charset charset)
      Reads the specified byte array into a string.
      Parameters:
      in - The input.
      Can be null.
      charset - The character set to use for decoding.
      Returns:
      The new string, or null if the input was null.
    • read

      public static String read(File in) throws IOException
      Reads the contents of a file into a string.

      Assumes default character encoding.

      Parameters:
      in - The file to read.
      Can be null.
      Returns:
      The contents of the reader as a string, or null if file does not exist.
      Throws:
      IOException - If a problem occurred trying to read from the reader.
    • read

      public static String read(Reader in) throws IOException
      Reads the contents of a reader into a string.
      Parameters:
      in - The input reader.
      Can be null.
      Stream is automatically closed.
      Returns:
      The contents of the reader as a string, or null if the reader was null.
      Throws:
      IOException - If a problem occurred trying to read from the reader.
    • read

      public static String read(Reader in, Consumer<IOException> onException)
      Reads the contents of a reader into a string.
      Parameters:
      in - The input reader.
      Can be null.
      Stream is automatically closed.
      onException - Consumer of any I/O exceptions.
      Returns:
      The contents of the reader as a string, or null if the reader was null.
    • read

      public static String read(Reader in, long expectedLength) throws IOException
      Reads the specified input into a String until the end of the input is reached.
      Parameters:
      in - The input reader.
      Can be null.
      String is automatically closed.
      expectedLength - Specify a positive number if the length of the input is known, or -1 if unknown.
      Returns:
      The contents of the reader as a string, or null if the reader was null.
      Throws:
      IOException - If a problem occurred trying to read from the reader.
    • read

      public static String read(InputStream in) throws IOException
      Reads the contents of an input stream into a string.

      Assumes UTF-8 encoding.

      Parameters:
      in - The input stream.
      Can be null.
      Stream is automatically closed.
      Returns:
      The contents of the reader as a string, or null if the input stream was null.
      Throws:
      IOException - If a problem occurred trying to read from the input stream.
    • read

      public static String read(InputStream in, Consumer<IOException> onException)
      Reads the contents of an input stream into a string.

      Assumes UTF-8 encoding.

      Parameters:
      in - The input stream.
      Can be null.
      Stream is automatically closed.
      onException - Consumer of any I/O exceptions.
      Returns:
      The contents of the reader as a string, or null if the input stream was null.
    • read

      public static String read(InputStream in, Charset cs) throws IOException
      Reads the contents of an input stream into a string using the specified charset.
      Parameters:
      in - The input stream.
      Can be null.
      Stream is automatically closed.
      cs - The charset of the contents of the input stream.
      Returns:
      The contents of the reader as a string or null if input stream was null.
      Throws:
      IOException - If a problem occurred trying to read from the input stream.
    • read

      public static String read(InputStream in, Charset cs, Consumer<IOException> onException)
      Reads the contents of an input stream into a string using the specified charset.
      Parameters:
      in - The input stream.
      Can be null.
      Stream is automatically closed.
      cs - The charset of the contents of the input stream.
      onException - Consumer of any I/O exceptions.
      Returns:
      The contents of the reader as a string or null if input stream was null.
    • readBytes

      public static byte[] readBytes(InputStream in) throws IOException
      Reads the specified input stream into the specified byte array.
      Parameters:
      in - The input stream to read.
      Can be null.
      Stream is automatically closed.
      Returns:
      A byte array containing the contents. Never null.
      Throws:
      IOException - Thrown by underlying stream.
    • readBytes

      public static byte[] readBytes(InputStream in, int maxBytes) throws IOException
      Reads the specified input stream into the specified byte array.
      Parameters:
      in - The input stream to read.
      Can be null.
      Stream is not automatically closed.
      maxBytes - The maximum number of bytes or -1 to read the entire stream.
      Returns:
      A byte array containing the contents. Never null.
      Throws:
      IOException - Thrown by underlying stream.
    • readBytes

      public static byte[] readBytes(File in) throws IOException
      Read the specified file into a byte array.
      Parameters:
      in - The file to read into a byte array.
      Returns:
      The contents of the file as a byte array.
      Throws:
      IOException - Thrown by underlying stream.
    • readBytes

      public static byte[] readBytes(File in, int maxBytes) throws IOException
      Read the specified file into a byte array.
      Parameters:
      in - The file to read into a byte array.
      maxBytes - The maximum number of bytes to read, or -1 to read all bytes.
      Returns:
      The contents of the file as a byte array.
      Throws:
      IOException - Thrown by underlying stream.
    • readBytes

      public static byte[] readBytes(Reader in) throws IOException
      Reads the specified input stream into the specified byte array.
      Parameters:
      in - The input stream to read.
      Can be null.
      Stream is automatically closed.
      Returns:
      A byte array containing the contents. Never null.
      Throws:
      IOException - Thrown by underlying stream.
    • toBufferedReader

      public static Reader toBufferedReader(Reader r)
      Wraps the specified reader in a buffered reader.
      Parameters:
      r - The reader being wrapped.
      Returns:
      The reader wrapped in a BufferedReader, or the original Reader if it's already a buffered reader.
    • count

      public static long count(InputStream is) throws IOException
      Counts the number of bytes in the input stream and then closes the stream.
      Parameters:
      is - The input stream to read from.
      Returns:
      The number of bytes read.
      Throws:
      IOException - Thrown by underlying stream.
    • count

      public static long count(Reader r) throws IOException
      Counts the number of characters in the reader and then closes the reader.
      Parameters:
      r - The reader to read from.
      Returns:
      The number of characters read.
      Throws:
      IOException - Thrown by underlying stream.
    • closeQuietly

      public static void closeQuietly(InputStream is)
      Close input stream and ignore any exceptions.

      No-op if input stream is null.

      Parameters:
      is - The input stream to close.
    • closeQuietly

      public static void closeQuietly(OutputStream os)
      Close output stream and ignore any exceptions.

      No-op if output stream is null.

      Parameters:
      os - The output stream to close.
    • closeQuietly

      public static void closeQuietly(Reader r)
      Close reader and ignore any exceptions.

      No-op if reader is null.

      Parameters:
      r - The reader to close.
    • closeQuietly

      public static void closeQuietly(Writer w)
      Close writer and ignore any exceptions.

      No-op if writer is null.

      Parameters:
      w - The writer to close.
    • closeQuietly

      public static void closeQuietly(Object... o)
      Quietly close all specified input streams, output streams, readers, and writers.
      Parameters:
      o - The list of all objects to quietly close.
    • flush

      public static void flush(Object... o) throws IOException
      Flushes multiple output streams and writers in a single call.
      Parameters:
      o - The objects to flush. null entries are ignored.
      Throws:
      IOException - Thrown by underlying stream.
    • close

      public static void close(Object... o) throws IOException
      Close all specified input streams, output streams, readers, and writers.
      Parameters:
      o - The list of all objects to close. null entries are ignored.
      Throws:
      IOException - Thrown by underlying stream.
    • loadSystemResourceAsString

      public static String loadSystemResourceAsString(String name, String... paths) throws IOException
      Loads a text file from either the file system or classpath.
      Parameters:
      name - The file name.
      paths - The paths to search.
      Returns:
      The file contents, or null if not found.
      Throws:
      IOException - Thrown by underlying stream.