Package gw.util

Class StreamUtil

java.lang.Object
gw.util.StreamUtil

public class StreamUtil extends Object
  • Constructor Details

    • StreamUtil

      private StreamUtil()
  • Method Details

    • toBytes

      public static byte[] toBytes(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 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 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 Properties toProperties(String propFileText) throws 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:
      CharacterCodingException - if invalid encoding
    • getInputStreamReader

      public static Reader getInputStreamReader(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 Reader getInputStreamReader(InputStream in, 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 Writer getOutputStreamWriter(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 InputStream getStringInputStream(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(InputStream in) throws 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:
      IOException - if an I/O error occurs
    • getContent

      public static String getContent(Reader in) throws 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:
      IOException - if an I/O error occurs
    • copy

      public static void copy(InputStream in, OutputStream out) throws 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:
      IOException - if an I/O error occurs
    • copy

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

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

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

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

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

      public static void closeNoThrow(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(Closeable... streams) throws 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:
      IOException - if an i/o exception occurs while closing any outputstream or writer
    • close

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