Class IOUtilities
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Callback interface for monitoring and controlling byte transfers. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
Safely closes any Closeable resource, suppressing any exceptions.static void
close
(XMLStreamReader reader) Safely closes an XMLStreamReader, suppressing any exceptions.static void
close
(XMLStreamWriter writer) Safely closes an XMLStreamWriter, suppressing any exceptions.static byte[]
compressBytes
(byte[] bytes) Compresses a byte array using GZIP compression.static byte[]
compressBytes
(byte[] bytes, int offset, int len) Compresses a portion of a byte array using GZIP compression.static void
compressBytes
(FastByteArrayOutputStream original, FastByteArrayOutputStream compressed) Compresses the contents of one FastByteArrayOutputStream into another using GZIP compression.static void
compressBytes
(ByteArrayOutputStream original, ByteArrayOutputStream compressed) Compresses the contents of one ByteArrayOutputStream into another using GZIP compression.static void
Safely flushes any Flushable resource, suppressing any exceptions.static void
flush
(XMLStreamWriter writer) Safely flushes an XMLStreamWriter, suppressing any exceptions.static InputStream
Gets an appropriate InputStream from a URLConnection, handling compression if necessary.static byte[]
Converts an InputStream's contents to a byte array.static void
transfer
(File file, OutputStream out) Transfers the contents of a File to an OutputStream.static void
Transfers the contents of a File to a URLConnection's output stream.static void
transfer
(InputStream in, byte[] bytes) Reads exactly the specified number of bytes from an InputStream into a byte array.static void
transfer
(InputStream s, File f, IOUtilities.TransferCallback cb) Transfers the contents of an InputStream to a File.static void
transfer
(InputStream in, OutputStream out) Transfers all bytes from an input stream to an output stream.static void
transfer
(InputStream in, OutputStream out, IOUtilities.TransferCallback cb) Transfers bytes from an input stream to an output stream with optional progress monitoring.static void
transfer
(URLConnection c, byte[] bytes) Transfers a byte array to a URLConnection's output stream.static void
Transfers the contents of a URLConnection's input stream to a File.static byte[]
uncompressBytes
(byte[] bytes) Uncompresses a GZIP-compressed byte array.static byte[]
uncompressBytes
(byte[] bytes, int offset, int len) Uncompresses a portion of a GZIP-compressed byte array.
-
Method Details
-
getInputStream
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 transferc
- the destination URLConnectioncb
- 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 URLConnectionf
- the destination Filecb
- 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 InputStreamf
- the destination Filecb
- 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 InputStreamout
- the destination OutputStreamcb
- optional callback for progress monitoring and cancellation (may be null)- Throws:
IOException
- if an I/O error occurs during transfer
-
transfer
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 frombytes
- 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
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 InputStreamout
- the destination OutputStream- Throws:
IOException
- if an I/O error occurs during transfer
-
transfer
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 Fileout
- the destination OutputStream- Throws:
IOException
- if an I/O error occurs during transfer
-
close
Safely closes an XMLStreamReader, suppressing any exceptions.- Parameters:
reader
- the XMLStreamReader to close (may be null)
-
close
Safely closes an XMLStreamWriter, suppressing any exceptions.- Parameters:
writer
- the XMLStreamWriter to close (may be null)
-
close
Safely closes any Closeable resource, suppressing any exceptions.- Parameters:
c
- the Closeable resource to close (may be null)
-
flush
Safely flushes any Flushable resource, suppressing any exceptions.- Parameters:
f
- the Flushable resource to flush (may be null)
-
flush
Safely flushes an XMLStreamWriter, suppressing any exceptions.- Parameters:
writer
- the XMLStreamWriter to flush (may be null)
-
inputStreamToBytes
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
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 tobytes
- 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 compresscompressed
- 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 compresscompressed
- 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 arrayoffset
- the starting position in the source arraylen
- 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 arrayoffset
- the starting position in the source arraylen
- the number of bytes to uncompress- Returns:
- the uncompressed byte array, or the original array if not compressed
- Throws:
RuntimeException
- if decompression fails
-