Class IOUtil


  • public final class IOUtil
    extends Object
    Utility methods for operating with java.io.* classes.
    • Method Detail

      • asUncompressedInputStream

        public static InputStream asUncompressedInputStream​(InputStream is)
                                                     throws IOException
        Ensures that the returned stream is an uncompressed version of the supplied input stream.

        This method first tries to read the first two bytes from the stream, then resets the stream. If the first two bytes equal the GZip magic number (see GZIPInputStream.GZIP_MAGIC), the supplied stream is wrapped in a GZIPInputStream. Otherwise, the stream is returned as-is.

        Note: this requires the input stream to support marking.

        Parameters:
        is - the input stream
        Returns:
        an uncompressed version of is
        Throws:
        IOException - if reading the magic number fails
        IllegalArgumentException - if the stream does not support marking
      • asBufferedInputStream

        public static InputStream asBufferedInputStream​(InputStream is)
        Ensures that the returned stream is a buffered version of the supplied input stream. The result must not necessarily be an instance of BufferedInputStream, it can also be, e.g., a ByteArrayInputStream, depending on the type of the supplied input stream.
        Parameters:
        is - the input stream
        Returns:
        a buffered version of is
      • asBufferedInputStream

        public static InputStream asBufferedInputStream​(File file)
                                                 throws IOException
        Returns an input stream that reads the contents of the given file. Additionally, buffers the input stream to improve performance.
        Parameters:
        file - the file to read
        Returns:
        a buffered input stream for the file contents
        Throws:
        IOException - if accessing the file results in an I/O error
      • asBufferedOutputStream

        public static OutputStream asBufferedOutputStream​(OutputStream os)
        Ensures that the returned stream is a buffered version of the supplied output stream. The result must not necessarily be an instance of BufferedOutputStream, it can also be, e.g., a ByteArrayOutputStream, depending on the type of the supplied output stream.
        Parameters:
        os - the output stream
        Returns:
        a buffered version of os
      • asBufferedOutputStream

        public static OutputStream asBufferedOutputStream​(File file)
                                                   throws IOException
        Returns an output stream that writes the contents to the given file. Additionally, buffers the input stream to improve performance.
        Parameters:
        file - the file to write to
        Returns:
        a buffered output stream for the file contents
        Throws:
        IOException - if accessing the file results in an I/O error
      • asBufferedUTF8Reader

        public static Reader asBufferedUTF8Reader​(File file)
                                           throws IOException
        Returns a reader that parses the contents of the given file with StandardCharsets.UTF_8 encoding. Additionally, buffers the input stream to improve performance.
        Parameters:
        file - the file to read
        Returns:
        a buffered, UTF-8-decoding reader for the file contents
        Throws:
        IOException - if accessing the file results in an I/O error
      • asBufferedUTF8Reader

        public static Reader asBufferedUTF8Reader​(InputStream is)
        Returns a reader that parses the contents of the given input stream with StandardCharsets.UTF_8 encoding. If the given input stream is not already a buffering input stream, additionally buffers the input stream to improve performance.

        Implementation note: the input stream (byte-wise representation) will be buffered, not the reader (character-wise representation).

        Parameters:
        is - the input stream to read
        Returns:
        a buffered, UTF-8-decoding reader for the input stream.
      • asUTF8Reader

        public static Reader asUTF8Reader​(InputStream is)
        Returns a reader that parses the contents of the given input stream with StandardCharsets.UTF_8 encoding.
        Parameters:
        is - the input stream to read
        Returns:
        a UTF-8-decoding reader for the input stream
      • asBufferedUTF8Writer

        public static Writer asBufferedUTF8Writer​(File file)
                                           throws IOException
        Returns a writer that writes contents to the given file with StandardCharsets.UTF_8 encoding. Additionally, buffers the input stream to improve performance.
        Parameters:
        file - the file to write to
        Returns:
        a buffered, UTF-8-encoding writer for the file contents
        Throws:
        IOException - if writing to the file results in I/O errors
      • asBufferedUTF8Writer

        public static Writer asBufferedUTF8Writer​(OutputStream os)
        Returns a writer that writes contents to the given output stream with StandardCharsets.UTF_8 encoding. If the given output stream is not already a buffering output stream, additionally buffers the output stream to improve performance.

        Implementation note: the output stream (byte-wise representation) will be buffered, not the writer (character- wise representation).

        Parameters:
        os - the output stream to write to
        Returns:
        a buffered, UTF-8 encoding writer for the output stream
      • asUTF8Writer

        public static Writer asUTF8Writer​(OutputStream os)
        Returns a writer that writes contents to the given output stream with StandardCharsets.UTF_8 encoding.
        Parameters:
        os - the output stream to write to
        Returns:
        a UTF-8-encoding writer for the output stream.
      • asUncompressedBufferedNonClosingUTF8Reader

        public static Reader asUncompressedBufferedNonClosingUTF8Reader​(InputStream is)
                                                                 throws IOException
        Returns a buffered reader that un-compresses the contents of is (in case the given input stream contains gzip'ed content), does not propagate calls to Reader.close() to the passed is and parses the contents of the given input stream with StandardCharsets.UTF_8 encoding.

        Implementation note: the input stream (byte-wise representation) will be buffered, not the reader (character-wise representation).

        Parameters:
        is - the input stream to read
        Returns:
        a (potentially) de-compressing, buffered, non-closing, UTF-8-decoding version of is
        Throws:
        IOException - if reading the stream (for detecting whether it contains compressed contents) fails
        See Also:
        asUTF8Reader(InputStream), asUncompressedBufferedNonClosingInputStream(InputStream)