Package com.norconex.commons.lang.io
Class IoUtil
java.lang.Object
com.norconex.commons.lang.io.IoUtil
I/O related utility methods.
-
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]borrowBytes(@NonNull InputStream is, int qty) Gets and resets the specified number of bytes from the input stream.static char[]borrowCharacters(@NonNull Reader reader, int qty) Gets and resets the specified number of characters from the reader.static voidcloseQuietly(Closeable... closeable) Given Apache has deprecated itsIOUtils#closeQuietly(java.io.Closeable)method, this one offers an alternative.static intconsume(InputStream is) Fully consumes an input stream.static intFully consumes an input stream.static intconsumeUntil(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 intconsumeUntil(Reader reader, IntPredicate predicate) Consumes markable reader characters until the predicate returnstruefor a character or the end of stream is reached.static intconsumeUntil(Reader reader, IntPredicate predicate, Appendable appendable) Consumes markable reader characters until the predicate returnstruefor a character or the end of stream is reached.static intconsumeWhile(Reader reader, IntPredicate predicate) Consumes markable reader characters while the predicate returnstruefor a character or the end of stream is reached.static intconsumeWhile(Reader reader, IntPredicate predicate, Appendable appendable) Consumes markable reader characters while the predicate returnstruefor a character or the end of stream is reached.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 String[]head(InputStream is, Charset encoding, int lineQty) Gets the first lines from an input stream, using the specified encoding.static booleanisEmpty(InputStream is) Gets whether the given input stream isnullor empty.static booleanGets whether the given Reader isnullor empty.static booleanstartsWith(InputStream is, byte[] bytes) Whether the given input stream starts the specified bytes array or not.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 String[]tail(InputStream is, Charset encoding, int lineQty) Gets the last lines from an input stream, using the specified encoding.static BufferedInputStreamtoBufferedInputStream(@NonNull InputStream in) Wraps the input stream in aBufferedInputStreamif not a subclass already.static BufferedReadertoBufferedReader(@NonNull Reader reader) Wraps the reader in aBufferedReaderif not a subclass already.static InputStreamGets an "empty" input stream (zero size) when the supplied input stream isnull.static OutputStreamGets a non-effective output stream when the supplied output stream isnull(writing to it has no effect).static ReadertoNonNullReader(Reader reader) Gets an "empty" reader (zero size) when the supplied reader isnull.static WritertoNonNullWriter(Writer writer) Gets a non-effective writer when the supplied writer isnull(writing to it has no effect).
-
Method Details
-
startsWith
Whether the given input stream starts the specified bytes array or not. The input stream must support marking. If the byte array or the input stream is null, thenfalseis returned.- Parameters:
is- input streambytes- byte array to compare- Returns:
trueif input stream starts with byte array- Throws:
IOException- ifInputStream.markSupported()returns false
-
borrowBytes
Gets and resets the specified number of bytes from the input stream. Must support marks.- Parameters:
is- input streamqty- number of bytes to read- Returns:
- byte array of length matching the quantity requested
- Throws:
IOException- ifInputStream.markSupported()returns false- Since:
- 2.0.0
-
borrowCharacters
Gets and resets the specified number of characters from the reader. Must support marks.- Parameters:
reader- readerqty- number of characters to read- Returns:
- char array of length matching the quantity requested
- Throws:
IOException- ifReader.markSupported()returns false- Since:
- 2.0.0
-
isEmpty
Gets whether the given input stream isnullor empty. Must support marks.- Parameters:
is- input stream- Returns:
trueifnullor empty- Throws:
IOException- ifInputStream.markSupported()returns false- Since:
- 2.0.0
-
isEmpty
Gets whether the given Reader isnullor empty. Must support marks.- Parameters:
reader- reader- Returns:
trueifnullor empty- Throws:
IOException- ifReader.markSupported()returns false- Since:
- 2.0.0
-
toBufferedReader
Wraps the reader in aBufferedReaderif not a subclass already.- Parameters:
reader- the reader to wrap if needed- Returns:
- buffered reader
- Since:
- 1.6.0
-
toBufferedInputStream
Wraps the input stream in aBufferedInputStreamif not a subclass already.- Parameters:
in- the input stream to wrap if needed- Returns:
- buffered input stream
- Since:
- 1.6.0
-
tail
Gets the last lines from an input stream, using UTF-8. This method is null-safe. If the input stream is null or empty, an empty string array will be returned.
Use of this method can often be a bad idea (especially on large streams) since it needs to read the entire stream to return the last lines. If you are dealing with files, useFileUtil.tail(java.io.File, int)instead, which can read a file starting from the end.- Parameters:
is- input streamlineQty- maximum number of lines to return- Returns:
- lines as a string array
- Throws:
IOException- problem reading lines
-
tail
Gets the last lines from an input stream, using the specified encoding. This method is null-safe. If the input stream is null or empty, an empty string array will be returned.
Use of this method can often be a bad idea (especially on large streams) since it needs to read the entire stream to return the last lines. If you are dealing with files, useFileUtil.tail(File, String, int)instead, which can read a file starting from the end.- Parameters:
is- input streamencoding- character encodinglineQty- maximum number of lines to return- Returns:
- lines as a string array
- Throws:
IOException- problem reading lines- Since:
- 1.5.0
-
tail
Gets the last lines from an input stream, using the specified encoding. This method is null-safe. If the input stream is null or empty, an empty string array will be returned.
Use of this method can often be a bad idea (especially on large streams) since it needs to read the entire stream to return the last lines. If you are dealing with files, useFileUtil.tail(File, String, int)instead, which can read a file starting from the end.- Parameters:
is- input streamencoding- character encodinglineQty- maximum number of lines to return- Returns:
- lines as a string array
- Throws:
IOException- problem reading lines- Since:
- 1.14.0
-
head
Gets the first lines from an input stream, using UTF-8. This method is null-safe. If the input stream is null or empty, an empty string array will be returned.- Parameters:
is- input streamlineQty- maximum number of lines to return- Returns:
- lines as a string array
- Throws:
IOException- problem reading lines
-
head
Gets the first lines from an input stream, using the specified encoding. This method is null-safe. If the input stream is null or empty, an empty string array will be returned.- Parameters:
is- input streamencoding- character encodinglineQty- maximum number of lines to return- Returns:
- lines as a string array
- Throws:
IOException- problem reading lines
-
head
Gets the first lines from an input stream, using the specified encoding. This method is null-safe. If the input stream is null or empty, an empty string array will be returned.- Parameters:
is- input streamencoding- character encodinglineQty- maximum number of lines to return- Returns:
- lines as a string array
- Throws:
IOException- problem reading lines- Since:
- 1.14.0
-
toNonNullReader
Gets an "empty" reader (zero size) when the supplied reader isnull. Else, return the supplied reader.- Parameters:
reader- original reader- Returns:
- the supplied reader, or an empty reader
- Since:
- 2.0.0
-
toNonNullInputStream
Gets an "empty" input stream (zero size) when the supplied input stream isnull. Else, return the supplied input stream.- Parameters:
is- original input stream- Returns:
- the supplied input stream, or an empty input stream
- Since:
- 2.0.0
-
toNonNullWriter
Gets a non-effective writer when the supplied writer isnull(writing to it has no effect). Else, return the supplied writer.- Parameters:
writer- original writer- Returns:
- the supplied writer, or an empty writer
- Since:
- 3.0.0
-
toNonNullOutputStream
Gets a non-effective output stream when the supplied output stream isnull(writing to it has no effect). Else, return the supplied output stream.- Parameters:
os- original output stream- Returns:
- the supplied output stream, or an empty output stream
- Since:
- 3.0.0
-
consume
Fully consumes an input stream.- Parameters:
is- input stream- Returns:
- number of bytes consumed
- Throws:
IOException- could not consume stream- Since:
- 2.0.0
-
consume
Fully consumes an input stream.- Parameters:
reader- reader- Returns:
- number of characters consumed
- Throws:
IOException- could not consume reader- Since:
- 2.0.0
-
consumeUntil
Consumes markable reader characters until the predicate returnstruefor 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).- Parameters:
reader- the reader to consumepredicate- the character evaluation condition- Returns:
- number of characters consumed (0 if reader is
null) - Throws:
IOException- could not consume streamIllegalArgumentException- if reader does not support mark- Since:
- 2.0.0
-
consumeUntil
public static int consumeUntil(Reader reader, IntPredicate predicate, Appendable appendable) throws IOException Consumes markable reader characters until the predicate returnstruefor a character or the end of stream is reached. Optionally append the consumed characters to anAppendable(e.g. StringBuilder). The character ending the consumption is not consumed (i.e., the reader cursor is reset just before that character).- Parameters:
reader- the reader to consumepredicate- the character evaluation conditionappendable- optional, to append consumed characters- Returns:
- number of characters consumed (0 if reader is
null) - Throws:
IOException- could not consume streamIllegalArgumentException- if reader does not support mark- Since:
- 2.0.0
-
consumeUntil
Consumes reader characters until after encountering the supplied string (the matching string is also consumed) or the end of stream is reached. Optionally append the consumed characters to anAppendable(e.g. StringBuilder). The matching string is also consumed.- Parameters:
reader- the reader to consumestr- the string to matchappendable- optional, to append consumed characters- Returns:
- number of characters consumed (0 if reader is
null) - Throws:
IOException- could not consume stream- Since:
- 2.0.0
-
consumeWhile
Consumes markable reader characters while the predicate returnstruefor 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).- Parameters:
reader- the reader to consumepredicate- the character evaluation condition- Returns:
- number of characters consumed (0 if reader is
null) - Throws:
IOException- could not consume streamIllegalArgumentException- if reader does not support mark- Since:
- 2.0.0
-
consumeWhile
public static int consumeWhile(Reader reader, IntPredicate predicate, Appendable appendable) throws IOException Consumes markable reader characters while the predicate returnstruefor a character or the end of stream is reached. Optionally append the consumed characters to anAppendable(e.g. StringBuilder). The character ending the consumption is not consumed (i.e., the reader cursor is reset just before that character).- Parameters:
reader- the reader to consumepredicate- the character evaluation conditionappendable- optional, to append consumed characters- Returns:
- number of characters consumed (0 if reader is
null) - Throws:
IOException- could not consume streamIllegalArgumentException- if reader does not support mark- Since:
- 2.0.0
-
closeQuietly
Given Apache has deprecated itsIOUtils#closeQuietly(java.io.Closeable)method, this one offers an alternative.- Parameters:
closeable- one or more input streams to close quietly- Since:
- 2.0.0
-