org.apache.poi.util
Class IOUtils

java.lang.Object
  extended by org.apache.poi.util.IOUtils

public final class IOUtils
extends java.lang.Object


Method Summary
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 void 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 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 in.read(b, off, len), 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 entire len number of bytes is read.
static byte[] toByteArray(java.nio.ByteBuffer buffer, int length)
          Returns an array (that shouldn't be written to!) of the ByteBuffer.
static byte[] toByteArray(java.io.InputStream stream)
          Reads all the data from the input stream, and returns the bytes read.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

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

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.

Throws:
java.io.IOException

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 in.read(b, off, len), 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.

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 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.

Throws:
java.io.IOException

copy

public static void 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.

Throws:
java.io.IOException

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


Copyright 2016 The Apache Software Foundation or its licensors, as applicable.