Class KiwiIO
- java.lang.Object
-
- org.kiwiproject.io.KiwiIO
-
public class KiwiIO extends Object
Static I/O utilities.The
closeQuietlymethods that acceptCloseablewere 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. replaceobj != nullchecks withnonNull(obj, etc. as well as adding logging. Did not bother copying all thecloseQuietlymethods that took a specific class such asReader,Writer,Socket, etc. They all implementCloseableand were probably only there because those specific classes pre-dated Java 5 whenCloseablewas added to the JDK, and we assume early (pre-Java 5) versions ofIOUtilsprovided them.
-
-
Constructor Summary
Constructors Constructor Description KiwiIO()
-
Method Summary
-
-
-
Method Detail
-
closeQuietly
public static void closeQuietly(Closeable closeable)
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 objects to close, may be null or already closed- See Also:
Throwable.addSuppressed(java.lang.Throwable)- 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
public static void closeQuietly(Closeable... closeables)
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:
closeQuietly(Closeable),Throwable.addSuppressed(java.lang.Throwable)- 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
public static void closeQuietly(XMLStreamReader xmlStreamReader)
Closes anXMLStreamReaderunconditionally.Since
XMLStreamReaderdoes not implementCloseable, you cannot use a try-with-resources.- Parameters:
xmlStreamReader- theXMLStreamReaderto close- See Also:
Throwable.addSuppressed(java.lang.Throwable)
-
closeQuietly
public static void closeQuietly(XMLStreamWriter xmlStreamWriter)
Closes anXMLStreamWriterunconditionally.Since
XMLStreamWriterdoes not implementCloseable, you cannot use a try-with-resources.- Parameters:
xmlStreamWriter- theXMLStreamWriterto close- See Also:
Throwable.addSuppressed(java.lang.Throwable)
-
newByteArrayInputStreamOfLines
public static ByteArrayInputStream newByteArrayInputStreamOfLines(String... lines)
Return a newly constructedByteArrayInputStreamcontaining the givenlinesseparated by theSystem.lineSeparator().- Parameters:
lines- the lines to convert- Returns:
- a ByteArrayInputStream containing the given lines
-
emptyByteArrayInputStream
public static ByteArrayInputStream emptyByteArrayInputStream()
Return a newly constructed, emptyByteArrayInputStream.- Returns:
- new ByteArrayInputStream
-
readLinesFromErrorStreamOf
public static List<String> readLinesFromErrorStreamOf(Process process)
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
public static List<String> readLinesFromErrorStreamOf(Process process, Charset charset)
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
public static List<String> readLinesFromInputStreamOf(Process process)
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
public static List<String> readLinesFromInputStreamOf(Process process, Charset charset)
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
public static List<String> readLinesFrom(InputStream stream, Charset charset)
- Parameters:
stream- the streamcharset- the charset- Returns:
- a list of strings from the input stream, encoded using the specified charset
-
streamLinesFromErrorStreamOf
public static Stream<String> streamLinesFromErrorStreamOf(Process process)
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
public static Stream<String> streamLinesFromErrorStreamOf(Process process, Charset charset)
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
public static Stream<String> streamLinesFromInputStreamOf(Process process)
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
public static Stream<String> streamLinesFromInputStreamOf(Process process, Charset charset)
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
public static Stream<String> streamLinesFrom(InputStream stream, Charset charset)
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
-
-