Package gw.util

Class StreamUtil


  • public class StreamUtil
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private StreamUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void close​(java.io.Closeable... streams)
      Closes the specified streams, suppressing any IOExceptions for inputstreams and readers.
      private static void close​(java.io.Closeable[] streams, int idx)  
      static void closeNoThrow​(java.io.Closeable stream)
      Close and swallow exception the exception.
      static void copy​(java.io.File fileOrDirectory, java.io.File toDir)
      Recursively copy a file or directory to a directory.
      static void copy​(java.io.InputStream in, java.io.OutputStream out)
      Copies the content of an input stream to an output stream.
      static void copy​(java.io.InputStream in, java.io.Writer writer)
      Copies the content of an input stream to a writer.
      static void copy​(java.io.Reader reader, java.io.OutputStream out)
      Copies the content of a reader to an output stream.
      static void copy​(java.io.Reader in, java.io.Writer out)
      Copies the content of a reader to a writer.
      static void copy​(java.nio.file.Path fileOrDirectory, java.nio.file.Path toDir)
      Recursively copy a file or directory to a directory.
      static byte[] getContent​(java.io.InputStream in)
      Returns the content of the specified input stream.
      static java.lang.String getContent​(java.io.Reader in)
      Returns the content of the specified reader.
      static java.io.Reader getInputStreamReader​(java.io.InputStream in)
      Returns a reader for the specified input stream, using UTF-8 encoding.
      static java.io.Reader getInputStreamReader​(java.io.InputStream in, java.lang.String charset)
      Returns a reader for the specified input stream, using specified encoding.
      static java.io.Writer getOutputStreamWriter​(java.io.OutputStream out)
      Returns a writer for the specified output stream, using UTF-8 encoding.
      static java.io.InputStream getStringInputStream​(java.lang.CharSequence cs)
      Returns an input stream for the specified character sequence, using UTF-8 encoding.
      static byte[] toBytes​(java.lang.CharSequence seq)
      Converts the specified character sequence to bytes using UTF-8.
      static java.util.Properties toProperties​(java.lang.String propFileText)
      Converts the specified property file text to a Properties object.
      static java.lang.String toString​(byte[] bytes)
      Converts the specified byte array to a String using UTF-8.
      static java.lang.String toString​(byte[] bytes, int offset, int length)
      Converts the specified byte array to a String using UTF-8.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • StreamUtil

        private StreamUtil()
    • Method Detail

      • toBytes

        public static byte[] toBytes​(java.lang.CharSequence seq)
        Converts the specified character sequence to bytes using UTF-8.
        Parameters:
        seq - the character sequence to convert
        Returns:
        the UTF-8 encoded result
      • toString

        public static java.lang.String toString​(byte[] bytes)
        Converts the specified byte array to a String using UTF-8.
        Parameters:
        bytes - the bytes to convert
        Returns:
        the resulting string
      • toString

        public static java.lang.String toString​(byte[] bytes,
                                                int offset,
                                                int length)
        Converts the specified byte array to a String using UTF-8.
        Parameters:
        bytes - the bytes to convert
        offset - the index of the first byte to decode
        length - the number of bytes to decode
        Returns:
        the resulting string
      • toProperties

        public static java.util.Properties toProperties​(java.lang.String propFileText)
                                                 throws java.nio.charset.CharacterCodingException
        Converts the specified property file text to a Properties object.
        Parameters:
        propFileText - the property file text in standard property file format
        Returns:
        the resulting Properties object
        Throws:
        java.nio.charset.CharacterCodingException - if invalid encoding
      • getInputStreamReader

        public static java.io.Reader getInputStreamReader​(java.io.InputStream in)
        Returns a reader for the specified input stream, using UTF-8 encoding.
        Parameters:
        in - the input stream to wrap
        Returns:
        a reader for this input stream
      • getInputStreamReader

        public static java.io.Reader getInputStreamReader​(java.io.InputStream in,
                                                          java.lang.String charset)
        Returns a reader for the specified input stream, using specified encoding.
        Parameters:
        in - the input stream to wrap
        charset - the input stream to wrap
        Returns:
        a reader for this input stream
      • getOutputStreamWriter

        public static java.io.Writer getOutputStreamWriter​(java.io.OutputStream out)
        Returns a writer for the specified output stream, using UTF-8 encoding.
        Parameters:
        out - the output stream to wrap
        Returns:
        a writer for this output stream
      • getStringInputStream

        public static java.io.InputStream getStringInputStream​(java.lang.CharSequence cs)
        Returns an input stream for the specified character sequence, using UTF-8 encoding.
        Parameters:
        cs - the character sequence to wrap
        Returns:
        an input stream for reading the specified character sequence
      • getContent

        public static byte[] getContent​(java.io.InputStream in)
                                 throws java.io.IOException
        Returns the content of the specified input stream. The stream will be closed after calling this method.
        Parameters:
        in - the input stream to read
        Returns:
        the content of the input stream
        Throws:
        java.io.IOException - if an I/O error occurs
      • getContent

        public static java.lang.String getContent​(java.io.Reader in)
                                           throws java.io.IOException
        Returns the content of the specified reader. The reader will be closed after calling this method.
        Parameters:
        in - the reader to read
        Returns:
        the content of the reader
        Throws:
        java.io.IOException - if an I/O error occurs
      • copy

        public static void copy​(java.io.InputStream in,
                                java.io.OutputStream out)
                         throws java.io.IOException
        Copies the content of an input stream to an output stream.
        Parameters:
        in - the input stream to read
        out - the output stream to write
        Throws:
        java.io.IOException - if an I/O error occurs
      • copy

        public static void copy​(java.io.InputStream in,
                                java.io.Writer writer)
                         throws java.io.IOException
        Copies the content of an input stream to a writer.
        Parameters:
        in - the input stream to read
        writer - the writer to write
        Throws:
        java.io.IOException - if an I/O error occurs
      • copy

        public static void copy​(java.io.Reader reader,
                                java.io.OutputStream out)
                         throws java.io.IOException
        Copies the content of a reader to an output stream.
        Parameters:
        reader - the reader to read
        out - the output stream to write
        Throws:
        java.io.IOException - if an I/O error occurs
      • copy

        public static void copy​(java.io.Reader in,
                                java.io.Writer out)
                         throws java.io.IOException
        Copies the content of a reader to a writer.
        Parameters:
        in - the reader to read
        out - the writer to write
        Throws:
        java.io.IOException - if an I/O error occurs
      • copy

        public static void copy​(java.io.File fileOrDirectory,
                                java.io.File toDir)
        Recursively copy a file or directory to a directory.
      • copy

        public static void copy​(java.nio.file.Path fileOrDirectory,
                                java.nio.file.Path toDir)
        Recursively copy a file or directory to a directory.
      • closeNoThrow

        public static void closeNoThrow​(java.io.Closeable stream)
        Close and swallow exception the exception. For use in finally blocks where the other io exceptions is what is wanted to be thrown.
        Parameters:
        stream - the streams to close
      • close

        public static void close​(java.io.Closeable... streams)
                          throws java.io.IOException
        Closes the specified streams, suppressing any IOExceptions for inputstreams and readers. Even if an I/O exception is thrown, all streams can be considered closed.
        Parameters:
        streams - the streams to close
        Throws:
        java.io.IOException - if an i/o exception occurs while closing any outputstream or writer
      • close

        private static void close​(java.io.Closeable[] streams,
                                  int idx)
                           throws java.io.IOException
        Throws:
        java.io.IOException