public final class IOUtil extends Object
Modifier and Type | Method and Description |
---|---|
static byte[] |
borrowBytes(InputStream is,
int qty)
Gets and resets the specified number of bytes from the input stream.
|
static char[] |
borrowCharacters(Reader reader,
int qty)
Gets and resets the specified number of characters from the reader.
|
static void |
closeQuietly(Closeable... closeable)
Given Apache has deprecated its
IOUtils#closeQuietly(java.io.Closeable) method,
this one offers an alternate one. |
static int |
consume(InputStream is)
Fully consumes an input stream.
|
static int |
consume(Reader reader)
Fully consumes an input stream.
|
static int |
consumeUntil(Reader reader,
IntPredicate predicate)
Consumes markable reader characters until the predicate returns
true for a character or the end of stream is reached. |
static int |
consumeUntil(Reader reader,
IntPredicate predicate,
Appendable appendable)
Consumes markable reader characters until the predicate returns
true for a character or the end of stream is reached. |
static int |
consumeUntil(Reader reader,
String str,
Appendable appendable)
Consumes reader characters until after encountering the supplied string
(the matching string is also consumed) or the end of stream is reached.
|
static int |
consumeWhile(Reader reader,
IntPredicate predicate)
Consumes markable reader characters while the predicate returns
true for a character or the end of stream is reached. |
static int |
consumeWhile(Reader reader,
IntPredicate predicate,
Appendable appendable)
Consumes markable reader characters while the predicate returns
true for a character or the end of stream is reached. |
static String[] |
head(InputStream is,
Charset encoding,
int lineQty)
Gets the first lines from an input stream, using the specified encoding.
|
static String[] |
head(InputStream is,
int lineQty)
Gets the first lines from an input stream, using UTF-8.
|
static String[] |
head(InputStream is,
String encoding,
int lineQty)
Gets the first lines from an input stream, using the specified encoding.
|
static boolean |
isEmpty(InputStream is)
Gets whether the given input stream is
null or empty. |
static boolean |
isEmpty(Reader reader)
Gets whether the given Reader is
null or empty. |
static boolean |
startsWith(InputStream is,
byte[] bytes)
Whether the given input stream starts the specified bytes array
or not.
|
static String[] |
tail(InputStream is,
Charset encoding,
int lineQty)
Gets the last lines from an input stream, using the specified encoding.
|
static String[] |
tail(InputStream is,
int lineQty)
Gets the last lines from an input stream, using UTF-8.
|
static String[] |
tail(InputStream is,
String encoding,
int lineQty)
Gets the last lines from an input stream, using the specified encoding.
|
static BufferedInputStream |
toBufferedInputStream(InputStream in)
Wraps the input stream in a
BufferedInputStream if not a subclass
already. |
static BufferedReader |
toBufferedReader(Reader reader)
Wraps the reader in a
BufferedReader if not a subclass already. |
static InputStream |
toNonNullInputStream(InputStream is)
Gets an "empty" input stream (zero size) when the supplied input
stream is
null . |
static Reader |
toNonNullReader(Reader reader)
Gets an "empty" reader (zero size) when the supplied reader
is
null . |
public static boolean startsWith(InputStream is, byte[] bytes) throws IOException
false
is returned.is
- input streambytes
- byte array to comparetrue
if input stream starts with byte arrayIOException
- if InputStream.markSupported()
returns falsepublic static byte[] borrowBytes(InputStream is, int qty) throws IOException
is
- input streamqty
- number of bytes to readIOException
- if InputStream.markSupported()
returns falsepublic static char[] borrowCharacters(Reader reader, int qty) throws IOException
reader
- readerqty
- number of characters to readIOException
- if Reader.markSupported()
returns falsepublic static boolean isEmpty(InputStream is) throws IOException
null
or empty.
Must support marks.is
- input streamtrue
if null
or emptyIOException
- if InputStream.markSupported()
returns falsepublic static boolean isEmpty(Reader reader) throws IOException
null
or empty.
Must support marks.reader
- readertrue
if null
or emptyIOException
- if Reader.markSupported()
returns falsepublic static BufferedReader toBufferedReader(Reader reader)
BufferedReader
if not a subclass already.reader
- the reader to wrap if neededpublic static BufferedInputStream toBufferedInputStream(InputStream in)
BufferedInputStream
if not a subclass
already.in
- the input stream to wrap if neededpublic static String[] tail(InputStream is, int lineQty) throws IOException
FileUtil.tail(
java.io.File, int)
instead, which can read
a file starting from the end.is
- input streamlineQty
- maximum number of lines to returnIOException
- problem reading linespublic static String[] tail(InputStream is, String encoding, int lineQty) throws IOException
FileUtil.tail(
File, String, int)
instead, which can read
a file starting from the end.is
- input streamencoding
- character encodinglineQty
- maximum number of lines to returnIOException
- problem reading linespublic static String[] tail(InputStream is, Charset encoding, int lineQty) throws IOException
FileUtil.tail(
File, String, int)
instead, which can read
a file starting from the end.is
- input streamencoding
- character encodinglineQty
- maximum number of lines to returnIOException
- problem reading linespublic static String[] head(InputStream is, int lineQty) throws IOException
is
- input streamlineQty
- maximum number of lines to returnIOException
- problem reading linespublic static String[] head(InputStream is, String encoding, int lineQty) throws IOException
is
- input streamencoding
- character encodinglineQty
- maximum number of lines to returnIOException
- problem reading linespublic static String[] head(InputStream is, Charset encoding, int lineQty) throws IOException
is
- input streamencoding
- character encodinglineQty
- maximum number of lines to returnIOException
- problem reading linespublic static Reader toNonNullReader(Reader reader)
null
.
Else, return the supplied reader.reader
- original readerpublic static InputStream toNonNullInputStream(InputStream is)
null
.
Else, return the supplied input stream.is
- original input streampublic static int consume(InputStream is) throws IOException
is
- input streamIOException
- could not consume streampublic static int consume(Reader reader) throws IOException
reader
- readerIOException
- could not consume readerpublic static int consumeUntil(Reader reader, IntPredicate predicate) throws IOException
true
for a character or the end of stream is reached.
The character ending the consumption is not consumed (i.e.,
the reader cursor is reset just before that character).reader
- the reader to consumepredicate
- the character evaluation conditionnull
)IOException
- could not consume streamIllegalArgumentException
- if reader does not support markpublic static int consumeUntil(Reader reader, IntPredicate predicate, Appendable appendable) throws IOException
true
for a character or the end of stream is reached.
Optionally append the consumed characters to an Appendable
(e.g. StringBuilder).
The character ending the consumption is not consumed (i.e.,
the reader cursor is reset just before that character).reader
- the reader to consumepredicate
- the character evaluation conditionappendable
- optional, to append consumed charactersnull
)IOException
- could not consume streamIllegalArgumentException
- if reader does not support markpublic static int consumeUntil(Reader reader, String str, Appendable appendable) throws IOException
Appendable
(e.g. StringBuilder).
The matching string is also consumed.reader
- the reader to consumestr
- the string to matchappendable
- optional, to append consumed charactersnull
)IOException
- could not consume streampublic static int consumeWhile(Reader reader, IntPredicate predicate) throws IOException
true
for a character or the end of stream is reached.
The character ending the consumption is not consumed (i.e.,
the reader cursor is reset just before that character).reader
- the reader to consumepredicate
- the character evaluation conditionnull
)IOException
- could not consume streamIllegalArgumentException
- if reader does not support markpublic static int consumeWhile(Reader reader, IntPredicate predicate, Appendable appendable) throws IOException
true
for a character or the end of stream is reached.
Optionally append the consumed characters to an Appendable
(e.g. StringBuilder).
The character ending the consumption is not consumed (i.e.,
the reader cursor is reset just before that character).reader
- the reader to consumepredicate
- the character evaluation conditionappendable
- optional, to append consumed charactersnull
)IOException
- could not consume streamIllegalArgumentException
- if reader does not support markpublic static void closeQuietly(Closeable... closeable)
IOUtils#closeQuietly(java.io.Closeable)
method,
this one offers an alternate one.closeable
- one or more input streams to close quietlyCopyright © 2008–2023 Norconex Inc.. All rights reserved.