Class IOUtils


  • public final class IOUtils
    extends java.lang.Object
    Largely based on Apache IOUtils

    Only needed methods copied, like copy InputStream -> OutoutStream and copy Reader -> Writer

    Javadoc maintained where possible, but the "since" tag was largely removed

    • Constructor Summary

      Constructors 
      Constructor Description
      IOUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static long copy​(java.io.InputStream input, java.io.OutputStream output, int bufferSize)
      Copies bytes from an InputStream to an OutputStream using an internal buffer of the given size.
      static int copy​(java.io.Reader input, java.io.Writer output)
      Copies chars from a Reader to a Writer.
      static long copyLarge​(java.io.InputStream input, java.io.OutputStream output)
      Copies bytes from a large (over 2GB) InputStream to an OutputStream.
      static long copyLarge​(java.io.InputStream input, java.io.OutputStream output, byte[] buffer)
      Copies bytes from a large (over 2GB) InputStream to an OutputStream.
      static long copyLarge​(java.io.InputStream input, java.io.OutputStream output, long inputOffset, long length)
      Copies some or all bytes from a large (over 2GB) InputStream to an OutputStream, optionally skipping input bytes.
      static long copyLarge​(java.io.InputStream input, java.io.OutputStream output, long inputOffset, long length, byte[] buffer)
      Copies some or all bytes from a large (over 2GB) InputStream to an OutputStream, optionally skipping input bytes.
      static long copyLarge​(java.io.Reader input, java.io.Writer output)
      Copies chars from a large (over 2GB) Reader to a Writer.
      static long copyLarge​(java.io.Reader input, java.io.Writer output, char[] buffer)
      Copies chars from a large (over 2GB) Reader to a Writer.
      static long skip​(java.io.InputStream input, long toSkip)
      Skips bytes from an input byte stream.
      static void skipFully​(java.io.InputStream input, long toSkip)
      Skips the requested number of bytes or fail if there are not enough left.
      • Methods inherited from class java.lang.Object

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

      • IOUtils

        public IOUtils()
    • Method Detail

      • copy

        public static long copy​(java.io.InputStream input,
                                java.io.OutputStream output,
                                int bufferSize)
                         throws java.io.IOException
        Copies bytes from an InputStream to an OutputStream using an internal buffer of the given size.

        This method buffers the input internally, so there is no need to use a BufferedInputStream.

        Parameters:
        input - the InputStream to read from
        output - the OutputStream to write to
        bufferSize - the bufferSize used to copy from the input to the output
        Returns:
        the number of bytes copied
        Throws:
        java.lang.NullPointerException - if the input or output is null
        java.io.IOException - if an I/O error occurs
      • copyLarge

        public static long copyLarge​(java.io.InputStream input,
                                     java.io.OutputStream output,
                                     long inputOffset,
                                     long length)
                              throws java.io.IOException
        Copies some or all bytes from a large (over 2GB) InputStream to an OutputStream, optionally skipping input bytes.

        This method buffers the input internally, so there is no need to use a BufferedInputStream.

        Note that the implementation uses skip(InputStream, long). This means that the method may be considerably less efficient than using the actual skip implementation, this is done to guarantee that the correct number of characters are skipped.

        The buffer size is given by DEFAULT_BUFFER_SIZE.
        Parameters:
        input - the InputStream to read from
        output - the OutputStream to write to
        inputOffset - : number of bytes to skip from input before copying -ve values are ignored
        length - : number of bytes to copy. -ve means all
        Returns:
        the number of bytes copied
        Throws:
        java.lang.NullPointerException - if the input or output is null
        java.io.IOException - if an I/O error occurs
      • copyLarge

        public static long copyLarge​(java.io.InputStream input,
                                     java.io.OutputStream output)
                              throws java.io.IOException
        Copies bytes from a large (over 2GB) InputStream to an OutputStream.

        This method buffers the input internally, so there is no need to use a BufferedInputStream.

        The buffer size is given by DEFAULT_BUFFER_SIZE.

        Parameters:
        input - the InputStream to read from
        output - the OutputStream to write to
        Returns:
        the number of bytes copied
        Throws:
        java.lang.NullPointerException - if the input or output is null
        java.io.IOException - if an I/O error occurs
      • copyLarge

        public static long copyLarge​(java.io.InputStream input,
                                     java.io.OutputStream output,
                                     byte[] buffer)
                              throws java.io.IOException
        Copies bytes from a large (over 2GB) InputStream to an OutputStream.

        This method uses the provided buffer, so there is no need to use a BufferedInputStream.

        Parameters:
        input - the InputStream to read from
        output - the OutputStream to write to
        buffer - the buffer to use for the copy
        Returns:
        the number of bytes copied
        Throws:
        java.lang.NullPointerException - if the input or output is null
        java.io.IOException - if an I/O error occurs
      • copyLarge

        public static long copyLarge​(java.io.InputStream input,
                                     java.io.OutputStream output,
                                     long inputOffset,
                                     long length,
                                     byte[] buffer)
                              throws java.io.IOException
        Copies some or all bytes from a large (over 2GB) InputStream to an OutputStream, optionally skipping input bytes.

        This method uses the provided buffer, so there is no need to use a BufferedInputStream.

        Note that the implementation uses skip(InputStream, long). This means that the method may be considerably less efficient than using the actual skip implementation, this is done to guarantee that the correct number of characters are skipped.

        Parameters:
        input - the InputStream to read from
        output - the OutputStream to write to
        inputOffset - : number of bytes to skip from input before copying -ve values are ignored
        length - : number of bytes to copy. -ve means all
        buffer - the buffer to use for the copy
        Returns:
        the number of bytes copied
        Throws:
        java.lang.NullPointerException - if the input or output is null
        java.io.IOException - if an I/O error occurs
      • skipFully

        public static void skipFully​(java.io.InputStream input,
                                     long toSkip)
                              throws java.io.IOException
        Skips the requested number of bytes or fail if there are not enough left.

        This allows for the possibility that InputStream.skip(long) may not skip as many bytes as requested (most likely because of reaching EOF).

        Note that the implementation uses skip(InputStream, long). This means that the method may be considerably less efficient than using the actual skip implementation, this is done to guarantee that the correct number of characters are skipped.

        Parameters:
        input - stream to skip
        toSkip - the number of bytes to skip
        Throws:
        java.io.IOException - if there is a problem reading the file
        java.lang.IllegalArgumentException - if toSkip is negative
        java.io.EOFException - if the number of bytes skipped was incorrect
        See Also:
        InputStream.skip(long)
      • skip

        public static long skip​(java.io.InputStream input,
                                long toSkip)
                         throws java.io.IOException
        Skips bytes from an input byte stream. This implementation guarantees that it will read as many bytes as possible before giving up; this may not always be the case for skip() implementations in subclasses of InputStream.

        Note that the implementation uses InputStream.read(byte[], int, int) rather than delegating to InputStream.skip(long). This means that the method may be considerably less efficient than using the actual skip implementation, this is done to guarantee that the correct number of bytes are skipped.

        Parameters:
        input - byte stream to skip
        toSkip - number of bytes to skip.
        Returns:
        number of bytes actually skipped.
        Throws:
        java.io.IOException - if there is a problem reading the file
        java.lang.IllegalArgumentException - if toSkip is negative
        See Also:
        InputStream.skip(long), IO-203 - Add skipFully() method for InputStreams
      • copy

        public static int copy​(java.io.Reader input,
                               java.io.Writer output)
                        throws java.io.IOException
        Copies chars from a Reader to a Writer.

        This method buffers the input internally, so there is no need to use a BufferedReader.

        Large streams (over 2GB) will return a chars copied value of -1 after the copy has completed since the correct number of chars cannot be returned as an int. For large streams use the copyLarge(Reader, Writer) method.

        Parameters:
        input - the Reader to read from
        output - the Writer to write to
        Returns:
        the number of characters copied, or -1 if > Integer.MAX_VALUE
        Throws:
        java.lang.NullPointerException - if the input or output is null
        java.io.IOException - if an I/O error occurs
        Since:
        1.1
      • copyLarge

        public static long copyLarge​(java.io.Reader input,
                                     java.io.Writer output)
                              throws java.io.IOException
        Copies chars from a large (over 2GB) Reader to a Writer.

        This method buffers the input internally, so there is no need to use a BufferedReader.

        The buffer size is given by DEFAULT_BUFFER_SIZE.

        Parameters:
        input - the Reader to read from
        output - the Writer to write to
        Returns:
        the number of characters copied
        Throws:
        java.lang.NullPointerException - if the input or output is null
        java.io.IOException - if an I/O error occurs
        Since:
        1.3
      • copyLarge

        public static long copyLarge​(java.io.Reader input,
                                     java.io.Writer output,
                                     char[] buffer)
                              throws java.io.IOException
        Copies chars from a large (over 2GB) Reader to a Writer.

        This method uses the provided buffer, so there is no need to use a BufferedReader.

        Parameters:
        input - the Reader to read from
        output - the Writer to write to
        buffer - the buffer to be used for the copy
        Returns:
        the number of characters copied
        Throws:
        java.lang.NullPointerException - if the input or output is null
        java.io.IOException - if an I/O error occurs
        Since:
        2.2