Class IOUtils


  • public final class IOUtils
    extends java.lang.Object
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static long calculateChecksum​(byte[] data)
      Calculate checksum on input data
      static long calculateChecksum​(java.io.InputStream stream)
      Calculate checksum on all the data read from input stream.
      static void closeQuietly​(java.io.Closeable closeable)
      Quietly (no exceptions) close Closable resource.
      static long copy​(java.io.InputStream srcStream, java.io.File destFile)
      Copy the contents of the stream to a new file.
      static long copy​(java.io.InputStream inp, java.io.OutputStream out)
      Copies all the data from the given InputStream to the OutputStream.
      static byte[] peekFirst8Bytes​(java.io.InputStream stream)
      Peeks at the first 8 bytes of the stream.
      static byte[] peekFirstNBytes​(java.io.InputStream stream, int limit)
      Peeks at the first N bytes of the stream.
      static int readByte​(java.io.InputStream is)
      Simple utility function to check that you haven't hit EOF when reading a byte.
      static int readFully​(java.io.InputStream in, byte[] b)
      Helper method, just calls readFully(in, b, 0, b.length)
      static int readFully​(java.io.InputStream in, byte[] b, int off, int len)
      Same as the normal InputStream.read(byte[], int, int), but tries to ensure that the entire len number of bytes is read.
      static int readFully​(java.nio.channels.ReadableByteChannel channel, java.nio.ByteBuffer b)
      Same as the normal channel.read(b), but tries to ensure that the buffer is filled completely if possible, i.e.
      static byte[] safelyAllocate​(long length, int maxLength)  
      static void setByteArrayMaxOverride​(int maxOverride)
      If this value is set to > 0, safelyAllocate(long, int) will ignore the maximum record length parameter.
      static long skipFully​(java.io.InputStream input, long toSkip)
      Skips bytes from an input byte stream.
      static byte[] toByteArray​(java.io.InputStream stream)
      Reads all the data from the input stream, and returns the bytes read.
      static byte[] toByteArray​(java.io.InputStream stream, int length)
      Reads up to length bytes from the input stream, and returns the bytes read.
      static byte[] toByteArray​(java.io.InputStream stream, long length, int maxLength)
      Reads up to length bytes from the input stream, and returns the bytes read.
      static byte[] toByteArray​(java.nio.ByteBuffer buffer, int length)
      Returns an array (that shouldn't be written to!) of the ByteBuffer.
      static void write​(POIDocument doc, java.io.OutputStream out)
      Deprecated.
      since 4.0, use try-with-resources, will be removed in 4.2
      static void write​(Workbook doc, java.io.OutputStream out)
      Deprecated.
      since 4.0, use try-with-resources, will be removed in 4.2
      static void writeAndClose​(POIDocument doc)
      Deprecated.
      since 4.0, use try-with-resources, will be removed in 4.2
      static void writeAndClose​(POIDocument doc, java.io.File out)
      Deprecated.
      since 4.0, use try-with-resources, will be removed in 4.2
      static void writeAndClose​(POIDocument doc, java.io.OutputStream out)
      Deprecated.
      since 4.0, use try-with-resources, will be removed in 4.2
      static void writeAndClose​(Workbook doc, java.io.OutputStream out)
      Deprecated.
      since 4.0, use try-with-resources, will be removed in 4.2
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • setByteArrayMaxOverride

        public static void setByteArrayMaxOverride​(int maxOverride)
        If this value is set to > 0, safelyAllocate(long, int) will ignore the maximum record length parameter. This is designed to allow users to bypass the hard-coded maximum record lengths if they are willing to accept the risk of an OutOfMemoryException.
        Parameters:
        maxOverride - The number of bytes that should be possible to be allocated in one step.
        Since:
        4.0.0
      • peekFirst8Bytes

        public static byte[] peekFirst8Bytes​(java.io.InputStream stream)
                                      throws java.io.IOException,
                                             EmptyFileException
        Peeks at the first 8 bytes of the stream. Returns those bytes, but with the stream unaffected. Requires a stream that supports mark/reset, or a PushbackInputStream. If the stream has >0 but <8 bytes, remaining bytes will be zero.
        Throws:
        EmptyFileException - if the stream is empty
        java.io.IOException
      • peekFirstNBytes

        public static byte[] peekFirstNBytes​(java.io.InputStream stream,
                                             int limit)
                                      throws java.io.IOException,
                                             EmptyFileException
        Peeks at the first N bytes of the stream. Returns those bytes, but with the stream unaffected. Requires a stream that supports mark/reset, or a PushbackInputStream. If the stream has >0 but <N bytes, remaining bytes will be zero.
        Throws:
        EmptyFileException - if the stream is empty
        java.io.IOException
      • toByteArray

        public static byte[] toByteArray​(java.io.InputStream stream)
                                  throws java.io.IOException
        Reads all the data from the input stream, and returns the bytes read.
        Parameters:
        stream - The byte stream of data to read.
        Returns:
        A byte array with the read bytes.
        Throws:
        java.io.IOException - If reading data fails or EOF is encountered too early for the given length.
      • toByteArray

        public static byte[] toByteArray​(java.io.InputStream stream,
                                         int length)
                                  throws java.io.IOException
        Reads up to length bytes from the input stream, and returns the bytes read.
        Parameters:
        stream - The byte stream of data to read.
        length - The maximum length to read, use Integer.MAX_VALUE to read the stream until EOF.
        Returns:
        A byte array with the read bytes.
        Throws:
        java.io.IOException - If reading data fails or EOF is encountered too early for the given length.
      • toByteArray

        public static byte[] toByteArray​(java.io.InputStream stream,
                                         long length,
                                         int maxLength)
                                  throws java.io.IOException
        Reads up to length bytes from the input stream, and returns the bytes read.
        Parameters:
        stream - The byte stream of data to read.
        length - The maximum length to read, use Integer.MAX_VALUE to read the stream until EOF
        maxLength - if the input is equal to/longer than maxLength bytes, then throw an IOException complaining about the length. use Integer.MAX_VALUE to disable the check
        Returns:
        A byte array with the read bytes.
        Throws:
        java.io.IOException - If reading data fails or EOF is encountered too early for the given length.
      • toByteArray

        public static byte[] toByteArray​(java.nio.ByteBuffer buffer,
                                         int length)
        Returns an array (that shouldn't be written to!) of the ByteBuffer. Will be of the requested length, or possibly longer if that's easier.
      • readFully

        public static int readFully​(java.io.InputStream in,
                                    byte[] b)
                             throws java.io.IOException
        Helper method, just calls readFully(in, b, 0, b.length)
        Throws:
        java.io.IOException
      • readFully

        public static int readFully​(java.io.InputStream in,
                                    byte[] b,
                                    int off,
                                    int len)
                             throws java.io.IOException

        Same as the normal InputStream.read(byte[], int, int), but tries to ensure that the entire len number of bytes is read.

        If the end of file is reached before any bytes are read, returns -1. If the end of the file is reached after some bytes are read, returns the number of bytes read. If the end of the file isn't reached before len bytes have been read, will return len bytes.

        Parameters:
        in - the stream from which the data is read.
        b - the buffer into which the data is read.
        off - the start offset in array b at which the data is written.
        len - the maximum number of bytes to read.
        Throws:
        java.io.IOException
      • readFully

        public static int readFully​(java.nio.channels.ReadableByteChannel channel,
                                    java.nio.ByteBuffer b)
                             throws java.io.IOException
        Same as the normal channel.read(b), but tries to ensure that the buffer is filled completely if possible, i.e. b.remaining() returns 0.

        If the end of file is reached before any bytes are read, returns -1. If the end of the file is reached after some bytes are read, returns the number of bytes read. If the end of the file isn't reached before the buffer has no more remaining capacity, will return the number of bytes that were read.

        Throws:
        java.io.IOException
      • write

        @Deprecated
        @Removal(version="4.2")
        public static void write​(POIDocument doc,
                                 java.io.OutputStream out)
                          throws java.io.IOException
        Deprecated.
        since 4.0, use try-with-resources, will be removed in 4.2
        Write a POI Document (Workbook, SlideShow, etc) to an output stream and close the output stream. This will attempt to close the output stream at the end even if there was a problem writing the document to the stream. If you are using Java 7 or higher, you may prefer to use a try-with-resources statement instead. This function exists for Java 6 code.
        Parameters:
        doc - a writeable document to write to the output stream
        out - the output stream that the document is written to
        Throws:
        java.io.IOException - thrown on errors writing to the stream
      • write

        @Deprecated
        @Removal(version="4.2")
        public static void write​(Workbook doc,
                                 java.io.OutputStream out)
                          throws java.io.IOException
        Deprecated.
        since 4.0, use try-with-resources, will be removed in 4.2
        Write a (Workbook) to an output stream and close the output stream. This will attempt to close the output stream at the end even if there was a problem writing the document to the stream. If you are using Java 7 or higher, you may prefer to use a try-with-resources statement instead. This function exists for Java 6 code.
        Parameters:
        doc - a writeable document to write to the output stream
        out - the output stream that the document is written to
        Throws:
        java.io.IOException - thrown on errors writing to the stream
      • writeAndClose

        @Deprecated
        @Removal(version="4.2")
        public static void writeAndClose​(POIDocument doc,
                                         java.io.OutputStream out)
                                  throws java.io.IOException
        Deprecated.
        since 4.0, use try-with-resources, will be removed in 4.2
        Write a POI Document (Workbook, SlideShow, etc) to an output stream and close the output stream. This will attempt to close the output stream at the end even if there was a problem writing the document to the stream. This will also attempt to close the document, even if an error occurred while writing the document or closing the output stream. If you are using Java 7 or higher, you may prefer to use a try-with-resources statement instead. This function exists for Java 6 code.
        Parameters:
        doc - a writeable and closeable document to write to the output stream, then close
        out - the output stream that the document is written to
        Throws:
        java.io.IOException - thrown on errors writing to the stream
      • writeAndClose

        @Deprecated
        @Removal(version="4.2")
        public static void writeAndClose​(POIDocument doc,
                                         java.io.File out)
                                  throws java.io.IOException
        Deprecated.
        since 4.0, use try-with-resources, will be removed in 4.2
        Like writeAndClose(POIDocument, OutputStream), but for writing to a File instead of an OutputStream. This will attempt to close the document, even if an error occurred while writing the document. If you are using Java 7 or higher, you may prefer to use a try-with-resources statement instead. This function exists for Java 6 code.
        Parameters:
        doc - a writeable and closeable document to write to the output file, then close
        out - the output file that the document is written to
        Throws:
        java.io.IOException - thrown on errors writing to the stream
      • writeAndClose

        @Deprecated
        @Removal(version="4.2")
        public static void writeAndClose​(POIDocument doc)
                                  throws java.io.IOException
        Deprecated.
        since 4.0, use try-with-resources, will be removed in 4.2
        Like writeAndClose(POIDocument, File), but for writing a POI Document in place (to the same file that it was opened from). This will attempt to close the document, even if an error occurred while writing the document. If you are using Java 7 or higher, you may prefer to use a try-with-resources statement instead. This function exists for Java 6 code.
        Parameters:
        doc - a writeable document to write in-place
        Throws:
        java.io.IOException - thrown on errors writing to the file
      • writeAndClose

        @Deprecated
        @Removal(version="4.2")
        public static void writeAndClose​(Workbook doc,
                                         java.io.OutputStream out)
                                  throws java.io.IOException
        Deprecated.
        since 4.0, use try-with-resources, will be removed in 4.2
        Throws:
        java.io.IOException
      • copy

        public static long copy​(java.io.InputStream inp,
                                java.io.OutputStream out)
                         throws java.io.IOException
        Copies all the data from the given InputStream to the OutputStream. It leaves both streams open, so you will still need to close them once done.
        Parameters:
        inp - The InputStream which provides the data
        out - The OutputStream to write the data to
        Returns:
        the amount of bytes copied
        Throws:
        java.io.IOException - If copying the data fails.
      • copy

        public static long copy​(java.io.InputStream srcStream,
                                java.io.File destFile)
                         throws java.io.IOException
        Copy the contents of the stream to a new file.
        Parameters:
        srcStream - The InputStream which provides the data
        destFile - The file where the data should be stored
        Returns:
        the amount of bytes copied
        Throws:
        java.io.IOException - If the target directory does not exist and cannot be created or if copying the data fails.
      • calculateChecksum

        public static long calculateChecksum​(byte[] data)
        Calculate checksum on input data
      • calculateChecksum

        public static long calculateChecksum​(java.io.InputStream stream)
                                      throws java.io.IOException
        Calculate checksum on all the data read from input stream. This should be more efficient than the equivalent code IOUtils.calculateChecksum(IOUtils.toByteArray(stream))
        Throws:
        java.io.IOException
      • closeQuietly

        public static void closeQuietly​(java.io.Closeable closeable)
        Quietly (no exceptions) close Closable resource. In case of error it will be printed to IOUtils class logger.
        Parameters:
        closeable - resource to close
      • skipFully

        public static long skipFully​(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.

        This mimics POI's readFully(InputStream, byte[]). If the end of file is reached before any bytes are read, returns -1. If the end of the file is reached after some bytes are read, returns the number of bytes read. If the end of the file isn't reached before len bytes have been read, will return len bytes.

        Copied nearly verbatim from commons-io 41a3e9c

        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)
      • safelyAllocate

        public static byte[] safelyAllocate​(long length,
                                            int maxLength)
      • readByte

        public static int readByte​(java.io.InputStream is)
                            throws java.io.IOException
        Simple utility function to check that you haven't hit EOF when reading a byte.
        Parameters:
        is - inputstream to read
        Returns:
        byte read, unless
        Throws:
        java.io.IOException - on IOException or EOF if -1 is read