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
All Methods Static Methods Concrete Methods Modifier and Type Method Description 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 ByteArrayInputStreamemptyByteArrayInputStream()Return a newly constructed, emptyByteArrayInputStream.static ByteArrayInputStreamnewByteArrayInputStreamOfLines(String... lines)Return a newly constructedByteArrayInputStreamcontaining the givenlinesseparated by theSystem.lineSeparator().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)static List<String>readLinesFrom(InputStream stream, Charset charset)static List<String>readLinesFromErrorStreamOf(Process process)static List<String>readLinesFromErrorStreamOf(Process process, Charset charset)static List<String>readLinesFromInputStreamOf(Process process)static List<String>readLinesFromInputStreamOf(Process process, Charset charset)static Stream<String>streamLinesFrom(InputStream stream, Charset charset)Return aStreamofStrings from the givenInputStreamusing the specifiedCharsetfor the String encoding.static Stream<String>streamLinesFromErrorStreamOf(Process process)static Stream<String>streamLinesFromErrorStreamOf(Process process, Charset charset)static Stream<String>streamLinesFromInputStreamOf(Process process)static Stream<String>streamLinesFromInputStreamOf(Process process, Charset charset)
-
-
-
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
-
readInputStreamOf
public static String readInputStreamOf(Process process)
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:
Process.getInputStream()
-
readInputStreamOf
public static String readInputStreamOf(Process process, Charset charset)
Read the input stream of the giveProcessas a String using the 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:
Process.getInputStream()
-
readErrorStreamOf
public static String readErrorStreamOf(Process process)
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:
Process.getErrorStream()
-
readErrorStreamOf
public static String readErrorStreamOf(Process process, Charset charset)
Read the error stream of the giveProcessas a String using the 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:
Process.getErrorStream()
-
readInputStreamAsString
public static String readInputStreamAsString(InputStream inputStream)
Convert the givenInputStreamto aUTF-8encoded String.- Parameters:
inputStream- the input stream- Returns:
- the input stream as a UTF-8 encoded string
-
readInputStreamAsString
public static String readInputStreamAsString(InputStream inputStream, Charset charset)
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
-
-