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
-
XML stream support: Some methods work with
javax.xml.stream.XMLStreamReader
andjavax.xml.stream.XMLStreamWriter
. These methods require thejava.xml
module to be present at runtime. If you're using OSGi, ensure your bundle imports thejavax.xml.stream
package or declare it as an optional import if XML support is not required. The rest of the library does not requirejava.xml
.
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);
Security and Performance Configuration:
IOUtilities provides configurable security and performance options through system properties. Most security features have safe defaults but can be customized as needed:
io.debug=false
— Enable debug loggingio.connect.timeout=5000
— Connection timeout (1s-5min)io.read.timeout=30000
— Read timeout (1s-5min)io.max.stream.size=2147483647
— Stream size limit (2GB)io.max.decompression.size=2147483647
— Decompression size limit (2GB)io.path.validation.disabled=false
— Path security validation enabledio.url.protocol.validation.disabled=false
— URL protocol validation enabledio.allowed.protocols=http,https,file,jar
— Allowed URL protocolsio.file.protocol.validation.disabled=false
— File protocol validation enabledio.debug.detailed.urls=false
— Detailed URL logging disabledio.debug.detailed.paths=false
— Detailed path logging disabled
- 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 byte[]
inputStreamToBytes
(InputStream in, int maxSize) Converts an InputStream's contents to a byte array with a maximum size limit.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 with default size limits.static byte[]
uncompressBytes
(byte[] bytes, int offset, int len) Uncompresses a portion of a GZIP-compressed byte array with default size limits.static byte[]
uncompressBytes
(byte[] bytes, int offset, int len, int maxSize) Uncompresses a portion of a GZIP-compressed byte array with specified size limit.
-
Method Details
-
getInputStream
Gets an appropriate InputStream from a URLConnection, handling compression if necessary.This method automatically detects and handles various compression encodings and optimizes connection performance with appropriate buffer sizing and connection parameters.
- GZIP ("gzip" or "x-gzip")
- DEFLATE ("deflate")
- 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 (thrown as unchecked)
-
transfer
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:
IOException
- if an I/O error occurs during the transfer (thrown as unchecked)
-
transfer
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:
IOException
- if an I/O error occurs during the transfer (thrown as unchecked)
-
transfer
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:
IOException
- if an I/O error occurs during the transfer (thrown as unchecked)
-
transfer
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. The callback receives a defensive copy of the buffer to prevent race conditions and data corruption.
- 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 (thrown as unchecked)
-
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. Uses DataInputStream.readFully for a simpler implementation.
- 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 (thrown as unchecked)
-
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 (thrown as unchecked)
-
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 (thrown as unchecked)
-
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 loads the entire stream into memory, so use with appropriate consideration for memory usage. Uses a default maximum size limit (2GB) to prevent memory exhaustion attacks while allowing reasonable data transfer operations. For custom limits, use
inputStreamToBytes(InputStream, int)
.- Parameters:
in
- the InputStream to read from- Returns:
- the byte array containing the stream's contents
- Throws:
IOException
- if an I/O error occurs or the stream exceeds the default size limit (thrown as unchecked)
-
inputStreamToBytes
Converts an InputStream's contents to a byte array with a maximum size limit.- Parameters:
in
- the InputStream to read frommaxSize
- the maximum number of bytes to read- Returns:
- the byte array containing the stream's contents
- Throws:
IOException
- if an I/O error occurs or the stream exceeds maxSize (thrown as unchecked)
-
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 (thrown as unchecked)
-
compressBytes
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 (thrown as unchecked)
-
compressBytes
public static void compressBytes(FastByteArrayOutputStream original, FastByteArrayOutputStream compressed) 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 (thrown as unchecked)
-
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 with default size limits.If the input is not GZIP-compressed, returns the original array unchanged. Uses a default maximum decompressed size (2GB) to prevent zip bomb attacks.
- Parameters:
bytes
- the compressed byte array- Returns:
- the uncompressed byte array, or the original array if not compressed
- Throws:
RuntimeException
- if decompression fails or exceeds size limits
-
uncompressBytes
public static byte[] uncompressBytes(byte[] bytes, int offset, int len) Uncompresses a portion of a GZIP-compressed byte array with default size limits.If the input is not GZIP-compressed, returns the original array unchanged. Uses a default maximum decompressed size (2GB) to prevent zip bomb attacks.
- 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 or exceeds size limits
-
uncompressBytes
public static byte[] uncompressBytes(byte[] bytes, int offset, int len, int maxSize) Uncompresses a portion of a GZIP-compressed byte array with specified size limit.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 uncompressmaxSize
- the maximum allowed decompressed size in bytes- Returns:
- the uncompressed byte array, or the original array if not compressed
- Throws:
RuntimeException
- if decompression fails or exceeds size limits
-