Class Streams


  • public abstract class Streams
    extends java.lang.Object
    Simple utility methods for file and stream copying. All copy methods use a block size of 4096 bytes, and close all affected streams when done.

    Mainly for use within the framework, but also useful for application code.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int BUFFER_SIZE  
    • Constructor Summary

      Constructors 
      Constructor Description
      Streams()  
    • Method Summary

      Modifier and Type Method Description
      static void copy​(byte[] in, java.io.OutputStream out)
      Copy the contents of the given byte array to the given OutputStream.
      static long copy​(java.io.InputStream in, java.io.OutputStream out)  
      static long copy​(java.io.InputStream in, java.io.OutputStream out, byte[] buffer)
      Copy the contents of the given InputStream to the given OutputStream.
      static int copy​(java.io.Reader in, java.io.Writer out)
      Copy the contents of the given Reader to the given Writer.
      static void copy​(java.lang.String in, java.io.Writer out)
      Copy the contents of the given String to the given output Writer.
      static java.lang.String copyToString​(java.io.Reader in)
      Copy the contents of the given Reader into a String.
      static BytesStream flushOnCloseStream​(BytesStream os)
      Wraps the given BytesStream in a StreamOutput that simply flushes when close is called.
      static java.util.List<java.lang.String> readAllLines​(java.io.InputStream input)  
      static void readAllLines​(java.io.InputStream input, java.util.function.Consumer<java.lang.String> consumer)  
      static int readFully​(java.io.InputStream reader, byte[] dest)  
      static int readFully​(java.io.InputStream reader, byte[] dest, int offset, int len)  
      static int readFully​(java.io.Reader reader, char[] dest)  
      static int readFully​(java.io.Reader reader, char[] dest, int offset, int len)  
      • Methods inherited from class java.lang.Object

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

      • Streams

        public Streams()
    • Method Detail

      • copy

        public static long copy​(java.io.InputStream in,
                                java.io.OutputStream out)
                         throws java.io.IOException
        Throws:
        java.io.IOException
      • copy

        public static long copy​(java.io.InputStream in,
                                java.io.OutputStream out,
                                byte[] buffer)
                         throws java.io.IOException
        Copy the contents of the given InputStream to the given OutputStream. Closes both streams when done.
        Parameters:
        in - the stream to copy from
        out - the stream to copy to
        Returns:
        the number of bytes copied
        Throws:
        java.io.IOException - in case of I/O errors
      • copy

        public static void copy​(byte[] in,
                                java.io.OutputStream out)
                         throws java.io.IOException
        Copy the contents of the given byte array to the given OutputStream. Closes the stream when done.
        Parameters:
        in - the byte array to copy from
        out - the OutputStream to copy to
        Throws:
        java.io.IOException - in case of I/O errors
      • copy

        public static int copy​(java.io.Reader in,
                               java.io.Writer out)
                        throws java.io.IOException
        Copy the contents of the given Reader to the given Writer. Closes both when done.
        Parameters:
        in - the Reader to copy from
        out - the Writer to copy to
        Returns:
        the number of characters copied
        Throws:
        java.io.IOException - in case of I/O errors
      • copy

        public static void copy​(java.lang.String in,
                                java.io.Writer out)
                         throws java.io.IOException
        Copy the contents of the given String to the given output Writer. Closes the write when done.
        Parameters:
        in - the String to copy from
        out - the Writer to copy to
        Throws:
        java.io.IOException - in case of I/O errors
      • copyToString

        public static java.lang.String copyToString​(java.io.Reader in)
                                             throws java.io.IOException
        Copy the contents of the given Reader into a String. Closes the reader when done.
        Parameters:
        in - the reader to copy from
        Returns:
        the String that has been copied to
        Throws:
        java.io.IOException - in case of I/O errors
      • readFully

        public static int readFully​(java.io.Reader reader,
                                    char[] dest)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • readFully

        public static int readFully​(java.io.Reader reader,
                                    char[] dest,
                                    int offset,
                                    int len)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • readFully

        public static int readFully​(java.io.InputStream reader,
                                    byte[] dest)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • readFully

        public static int readFully​(java.io.InputStream reader,
                                    byte[] dest,
                                    int offset,
                                    int len)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • readAllLines

        public static java.util.List<java.lang.String> readAllLines​(java.io.InputStream input)
                                                             throws java.io.IOException
        Throws:
        java.io.IOException
      • readAllLines

        public static void readAllLines​(java.io.InputStream input,
                                        java.util.function.Consumer<java.lang.String> consumer)
                                 throws java.io.IOException
        Throws:
        java.io.IOException