public final class IOUtil extends Object
java.io.*
classes.Modifier and Type | Method and 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 to
OutputStream.close() to the passed
os . |
static Writer |
asBufferedNonClosingUTF8Writer(OutputStream os)
Returns a writer that writes contents to the given output stream with
StandardCharsets.UTF_8 encoding and
does not propagate calls to Writer.close() to the passed os . |
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 with
StandardCharsets.UTF_8 encoding. |
static Reader |
asBufferedUTF8Reader(InputStream is)
Returns a reader that parses the contents of the given input stream with
StandardCharsets.UTF_8 encoding. |
static Writer |
asBufferedUTF8Writer(File file)
Returns a writer that writes contents to the given file with
StandardCharsets.UTF_8 encoding. |
static Writer |
asBufferedUTF8Writer(OutputStream os)
Returns a writer that writes contents to the given output stream with
StandardCharsets.UTF_8 encoding. |
static InputStream |
asUncompressedBufferedNonClosingInputStream(InputStream is)
Returns a buffered input stream that de-compresses the contents of
is (in case the given input stream
contains gzip'ed content) and does not propagate calls to InputStream.close() to the passed is . |
static Reader |
asUncompressedBufferedNonClosingUTF8Reader(InputStream is)
Returns a buffered reader that un-compresses the contents of
is (in case the given input stream contains
gzip'ed content), does not propagate calls to Reader.close() to the passed is and parses the
contents of the given input stream with StandardCharsets.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 with
StandardCharsets.UTF_8 encoding. |
static Writer |
asUTF8Writer(OutputStream os)
Returns a writer that writes contents to the given output stream with
StandardCharsets.UTF_8 encoding. |
public static InputStream asUncompressedInputStream(InputStream is) throws IOException
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 a
GZIPInputStream
. Otherwise, the stream is returned as-is.
Note: this requires the input stream to support marking
.
is
- the input streamis
IOException
- if reading the magic number failsIllegalArgumentException
- if the stream does not support marking
public static InputStream asBufferedInputStream(InputStream is)
BufferedInputStream
, it can also be, e.g., a ByteArrayInputStream
,
depending on the type of the supplied input stream.is
- the input streamis
public static InputStream asBufferedInputStream(File file) throws IOException
file
- the file to readIOException
- if accessing the file results in an I/O errorpublic static OutputStream asBufferedOutputStream(OutputStream os)
BufferedOutputStream
, it can also be, e.g., a ByteArrayOutputStream
,
depending on the type of the supplied output stream.os
- the output streamos
public static OutputStream asBufferedOutputStream(File file) throws IOException
file
- the file to write toIOException
- if accessing the file results in an I/O errorpublic static Reader asBufferedUTF8Reader(File file) throws IOException
StandardCharsets.UTF_8
encoding.
Additionally buffers the input stream to improve performance.file
- the file to readIOException
- if accessing the file results in an I/O errorpublic static Reader asBufferedUTF8Reader(InputStream is)
StandardCharsets.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).
is
- the input stream to readpublic static Reader asUTF8Reader(InputStream is)
StandardCharsets.UTF_8
encoding.is
- the input stream to readpublic static Writer asBufferedUTF8Writer(File file) throws IOException
StandardCharsets.UTF_8
encoding.
Additionally buffers the input stream to improve performance.file
- the file to write toIOException
- if writing to the file results in I/O errorspublic static Writer asBufferedUTF8Writer(OutputStream os)
StandardCharsets.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).
os
- the output stream to write topublic static Writer asUTF8Writer(OutputStream os)
StandardCharsets.UTF_8
encoding.os
- the output stream to write topublic static InputStream asUncompressedBufferedNonClosingInputStream(InputStream is) throws IOException
is
(in case the given input stream
contains gzip'ed content) and does not propagate calls to InputStream.close()
to the passed is
.is
- the input stream to readis
IOException
- if reading the stream (for detecting whether or not it contains compressed contents) failsNonClosingInputStream
,
asBufferedInputStream(InputStream)
,
asUncompressedInputStream(InputStream)
public static Reader asUncompressedBufferedNonClosingUTF8Reader(InputStream is) throws IOException
is
(in case the given input stream contains
gzip'ed content), does not propagate calls to Reader.close()
to the passed is
and parses the
contents of the given input stream with StandardCharsets.UTF_8
encoding.
Implementation note: the input stream (byte-wise representation) will be buffered, not the reader (character-wise representation).
is
- the input stream to readis
IOException
- if reading the stream (for detecting whether or not it contains compressed contents) failsasUTF8Reader(InputStream)
,
asUncompressedBufferedNonClosingInputStream(InputStream)
public static OutputStream asBufferedNonClosingOutputStream(OutputStream os)
OutputStream.close()
to the passed
os
.os
- the output stream to write toos
asBufferedOutputStream(OutputStream)
,
NonClosingOutputStream
public static Writer asBufferedNonClosingUTF8Writer(OutputStream os)
StandardCharsets.UTF_8
encoding and
does not propagate calls to Writer.close()
to the passed os
.
Implementation note: the output stream (byte-wise representation) will be buffered, not the writer (character- wise representation).
os
- the output stream to write toasBufferedNonClosingOutputStream(OutputStream)
,
asUTF8Writer(OutputStream)
Copyright © 2020. All rights reserved.