Class IOUtils
- java.lang.Object
-
- org.apache.poi.util.IOUtils
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static long
calculateChecksum(byte[] data)
Calculate checksum on input datastatic long
calculateChecksum(InputStream stream)
Calculate checksum on all the data read from input stream.static void
closeQuietly(Closeable closeable)
Quietly (no exceptions) close Closable resource.static long
copy(InputStream srcStream, File destFile)
Copy the contents of the stream to a new file.static long
copy(InputStream inp, OutputStream out)
Copies all the data from the given InputStream to the OutputStream.static long
copy(InputStream inp, OutputStream out, long limit)
Copies all the data from the given InputStream to the OutputStream.static int
getMaxByteArrayInitSize()
static byte[]
peekFirst8Bytes(InputStream stream)
Peeks at the first 8 bytes of the stream.static byte[]
peekFirstNBytes(InputStream stream, int limit)
Peeks at the first N bytes of the stream.static int
readByte(InputStream is)
Simple utility function to check that you haven't hit EOF when reading a byte.static int
readFully(InputStream in, byte[] b)
Helper method, just callsreadFully(in, b, 0, b.length)
static int
readFully(InputStream in, byte[] b, int off, int len)
Same as the normalInputStream.read(byte[], int, int)
, but tries to ensure that the entire len number of bytes is read.static int
readFully(ReadableByteChannel channel, ByteBuffer b)
Same as the normalchannel.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
safelyAllocateCheck(long length, int maxLength)
static byte[]
safelyClone(byte[] src, int offset, int 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 void
setMaxByteArrayInitSize(int maxOverride)
static long
skipFully(InputStream input, long toSkip)
Skips bytes from an input byte stream.static byte[]
toByteArray(InputStream stream)
Reads all the data from the input stream, and returns the bytes read.static byte[]
toByteArray(InputStream stream, int length)
Reads up tolength
bytes from the input stream, and returns the bytes read.static byte[]
toByteArray(InputStream stream, int length, int maxLength)
Reads up tolength
bytes from the input stream, and returns the bytes read.static byte[]
toByteArray(ByteBuffer buffer, int length)
Returns an array (that shouldn't be written to!) of the ByteBuffer.static byte[]
toByteArrayWithMaxLength(InputStream stream, int maxLength)
Reads the input stream, and returns the bytes read.
-
-
-
Method Detail
-
setMaxByteArrayInitSize
public static void setMaxByteArrayInitSize(int maxOverride)
- Parameters:
maxOverride
- the max init size of ByteArrayOutputStream. -1 (the default) means init size of ByteArrayOutputStream could be up toInteger.MAX_VALUE
- Since:
- POI 5.2.2
-
getMaxByteArrayInitSize
public static int getMaxByteArrayInitSize()
- Returns:
- the max init size of ByteArrayOutputStream.
-1 (the default) means init size of ByteArrayOutputStream could be up to
Integer.MAX_VALUE
- Since:
- POI 5.2.2
-
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 allocating memory up to the size specified. It also allows to impose a lower limit than used for very memory constrained systems. Note: This is a per-allocation limit and does not allow you to limit the overall sum of allocations! Use -1 for using the limits specified per record-type.- Parameters:
maxOverride
- The maximum number of bytes that should be possible to be allocated in one step.- Since:
- 4.0.0
-
peekFirst8Bytes
public static byte[] peekFirst8Bytes(InputStream stream) throws 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 emptyIOException
-
peekFirstNBytes
public static byte[] peekFirstNBytes(InputStream stream, int limit) throws 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 emptyIOException
-
toByteArray
public static byte[] toByteArray(InputStream stream) throws 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:
IOException
- If reading data fails or EOF is encountered too early for the given length.RecordFormatException
- If the requested length is invalid.
-
toByteArray
public static byte[] toByteArray(InputStream stream, int length) throws IOException
Reads up tolength
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, useInteger.MAX_VALUE
to read the stream until EOF.- Returns:
- A byte array with the read bytes.
- Throws:
IOException
- If reading data fails or EOF is encountered too early for the given length.RecordFormatException
- If the requested length is invalid.
-
toByteArray
public static byte[] toByteArray(InputStream stream, int length, int maxLength) throws IOException
Reads up tolength
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, useInteger.MAX_VALUE
to read the stream until EOFmaxLength
- if the input is equal to/longer thanmaxLength
bytes, then throw anIOException
complaining about the length. useInteger.MAX_VALUE
to disable the check - ifsetByteArrayMaxOverride(int)
is set then that max of that value and this maxLength is used- Returns:
- A byte array with the read bytes.
- Throws:
IOException
- If reading data fails or EOF is encountered too early for the given length.RecordFormatException
- If the requested length is invalid.
-
toByteArrayWithMaxLength
public static byte[] toByteArrayWithMaxLength(InputStream stream, int maxLength) throws IOException
Reads the input stream, and returns the bytes read.- Parameters:
stream
- The byte stream of data to read.maxLength
- if the input is equal to/longer thanmaxLength
bytes, then throw anIOException
complaining about the length. useInteger.MAX_VALUE
to disable the check - ifsetByteArrayMaxOverride(int)
is set then that max of that value and this maxLength is used- Returns:
- A byte array with the read bytes.
- Throws:
IOException
- If reading data fails or EOF is encountered too early for the given length.RecordFormatException
- If the requested length is invalid.- Since:
- POI 5.2.1
-
toByteArray
public static byte[] toByteArray(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(InputStream in, byte[] b) throws IOException
Helper method, just callsreadFully(in, b, 0, b.length)
- Parameters:
in
- the stream from which the data is read.b
- the buffer into which the data is read.- Returns:
- the number of bytes read or -1 if no bytes were read
- Throws:
IOException
- if reading from the stream fails
-
readFully
public static int readFully(InputStream in, byte[] b, int off, int len) throws 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 beforelen
bytes have been read, will returnlen
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 arrayb
at which the data is written.len
- the maximum number of bytes to read.- Returns:
- the number of bytes read or -1 if no bytes were read
- Throws:
IOException
- if reading from the stream fails
-
readFully
public static int readFully(ReadableByteChannel channel, ByteBuffer b) throws IOException
Same as the normalchannel.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.
- Parameters:
channel
- The byte-channel to read data fromb
- the buffer into which the data is read.- Returns:
- the number of bytes read or -1 if no bytes were read
- Throws:
IOException
- if reading from the stream fails
-
copy
public static long copy(InputStream inp, OutputStream out) throws 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
- TheInputStream
which provides the dataout
- TheOutputStream
to write the data to- Returns:
- the amount of bytes copied
- Throws:
IOException
- If copying the data fails.
-
copy
public static long copy(InputStream inp, OutputStream out, long limit) throws 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
- TheInputStream
which provides the dataout
- TheOutputStream
to write the data tolimit
- limit the copied bytes - use-1
for no limit- Returns:
- the amount of bytes copied
- Throws:
IOException
- If copying the data fails.
-
copy
public static long copy(InputStream srcStream, File destFile) throws IOException
Copy the contents of the stream to a new file.- Parameters:
srcStream
- TheInputStream
which provides the datadestFile
- The file where the data should be stored- Returns:
- the amount of bytes copied
- Throws:
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(InputStream stream) throws IOException
Calculate checksum on all the data read from input stream. This should be more efficient than the equivalent codeIOUtils.calculateChecksum(IOUtils.toByteArray(stream))
- Throws:
IOException
-
closeQuietly
public static void closeQuietly(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(InputStream input, long toSkip) throws 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 ofInputStream
.Note that the implementation uses
InputStream.read(byte[], int, int)
rather than delegating toInputStream.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 beforelen
bytes have been read, will returnlen
bytes.Copied nearly verbatim from commons-io 41a3e9c
- Parameters:
input
- byte stream to skiptoSkip
- number of bytes to skip.- Returns:
- number of bytes actually skipped.
- Throws:
IOException
- if there is a problem reading the fileIllegalArgumentException
- if toSkip is negative- See Also:
InputStream.skip(long)
-
safelyAllocate
public static byte[] safelyAllocate(long length, int maxLength)
-
safelyAllocateCheck
public static void safelyAllocateCheck(long length, int maxLength)
-
safelyClone
public static byte[] safelyClone(byte[] src, int offset, int length, int maxLength)
-
readByte
public static int readByte(InputStream is) throws IOException
Simple utility function to check that you haven't hit EOF when reading a byte.- Parameters:
is
- input stream to read- Returns:
- byte read, unless
- Throws:
IOException
- on IOException or EOF if -1 is read
-
-