Class KiwiIO
The closeQuietly methods that accept Closeable were copied directly from Apache Commons I/O and
the deprecation warnings and annotations removed. While they should not be used often, sometimes they might come in
handy, so we want to keep them around for posterity. Slight style modifications were made (e.g. replace obj != null
checks with nonNull(obj, etc.) as well as adding logging. Did not bother copying all the closeQuietly
methods that took a specific class such as Reader, Writer, Socket, etc.
They all implement Closeable and were probably only there because those specific classes pre-dated Java 5 when
Closeable was added to the JDK, and we assume early (pre-Java 5) versions of IOUtils provided them.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordRepresents a resource that can be closed using a "close" method. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcloseObjectQuietly(Object object) Closes an object unconditionally.static voidcloseObjectQuietly(String closeMethodName, Object object) Closes an object unconditionally.static voidcloseObjectsQuietly(Object... objects) Closes one or more objects unconditionally.static voidcloseObjectsQuietly(String closeMethodName, Object... objects) Closes one or more objects unconditionally.static voidcloseQuietly(Closeable closeable) Closes aCloseableunconditionally.static voidcloseQuietly(Closeable... closeables) Closes aCloseableunconditionally.static voidcloseQuietly(XMLStreamReader xmlStreamReader) Closes anXMLStreamReaderunconditionally.static voidcloseQuietly(XMLStreamWriter xmlStreamWriter) Closes anXMLStreamWriterunconditionally.static voidcloseResourceQuietly(KiwiIO.CloseableResource closeableResource) Closes a resource unconditionally.Return the default method names used when closing objects using any of the methods to close genericObject.static ByteArrayInputStreamReturn a newly constructed, emptyByteArrayInputStream.static ByteArrayInputStreamnewByteArrayInputStream(String value) Creates a newByteArrayInputStreamcontaining the bytes of the given string using the UTF-8 character set.static ByteArrayInputStreamnewByteArrayInputStream(String value, Charset charset) Creates a newByteArrayInputStreamcontaining the bytes of the given string using the specified character set.static ByteArrayInputStreamnewByteArrayInputStreamOfLines(String... lines) Return a newly constructedByteArrayInputStreamcontaining the givenlinesseparated by theSystem.lineSeparator()and using UTF-8 as theCharsetwhen converting the joined lines to bytes.static StringreadErrorStreamOf(Process process) Read the error stream of the giveProcessas a String usingUTF-8as the string encoding.static StringreadErrorStreamOf(Process process, Charset charset) static StringreadInputStreamAsString(InputStream inputStream) Convert the givenInputStreamto aUTF-8encoded String.static StringreadInputStreamAsString(InputStream inputStream, Charset charset) Convert the givenInputStreamto a String using the givenCharsetfor the string encoding.static StringreadInputStreamOf(Process process) Read the input stream of the giveProcessas a String usingUTF-8as the String encoding.static StringreadInputStreamOf(Process process, Charset charset) readLinesFrom(InputStream stream, Charset charset) readLinesFromErrorStreamOf(Process process) readLinesFromErrorStreamOf(Process process, Charset charset) readLinesFromInputStreamOf(Process process) readLinesFromInputStreamOf(Process process, Charset charset) streamLinesFrom(InputStream stream, Charset charset) Return aStreamofStrings from the givenInputStreamusing the specifiedCharsetfor the String encoding.streamLinesFromErrorStreamOf(Process process) streamLinesFromErrorStreamOf(Process process, Charset charset) streamLinesFromInputStreamOf(Process process) streamLinesFromInputStreamOf(Process process, Charset charset)
-
Method Details
-
closeQuietly
Closes aCloseableunconditionally.Equivalent to
Closeable.close(), except any exceptions will be ignored. This is typically used in finally blocks.Example code:
Closeable closeable = null; try { closeable = new FileReader("foo.txt"); // process closeable closeable.close(); } catch (Exception e) { // error handling } finally { IOUtils.closeQuietly(closeable); }Closing all streams:
try { return IOUtils.copy(inputStream, outputStream); } finally { IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outputStream); }- Parameters:
closeable- the object to close, may be null or already closed- See Also:
- Implementation Note:
- Copied from Apache Commons I/O's IOUtils once it became deprecated with the message "Please use the try-with-resources statement or handle suppressed exceptions manually."
-
closeQuietly
Closes aCloseableunconditionally.Equivalent to
Closeable.close(), except any exceptions will be ignored.This is typically used in finally blocks to ensure that the closeable is closed even if an Exception was thrown before the normal close statement was reached.
It should not be used to replace the close statement(s) which should be present for the non-exceptional case.
It is only intended to simplify tidying up where normal processing has already failed and reporting close failure as well is not necessary or useful.Example code:
Closeable closeable = null; try { closeable = new FileReader("foo.txt"); // processing using the closeable; may throw an Exception closeable.close(); // Normal close - exceptions not ignored } catch (Exception e) { // error handling } finally { IOUtils.closeQuietly(closeable); // In case normal close was skipped due to Exception }Closing all streams:
try { return IOUtils.copy(inputStream, outputStream); } finally { IOUtils.closeQuietly(inputStream, outputStream); }- Parameters:
closeables- the objects to close, may be null or already closed- See Also:
- Implementation Note:
- Copied from Apache Commons I/O's IOUtils once it became deprecated with the message "Please use the try-with-resources statement or handle suppressed exceptions manually."
-
closeQuietly
Closes anXMLStreamReaderunconditionally.Since
XMLStreamReaderdoes not implementCloseable, you cannot use a try-with-resources.- Parameters:
xmlStreamReader- theXMLStreamReaderto close- See Also:
-
closeQuietly
Closes anXMLStreamWriterunconditionally.Since
XMLStreamWriterdoes not implementCloseable, you cannot use a try-with-resources.- Parameters:
xmlStreamWriter- theXMLStreamWriterto close- See Also:
-
defaultCloseMethodNames
Return the default method names used when closing objects using any of the methods to close genericObject.These method names are tried in order when attempting to close an Object when no explicit close method name is provided.
The default names are
- Returns:
- the default close method names
-
closeObjectQuietly
Closes an object unconditionally. This method ignores null objects and exceptions.The object may be a
KiwiIO.CloseableResource.Uses the default close method names.
- Parameters:
object- the object to close, may be null or already closed- See Also:
-
closeObjectQuietly
Closes an object unconditionally. This method ignores null objects and exceptions.The object may not be a
KiwiIO.CloseableResource, since it could contain a different close method name.- Parameters:
closeMethodName- the name of the close methodobject- the object to close, may be null or already closed- Throws:
IllegalArgumentException- if closeMethodName is blank or object is a CloseableResource
-
closeObjectsQuietly
Closes one or more objects unconditionally. This method ignores null objects and exceptions.The objects may contain
KiwiIO.CloseableResourceand/or other closeable objects.Uses the default close method names.
- Parameters:
objects- the objects to close, may be null or already closed- See Also:
-
closeObjectsQuietly
Closes one or more objects unconditionally. This method ignores null objects and exceptions.The objects should not contain any
KiwiIO.CloseableResourceinstances. The reason is that those could specify a different close method name.- Parameters:
closeMethodName- the name of the close methodobjects- the objects to close, may be null or already closed- Throws:
IllegalArgumentException- of objects contains any CloseableResource instances
-
closeResourceQuietly
Closes a resource unconditionally. This method ignores null objects and exceptions.The object inside the resource may be null or already closed. The resource must contain at least one close method name.
- Parameters:
closeableResource- the resource to close, must not be null- Throws:
IllegalArgumentException- if the closeableResource is null or has no close method names
-
newByteArrayInputStreamOfLines
Return a newly constructedByteArrayInputStreamcontaining the givenlinesseparated by theSystem.lineSeparator()and using UTF-8 as theCharsetwhen converting the joined lines to bytes.- Parameters:
lines- the lines to convert- Returns:
- a ByteArrayInputStream containing the given lines, encoded using UTF-8
-
newByteArrayInputStream
Creates a newByteArrayInputStreamcontaining the bytes of the given string using the UTF-8 character set.Note: The UTF-8 character set is widely used and supports a vast range of characters, making it suitable for most applications. However, if the string was encoded using a different character set, use the other version of this method that accepts a
Charsetto specify the character set that was used to encode the string.- Parameters:
value- the string from which to create the ByteArrayInputStream- Returns:
- a new ByteArrayInputStream initialized with bytes from the provided string
- Throws:
IllegalArgumentException- if the input string is null
-
newByteArrayInputStream
Creates a newByteArrayInputStreamcontaining the bytes of the given string using the specified character set.- Parameters:
value- the string from which to create the ByteArrayInputStreamcharset- the character set used to encode the string as bytes- Returns:
- a new ByteArrayInputStream initialized with bytes from the provided string
- Throws:
IllegalArgumentException- if the input string or charset is null
-
emptyByteArrayInputStream
Return a newly constructed, emptyByteArrayInputStream.- Returns:
- new ByteArrayInputStream
-
readLinesFromErrorStreamOf
Return aListofStrings from the error stream of the givenProcessusingUTF-8for the String encoding.- Parameters:
process- the process- Returns:
- the list of UTF-8 encoded strings from the process' error stream
-
readLinesFromErrorStreamOf
Return aListofStrings from the error stream of the givenProcessusing the specifiedCharsetfor the String encoding.- Parameters:
process- the processcharset- the charset- Returns:
- the list of UTF-8 encoded strings from the process' error stream
-
readLinesFromInputStreamOf
Return aListofStrings from the input stream of the givenProcessusingUTF-8for the String encoding.- Parameters:
process- the process- Returns:
- the list of UTF-8 encoded strings from the process' input stream
-
readLinesFromInputStreamOf
Return aListofStrings from the input stream of the givenProcessusing the specifiedCharsetfor the String encoding.- Parameters:
process- the processcharset- the charset- Returns:
- the list of UTF-8 encoded strings from the process' input stream
-
readLinesFrom
- Parameters:
stream- the streamcharset- the charset- Returns:
- a list of strings from the input stream, encoded using the specified charset
-
streamLinesFromErrorStreamOf
Return aStreamofStrings from the error stream of the givenProcessusingUTF-8for the String encoding.- Parameters:
process- the process- Returns:
- the stream of UTF-8 encoded strings from the process' error stream
-
streamLinesFromErrorStreamOf
Return aStreamofStrings from the error stream of the givenProcessusing the specifiedCharsetfor the String encoding.- Parameters:
process- the processcharset- the charset- Returns:
- the stream of strings from the process' error stream, encoded using the specified charset
-
streamLinesFromInputStreamOf
Return aStreamofStrings from the input stream of the givenProcessusingUTF-8for the String encoding.- Parameters:
process- the process- Returns:
- the stream of UTF-8 encoded strings from the process' input stream
-
streamLinesFromInputStreamOf
Return aStreamofStrings from the input stream of the givenProcessusing the specifiedCharsetfor the String encoding.- Parameters:
process- the processcharset- the charset- Returns:
- the stream of strings from the process' input stream, encoded using the specified charset
-
streamLinesFrom
Return aStreamofStrings from the givenInputStreamusing the specifiedCharsetfor the String encoding.- Parameters:
stream- the streamcharset- the charset- Returns:
- the stream of strings from the input stream, encoded using the specified charset
-
readInputStreamOf
Read the input stream of the giveProcessas a String usingUTF-8as the String encoding.Note that process output may contain one or more lines, which will therefore include line termination characters within or at the end of the returned string.
- Parameters:
process- the process- Returns:
- the process' input stream as a UTF-8 encoded string
- See Also:
-
readInputStreamOf
Read the input stream of the giveProcessas a String using the specifiedCharsetfor the string encoding.Note that process output may contain one or more lines, which will therefore include line termination characters within or at the end of the returned string.
- Parameters:
process- the processcharset- the charset- Returns:
- the process' input stream as a string, encoded using the specified charset
- See Also:
-
readErrorStreamOf
Read the error stream of the giveProcessas a String usingUTF-8as the string encoding.Note that process output may contain one or more lines, which will therefore include line termination characters within or at the end of the returned string.
- Parameters:
process- the process- Returns:
- the process' error stream as a UTF-8 encoded string
- See Also:
-
readErrorStreamOf
Read the error stream of the giveProcessas a String using the specifiedCharsetfor the string encoding.Note that process output may contain one or more lines, which will therefore include line termination characters within or at the end of the returned string.
- Parameters:
process- the processcharset- the charset- Returns:
- the process' error stream as a string, encoded using the specified charset
- See Also:
-
readInputStreamAsString
Convert the givenInputStreamto aUTF-8encoded String.- Parameters:
inputStream- the input stream- Returns:
- the input stream as a UTF-8 encoded string
-
readInputStreamAsString
Convert the givenInputStreamto a String using the givenCharsetfor the string encoding.- Parameters:
inputStream- the input streamcharset- the charset- Returns:
- the input stream as a string, encoded using the specified charset
-