Class IOUtilities

java.lang.Object
com.cedarsoftware.util.IOUtilities

public final class IOUtilities extends Object
Utility class providing robust I/O operations with built-in error handling and resource management.

This class simplifies common I/O tasks such as:

  • Stream transfers and copying
  • Resource closing and flushing
  • Byte array compression/decompression
  • URL connection handling
  • File operations

Key Features:

  • Automatic buffer management for optimal performance
  • GZIP and Deflate compression support
  • Silent exception handling for close/flush operations
  • Progress tracking through callback mechanism
  • Support for XML stream operations

Usage Example:


 // Copy file to output stream
 try (InputStream fis = Files.newInputStream(Paths.get("input.txt"))) {
     try (OutputStream fos = Files.newOutputStream(Paths.get("output.txt"))) {
         IOUtilities.transfer(fis, fos);
     }
 }

 // Compress byte array
 byte[] compressed = IOUtilities.compressBytes(originalBytes);
 byte[] uncompressed = IOUtilities.uncompressBytes(compressed);
 
Author:
Ken Partlow, John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

License

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
  • Method Details

    • getInputStream

      public static InputStream getInputStream(URLConnection c) throws IOException
      Gets an appropriate InputStream from a URLConnection, handling compression if necessary.

      This method automatically detects and handles various compression encodings:

      • GZIP ("gzip" or "x-gzip")
      • DEFLATE ("deflate")

      The returned stream is always buffered for optimal performance.

      Parameters:
      c - the URLConnection to get the input stream from
      Returns:
      a buffered InputStream, potentially wrapped with a decompressing stream
      Throws:
      IOException - if an I/O error occurs
    • transfer

      public static void transfer(File f, URLConnection c, IOUtilities.TransferCallback cb) throws Exception
      Transfers the contents of a File to a URLConnection's output stream.

      Progress can be monitored and the transfer can be cancelled through the callback interface.

      Parameters:
      f - the source File to transfer
      c - the destination URLConnection
      cb - optional callback for progress monitoring and cancellation (may be null)
      Throws:
      Exception - if any error occurs during the transfer
    • transfer

      public static void transfer(URLConnection c, File f, IOUtilities.TransferCallback cb) throws Exception
      Transfers the contents of a URLConnection's input stream to a File.

      Progress can be monitored and the transfer can be cancelled through the callback interface. Automatically handles compressed streams.

      Parameters:
      c - the source URLConnection
      f - the destination File
      cb - optional callback for progress monitoring and cancellation (may be null)
      Throws:
      Exception - if any error occurs during the transfer
    • transfer

      public static void transfer(InputStream s, File f, IOUtilities.TransferCallback cb) throws Exception
      Transfers the contents of an InputStream to a File.

      Progress can be monitored and the transfer can be cancelled through the callback interface. The output stream is automatically buffered for optimal performance.

      Parameters:
      s - the source InputStream
      f - the destination File
      cb - optional callback for progress monitoring and cancellation (may be null)
      Throws:
      Exception - if any error occurs during the transfer
    • transfer

      public static void transfer(InputStream in, OutputStream out, IOUtilities.TransferCallback cb) throws IOException
      Transfers bytes from an input stream to an output stream with optional progress monitoring.

      This method does not close the streams; that responsibility remains with the caller. Progress can be monitored and the transfer can be cancelled through the callback interface.

      Parameters:
      in - the source InputStream
      out - the destination OutputStream
      cb - optional callback for progress monitoring and cancellation (may be null)
      Throws:
      IOException - if an I/O error occurs during transfer
    • transfer

      public static void transfer(InputStream in, byte[] bytes) throws IOException
      Reads exactly the specified number of bytes from an InputStream into a byte array.

      This method will continue reading until either the byte array is full or the end of the stream is reached.

      Parameters:
      in - the InputStream to read from
      bytes - the byte array to fill
      Throws:
      IOException - if the stream ends before the byte array is filled or if any other I/O error occurs
    • transfer

      public static void transfer(InputStream in, OutputStream out) throws IOException
      Transfers all bytes from an input stream to an output stream.

      This method does not close the streams; that responsibility remains with the caller. Uses an internal buffer for efficient transfer.

      Parameters:
      in - the source InputStream
      out - the destination OutputStream
      Throws:
      IOException - if an I/O error occurs during transfer
    • transfer

      public static void transfer(File file, OutputStream out) throws IOException
      Transfers the contents of a File to an OutputStream.

      The input is automatically buffered for optimal performance. The output stream is flushed after the transfer but not closed.

      Parameters:
      file - the source File
      out - the destination OutputStream
      Throws:
      IOException - if an I/O error occurs during transfer
    • close

      public static void close(XMLStreamReader reader)
      Safely closes an XMLStreamReader, suppressing any exceptions.
      Parameters:
      reader - the XMLStreamReader to close (may be null)
    • close

      public static void close(XMLStreamWriter writer)
      Safely closes an XMLStreamWriter, suppressing any exceptions.
      Parameters:
      writer - the XMLStreamWriter to close (may be null)
    • close

      public static void close(Closeable c)
      Safely closes any Closeable resource, suppressing any exceptions.
      Parameters:
      c - the Closeable resource to close (may be null)
    • flush

      public static void flush(Flushable f)
      Safely flushes any Flushable resource, suppressing any exceptions.
      Parameters:
      f - the Flushable resource to flush (may be null)
    • flush

      public static void flush(XMLStreamWriter writer)
      Safely flushes an XMLStreamWriter, suppressing any exceptions.
      Parameters:
      writer - the XMLStreamWriter to flush (may be null)
    • inputStreamToBytes

      public static byte[] inputStreamToBytes(InputStream in)
      Converts an InputStream's contents to a byte array.

      This method should only be used when the input stream's length is known to be relatively small, as it loads the entire stream into memory.

      Parameters:
      in - the InputStream to read from
      Returns:
      the byte array containing the stream's contents, or null if an error occurs
    • transfer

      public static void transfer(URLConnection c, byte[] bytes) throws IOException
      Transfers a byte array to a URLConnection's output stream.

      The output stream is automatically buffered for optimal performance and properly closed after transfer.

      Parameters:
      c - the URLConnection to write to
      bytes - the byte array to transfer
      Throws:
      IOException - if an I/O error occurs during transfer
    • compressBytes

      public static void compressBytes(ByteArrayOutputStream original, ByteArrayOutputStream compressed) throws IOException
      Compresses the contents of one ByteArrayOutputStream into another using GZIP compression.

      Uses BEST_SPEED compression level for optimal performance.

      Parameters:
      original - the ByteArrayOutputStream containing the data to compress
      compressed - the ByteArrayOutputStream to receive the compressed data
      Throws:
      IOException - if an I/O error occurs during compression
    • compressBytes

      public static void compressBytes(FastByteArrayOutputStream original, FastByteArrayOutputStream compressed) throws IOException
      Compresses the contents of one FastByteArrayOutputStream into another using GZIP compression.

      Uses BEST_SPEED compression level for optimal performance.

      Parameters:
      original - the FastByteArrayOutputStream containing the data to compress
      compressed - the FastByteArrayOutputStream to receive the compressed data
      Throws:
      IOException - if an I/O error occurs during compression
    • compressBytes

      public static byte[] compressBytes(byte[] bytes)
      Compresses a byte array using GZIP compression.
      Parameters:
      bytes - the byte array to compress
      Returns:
      a new byte array containing the compressed data
      Throws:
      RuntimeException - if compression fails
    • compressBytes

      public static byte[] compressBytes(byte[] bytes, int offset, int len)
      Compresses a portion of a byte array using GZIP compression.
      Parameters:
      bytes - the source byte array
      offset - the starting position in the source array
      len - the number of bytes to compress
      Returns:
      a new byte array containing the compressed data
      Throws:
      RuntimeException - if compression fails
    • uncompressBytes

      public static byte[] uncompressBytes(byte[] bytes)
      Uncompresses a GZIP-compressed byte array.

      If the input is not GZIP-compressed, returns the original array unchanged.

      Parameters:
      bytes - the compressed byte array
      Returns:
      the uncompressed byte array, or the original array if not compressed
      Throws:
      RuntimeException - if decompression fails
    • uncompressBytes

      public static byte[] uncompressBytes(byte[] bytes, int offset, int len)
      Uncompresses a portion of a GZIP-compressed byte array.

      If the input is not GZIP-compressed, returns the original array unchanged.

      Parameters:
      bytes - the compressed byte array
      offset - the starting position in the source array
      len - the number of bytes to uncompress
      Returns:
      the uncompressed byte array, or the original array if not compressed
      Throws:
      RuntimeException - if decompression fails