public final class IOUtil extends Object
Modifier and Type | Method and Description |
---|---|
static InputStream |
asBufferedInputStream(InputStream is)
Ensures that the returned stream is a buffered version of the supplied input stream.
|
static OutputStream |
asBufferedOutputStream(OutputStream os) |
static InputStream |
asUncompressedInputStream(InputStream is)
Ensures that the returned stream is an uncompressed version of the supplied input stream.
|
static Reader |
asUTF8Reader(File file) |
static Writer |
asUTF8Writer(File file) |
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 OutputStream asBufferedOutputStream(OutputStream os)
public static Reader asUTF8Reader(File file) throws FileNotFoundException
FileNotFoundException
public static Writer asUTF8Writer(File file) throws FileNotFoundException
FileNotFoundException
Copyright © 2018. All rights reserved.