Class IOUtil
- java.lang.Object
-
- net.automatalib.common.util.IOUtil
-
public final class IOUtil extends Object
Utility methods for operating withjava.io.*
classes.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static InputStream
asBufferedInputStream(File file)
Returns an input stream that reads the contents of the given file.static InputStream
asBufferedInputStream(InputStream is)
Ensures that the returned stream is a buffered version of the supplied input stream.static OutputStream
asBufferedNonClosingOutputStream(OutputStream os)
Returns a buffered output stream that does not propagate calls toOutputStream.close()
to the passedos
.static Writer
asBufferedNonClosingUTF8Writer(OutputStream os)
Returns a writer that writes contents to the given output stream withStandardCharsets.UTF_8
encoding and does not propagate calls toWriter.close()
to the passedos
.static OutputStream
asBufferedOutputStream(File file)
Returns an output stream that writes the contents to the given file.static OutputStream
asBufferedOutputStream(OutputStream os)
Ensures that the returned stream is a buffered version of the supplied output stream.static Reader
asBufferedUTF8Reader(File file)
Returns a reader that parses the contents of the given file withStandardCharsets.UTF_8
encoding.static Reader
asBufferedUTF8Reader(InputStream is)
Returns a reader that parses the contents of the given input stream withStandardCharsets.UTF_8
encoding.static Writer
asBufferedUTF8Writer(File file)
Returns a writer that writes contents to the given file withStandardCharsets.UTF_8
encoding.static Writer
asBufferedUTF8Writer(OutputStream os)
Returns a writer that writes contents to the given output stream withStandardCharsets.UTF_8
encoding.static InputStream
asUncompressedBufferedNonClosingInputStream(InputStream is)
Returns a buffered input stream that de-compresses the contents ofis
(in case the given input stream contains gzip'ed content) and does not propagate calls toInputStream.close()
to the passedis
.static Reader
asUncompressedBufferedNonClosingUTF8Reader(InputStream is)
Returns a buffered reader that un-compresses the contents ofis
(in case the given input stream contains gzip'ed content), does not propagate calls toReader.close()
to the passedis
and parses the contents of the given input stream withStandardCharsets.UTF_8
encoding.static InputStream
asUncompressedInputStream(InputStream is)
Ensures that the returned stream is an uncompressed version of the supplied input stream.static Reader
asUTF8Reader(InputStream is)
Returns a reader that parses the contents of the given input stream withStandardCharsets.UTF_8
encoding.static Writer
asUTF8Writer(OutputStream os)
Returns a writer that writes contents to the given output stream withStandardCharsets.UTF_8
encoding.
-
-
-
Method Detail
-
asUncompressedInputStream
public static InputStream asUncompressedInputStream(InputStream is) throws IOException
Ensures that the returned stream is an uncompressed version of the supplied input stream.This method first tries to read the first two bytes from the stream, then resets the stream. If the first two bytes equal the GZip magic number (see
GZIPInputStream.GZIP_MAGIC
), the supplied stream is wrapped in aGZIPInputStream
. Otherwise, the stream is returned as-is.Note: this requires the input stream to
support marking
.- Parameters:
is
- the input stream- Returns:
- an uncompressed version of
is
- Throws:
IOException
- if reading the magic number failsIllegalArgumentException
- if the stream does not supportmarking
-
asBufferedInputStream
public static InputStream asBufferedInputStream(InputStream is)
Ensures that the returned stream is a buffered version of the supplied input stream. The result must not necessarily be an instance ofBufferedInputStream
, it can also be, e.g., aByteArrayInputStream
, depending on the type of the supplied input stream.- Parameters:
is
- the input stream- Returns:
- a buffered version of
is
-
asBufferedInputStream
public static InputStream asBufferedInputStream(File file) throws IOException
Returns an input stream that reads the contents of the given file. Additionally, buffers the input stream to improve performance.- Parameters:
file
- the file to read- Returns:
- a buffered input stream for the file contents
- Throws:
IOException
- if accessing the file results in an I/O error
-
asBufferedOutputStream
public static OutputStream asBufferedOutputStream(OutputStream os)
Ensures that the returned stream is a buffered version of the supplied output stream. The result must not necessarily be an instance ofBufferedOutputStream
, it can also be, e.g., aByteArrayOutputStream
, depending on the type of the supplied output stream.- Parameters:
os
- the output stream- Returns:
- a buffered version of
os
-
asBufferedOutputStream
public static OutputStream asBufferedOutputStream(File file) throws IOException
Returns an output stream that writes the contents to the given file. Additionally, buffers the input stream to improve performance.- Parameters:
file
- the file to write to- Returns:
- a buffered output stream for the file contents
- Throws:
IOException
- if accessing the file results in an I/O error
-
asBufferedUTF8Reader
public static Reader asBufferedUTF8Reader(File file) throws IOException
Returns a reader that parses the contents of the given file withStandardCharsets.UTF_8
encoding. Additionally, buffers the input stream to improve performance.- Parameters:
file
- the file to read- Returns:
- a buffered, UTF-8-decoding reader for the file contents
- Throws:
IOException
- if accessing the file results in an I/O error
-
asBufferedUTF8Reader
public static Reader asBufferedUTF8Reader(InputStream is)
Returns a reader that parses the contents of the given input stream withStandardCharsets.UTF_8
encoding. If the given input stream is not already a buffering input stream, additionally buffers the input stream to improve performance.Implementation note: the input stream (byte-wise representation) will be buffered, not the reader (character-wise representation).
- Parameters:
is
- the input stream to read- Returns:
- a buffered, UTF-8-decoding reader for the input stream.
-
asUTF8Reader
public static Reader asUTF8Reader(InputStream is)
Returns a reader that parses the contents of the given input stream withStandardCharsets.UTF_8
encoding.- Parameters:
is
- the input stream to read- Returns:
- a UTF-8-decoding reader for the input stream
-
asBufferedUTF8Writer
public static Writer asBufferedUTF8Writer(File file) throws IOException
Returns a writer that writes contents to the given file withStandardCharsets.UTF_8
encoding. Additionally, buffers the input stream to improve performance.- Parameters:
file
- the file to write to- Returns:
- a buffered, UTF-8-encoding writer for the file contents
- Throws:
IOException
- if writing to the file results in I/O errors
-
asBufferedUTF8Writer
public static Writer asBufferedUTF8Writer(OutputStream os)
Returns a writer that writes contents to the given output stream withStandardCharsets.UTF_8
encoding. If the given output stream is not already a buffering output stream, additionally buffers the output stream to improve performance.Implementation note: the output stream (byte-wise representation) will be buffered, not the writer (character- wise representation).
- Parameters:
os
- the output stream to write to- Returns:
- a buffered, UTF-8 encoding writer for the output stream
-
asUTF8Writer
public static Writer asUTF8Writer(OutputStream os)
Returns a writer that writes contents to the given output stream withStandardCharsets.UTF_8
encoding.- Parameters:
os
- the output stream to write to- Returns:
- a UTF-8-encoding writer for the output stream.
-
asUncompressedBufferedNonClosingInputStream
public static InputStream asUncompressedBufferedNonClosingInputStream(InputStream is) throws IOException
Returns a buffered input stream that de-compresses the contents ofis
(in case the given input stream contains gzip'ed content) and does not propagate calls toInputStream.close()
to the passedis
.- Parameters:
is
- the input stream to read- Returns:
- a (potentially) de-compressing, buffered, non-closing version of
is
- Throws:
IOException
- if reading the stream (for detecting whether it contains compressed contents) fails- See Also:
NonClosingInputStream
,asBufferedInputStream(InputStream)
,asUncompressedInputStream(InputStream)
-
asUncompressedBufferedNonClosingUTF8Reader
public static Reader asUncompressedBufferedNonClosingUTF8Reader(InputStream is) throws IOException
Returns a buffered reader that un-compresses the contents ofis
(in case the given input stream contains gzip'ed content), does not propagate calls toReader.close()
to the passedis
and parses the contents of the given input stream withStandardCharsets.UTF_8
encoding.Implementation note: the input stream (byte-wise representation) will be buffered, not the reader (character-wise representation).
- Parameters:
is
- the input stream to read- Returns:
- a (potentially) de-compressing, buffered, non-closing, UTF-8-decoding version of
is
- Throws:
IOException
- if reading the stream (for detecting whether it contains compressed contents) fails- See Also:
asUTF8Reader(InputStream)
,asUncompressedBufferedNonClosingInputStream(InputStream)
-
asBufferedNonClosingOutputStream
public static OutputStream asBufferedNonClosingOutputStream(OutputStream os)
Returns a buffered output stream that does not propagate calls toOutputStream.close()
to the passedos
.- Parameters:
os
- the output stream to write to- Returns:
- a buffered, non-closing version of
os
- See Also:
asBufferedOutputStream(OutputStream)
,NonClosingOutputStream
-
asBufferedNonClosingUTF8Writer
public static Writer asBufferedNonClosingUTF8Writer(OutputStream os)
Returns a writer that writes contents to the given output stream withStandardCharsets.UTF_8
encoding and does not propagate calls toWriter.close()
to the passedos
.Implementation note: the output stream (byte-wise representation) will be buffered, not the writer (character- wise representation).
- Parameters:
os
- the output stream to write to- Returns:
- a buffered, non-closing, UTF-8-encoding writer for the output stream
- See Also:
asBufferedNonClosingOutputStream(OutputStream)
,asUTF8Writer(OutputStream)
-
-