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 |
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 |
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. |
static void |
closeQuietly(Closeable closeable)
Quitely closes a closeable.
|
static void |
copy(InputStream is,
OutputStream os)
Copies all data from the given input stream to the given output stream and closes the streams.
|
static void |
copy(InputStream is,
OutputStream os,
boolean close)
Copies all data from the given input stream to the given output stream.
|
static void |
copy(Reader r,
Writer w)
Copies all text from the given reader to the given writer and closes both afterwards.
|
static void |
copy(Reader r,
Writer w,
boolean close)
Copies all text from the given reader to the given writer.
|
static void |
skip(InputStream is)
Skips the content of the stream as long as there is data available.
|
public static void skip(InputStream is) throws IOException
is
- the input stream.IOException
- if an I/O error occurs.public static void copy(InputStream is, OutputStream os) throws IOException
copy(is, os, true)
.is
- the input stream.os
- the output stream.IOException
- if an I/O error occurs.copy(InputStream, OutputStream, boolean)
public static void copy(InputStream is, OutputStream os, boolean close) throws IOException
is
- the input stream.os
- the output stream.close
- true
if both streams are closed afterwards, false
otherwise.IOException
- if an I/O error occurs.public static void copy(Reader r, Writer w) throws IOException
copy(r, w, true)
.r
- the reader.w
- the writer.IOException
- if an I/O error occurs.copy(Reader, Writer, boolean)
public static void copy(Reader r, Writer w, boolean close) throws IOException
r
- the reader.w
- the writer.close
- true
if both reader and writer are closed afterwards, false
otherwise.IOException
- if an I/O error occurs.public static void closeQuietly(Closeable closeable)
closeable
- the closeable to closepublic 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, but possibly in a buffered version (see asBufferedInputStream(InputStream)
).
is
- the input streamis
IOException
- if reading the magic number failspublic 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 error.public 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 error.public 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 error.public 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 errors.public 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 toCopyright © 2020. All rights reserved.