Class KiwiIO


  • public class KiwiIO
    extends Object
    Static I/O utilities.

    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.

    • Constructor Detail

      • KiwiIO

        public KiwiIO()
    • Method Detail

      • closeQuietly

        public static void closeQuietly​(Closeable closeable)
        Closes a Closeable unconditionally.

        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 a Closeable unconditionally.

        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."
      • readLinesFromErrorStreamOf

        public static List<String> readLinesFromErrorStreamOf​(Process process)
        Return a List of Strings from the error stream of the given Process using UTF-8 for 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 a List of Strings from the error stream of the given Process using the specified Charset for the String encoding.
        Parameters:
        process - the process
        charset - the charset
        Returns:
        the list of UTF-8 encoded strings from the process' error stream
      • readLinesFromInputStreamOf

        public static List<String> readLinesFromInputStreamOf​(Process process)
        Return a List of Strings from the input stream of the given Process using UTF-8 for 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 a List of Strings from the input stream of the given Process using the specified Charset for the String encoding.
        Parameters:
        process - the process
        charset - 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)
        Return a List of Strings from the input stream using the specified Charset for the String encoding.
        Parameters:
        stream - the stream
        charset - 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 a Stream of Strings from the error stream of the given Process using UTF-8 for 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 a Stream of Strings from the error stream of the given Process using the specified Charset for the String encoding.
        Parameters:
        process - the process
        charset - 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 a Stream of Strings from the input stream of the given Process using UTF-8 for 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 a Stream of Strings from the input stream of the given Process using the specified Charset for the String encoding.
        Parameters:
        process - the process
        charset - 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 a Stream of Strings from the given InputStream using the specified Charset for the String encoding.
        Parameters:
        stream - the stream
        charset - 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 give Process as a String using UTF-8 as 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 give Process as a String using the the specified Charset for 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
        charset - 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 give Process as a String using UTF-8 as 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 give Process as a String using the the specified Charset for 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
        charset - 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 given InputStream to a UTF-8 encoded 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 given InputStream to a String using the given Charset for the string encoding.
        Parameters:
        inputStream - the input stream
        charset - the charset
        Returns:
        the input stream as a string, encoded using the specified charset