Class IoUtils

java.lang.Object
org.apache.juneau.commons.utils.IoUtils

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

  • Constructor Details

    • IoUtils

      protected IoUtils()
      Constructor.
  • Method Details

    • cleanupThreadLocals

      public static void cleanupThreadLocals()
      Cleans up thread-local buffer caches for the current thread.

      This method should be called when a thread is finished using the buffer caches to prevent memory leaks in thread pool environments where threads are reused.

    • 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.
    • drainQuietly

      public static void drainQuietly(Closeable in)
      Reads and discards the remainder of a reader or input stream, ignoring any exceptions.

      Reads until end-of-stream using a 256-unit buffer. Any IOException stops the drain silently. No-op if the object is null or is neither a Reader nor an InputStream. The stream is not closed.

      Parameters:
      in - The reader or input stream to drain.
    • 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(Object... o)
      Quietly close all specified input streams, output streams, readers, and writers.
      Parameters:
      o - The list of all objects to quietly 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.
    • 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. Can be null (returns 0).
      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. Can be null (returns 0).
      Returns:
      The number of characters read.
      Throws:
      IOException - Thrown by underlying stream.
    • 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.
    • 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. Must not be null.
      paths - The paths to search.
      Returns:
      The file contents, or null if not found.
      Throws:
      IOException - Thrown by underlying stream.
    • 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 (returns 0).
      Reader is automatically closed.
      out - The file to write the output to.
      Can be null (returns 0).
      Writer is flushed but not automatically closed.
      Returns:
      The number of characters piped.
      Throws:
      IOException - Thrown by underlying 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. Must not be null when the input is non-null.
      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(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, 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. Must not be null when the input stream is non-null.
      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. Must not be null when the input stream is non-null.
      onException - Consumer of any I/O exceptions. Must not be null (invoked on I/O error).
      Returns:
      The contents of the reader as a string or null if input stream was null.
    • 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. Must not be null (invoked on I/O error).
      Returns:
      The contents of the reader as a string, or null if the input stream was null.
    • read

      public static String read(InputStream in, int maxBytes) throws IOException
      Reads the contents of an input stream into a string, reading up to the specified maximum number of bytes.

      Assumes UTF-8 encoding.

      Parameters:
      in - The input stream.
      Can be null.
      Stream is automatically closed.
      maxBytes - The maximum number of bytes to read, or -1 to read all bytes.
      Returns:
      The contents of the input stream 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, int maxBytes, Charset cs) throws IOException
      Reads the contents of an input stream into a string using the specified charset, reading up to the specified maximum number of bytes.
      Parameters:
      in - The input stream.
      Can be null.
      Stream is automatically closed.
      maxBytes - The maximum number of bytes to read, or -1 to read all bytes.
      cs - The charset of the contents of the input stream. Must not be null when the input stream is non-null.
      Returns:
      The contents of the input stream 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, long maxBytes) throws IOException
      Reads the contents of an input stream into a string, reading up to the specified maximum number of bytes.

      Assumes UTF-8 encoding.

      Parameters:
      in - The input stream.
      Can be null.
      Stream is automatically closed.
      maxBytes - The maximum number of bytes to read, or -1 to read all bytes.
      Returns:
      The contents of the input stream 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(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, or null if the input was null.
      Throws:
      IOException - If thrown from output stream.
      IllegalArgumentException - If the input is non-null but not one of the supported types.
    • read

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

      Assumes default character encoding.

      Parameters:
      in - The path to read.
      Can be null.
      Returns:
      The contents of the reader as a string, or null if path does not exist.
      Throws:
      IOException - If a problem occurred trying to read from the reader.
      Since:
      9.1.0
    • 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. Must not be null (invoked on I/O error).
      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.
    • 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.
      Can be null (returns an empty array).
      Returns:
      The contents of the file as a byte array. Never null.
      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.
      Can be null (returns an empty 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. Never null.
      Throws:
      IOException - Thrown by underlying stream.
    • 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 (returns an empty array).
      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 (returns an empty array).
      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(Reader in) throws IOException
      Reads the specified input stream into the specified byte array.
      Parameters:
      in - The input stream to read.
      Can be null (returns an empty array).
      Stream is automatically closed.
      Returns:
      A byte array containing the contents. Never null.
      Throws:
      IOException - Thrown by underlying stream.
    • inflate

      public static byte[] inflate(byte[] compressed, boolean nowrap, long maxOutputBytes, int maxRatio) throws IOException
      Inflates (decompresses) the specified compressed bytes with a hard ceiling on the total output size and an effective output-to-input ratio guard.

      Building on the bounded-copy family in this class (such as readBytes(InputStream,int) and pipe(InputStream,OutputStream,long)), this method decompresses untrusted input without letting a small compressed payload expand into an unbounded allocation. Two independent limits are enforced during inflation, so a violating stream is aborted before its full output is ever materialized:

      • Absolute cap. If the running output size would exceed maxOutputBytes, an IOException is thrown.
      • Ratio guard. If the running output size would exceed maxRatio times the compressed input length, an IOException is thrown — this catches a highly-compressible payload even when the absolute cap is generous.
      Parameters:
      compressed - The compressed input bytes.
      Can be null (returns an empty array).
      nowrap - Pass true for a raw (headerless) DEFLATE stream, or false for a zlib-wrapped stream.
      Must match the format used to compress the input.
      maxOutputBytes - The maximum number of decompressed bytes to allow.
      Values <= 0 disable the absolute cap (mirroring the maxLength convention used elsewhere); the ratio guard, if enabled, still applies.
      maxRatio - The maximum allowed ratio of output size to compressed input size.
      Values <= 0 disable the ratio guard.
      Returns:
      The decompressed bytes. Never null.
      Throws:
      IOException - If the input is not valid for the selected format, or if either the absolute cap or the ratio guard is exceeded.
      Since:
      10.0.0
    • toBufferedReader

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

      public static long pipe(byte[] in, OutputStream out, int maxBytes) throws IOException
      Pipes bytes to output stream.
      Returns 0 if either in or out is null.
      Throws:
      IOException
    • pipe

      public static long pipe(InputStream in, OutputStream out) throws IOException
      Pipes input stream to output stream.
      Returns 0 if either in or out is null.
      Throws:
      IOException
    • pipe

      public static long pipe(InputStream in, OutputStream out, Consumer<IOException> onException)
      Pipes input stream to output stream with exception handler.
      Returns 0 if either in or out is null.
      onException must not be null (invoked on I/O error).
    • pipe

      public static long pipe(InputStream in, OutputStream out, long maxBytes) throws IOException
      Pipes input stream to output stream with max bytes.
      Returns 0 if either in or out is null.
      Throws:
      IOException
    • pipe

      public static long pipe(InputStream in, Writer out) throws IOException
      Pipes input stream to writer.
      Returns 0 if either in or out is null.
      Throws:
      IOException
    • pipe

      public static long pipe(InputStream in, Writer out, Consumer<IOException> onException)
      Pipes input stream to writer with exception handler.
      Returns 0 if either in or out is null.
      onException must not be null (invoked on I/O error).
    • pipe

      public static long pipe(Reader in, File out) throws IOException
      Pipes reader to file.
      Returns 0 if either in or out is null.
      Throws:
      IOException
    • pipe

      public static long pipe(Reader in, OutputStream out) throws IOException
      Pipes reader to output stream.
      Returns 0 if either in or out is null.
      Throws:
      IOException
    • pipe

      public static long pipe(Reader in, OutputStream out, Consumer<IOException> onException)
      Pipes reader to output stream with exception handler.
      Returns 0 if either in or out is null.
      onException must not be null (invoked on I/O error).
    • pipe

      public static long pipe(Reader in, Writer out) throws IOException
      Pipes reader to writer.
      Returns 0 if either in or out is null.
      Throws:
      IOException
    • pipe

      public static long pipe(Reader in, Writer out, Consumer<IOException> onException)
      Pipes reader to writer with exception handler.
      Returns 0 if either in or out is null.
      onException must not be null (invoked on I/O error).
    • printLines

      public static final void printLines(String[] lines)
      Prints all the specified lines to the log with line numbers.
      Parameters:
      lines - The lines to print. Must not be null (individual null entries render as "null").