@Internal public final class IOUtils extends java.lang.Object
Modifier and Type | Method and 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.
(将给定 InputStream 中的所有数据复制到 OutputStream。)
|
static long |
copy(java.io.InputStream inp, java.io.OutputStream out, long limit)
Copies all the data from the given InputStream to the OutputStream.
(将给定 InputStream 中的所有数据复制到 OutputStream。)
|
static byte[] |
peekFirst8Bytes(java.io.InputStream stream)
Peeks at the first 8 bytes of the stream.
(查看流的前 8 个字节。)
|
static byte[] |
peekFirstNBytes(java.io.InputStream stream, int limit)
Peeks at the first N bytes of the stream.
(查看流的前 N 个字节。)
|
static int |
readByte(java.io.InputStream is)
Simple utility function to check that you haven't hit EOF when reading a byte.
(简单的实用功能,用于检查您在读取字节时是否未命中 EOF。)
|
static int |
readFully(java.io.InputStream in, byte[] b)
Helper method, just calls
readFully(in, b, 0, b.length)
(辅助方法,只调用 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.
(与普通的 InputStream.read(byte[], int, int) 相同,但会尝试确保读取整个 len 个字节数。)
|
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.
(与普通的 channel.read(b) 相同,但尽可能确保缓冲区被完全填满,即)
|
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.
(如果此值设置为 > 0,safelyAllocate(long, int) 将忽略最大记录长度参数。)
|
static long |
skipFully(java.io.InputStream input, long toSkip)
Skips bytes from an input byte stream.
(从输入字节流中跳过字节。)
|
static byte[] |
toByteArray(java.nio.ByteBuffer buffer, int length)
Returns an array (that shouldn't be written to!) of the ByteBuffer.
(返回 ByteBuffer 的数组(不应写入!)。)
|
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.
(从输入流中读取最多 length 个字节,并返回读取的字节。)
|
static byte[] |
toByteArray(java.io.InputStream stream, int length, int maxLength)
Reads up to
length bytes from the input stream, and returns the bytes read.
(从输入流中读取最多 length 个字节,并返回读取的字节。)
|
public static void setByteArrayMaxOverride(int maxOverride)
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 an per-allocation limit and does not allow to limit overall sum of allocations! Use -1 for using the limits specified per record-type.
(如果此值设置为 > 0,safelyAllocate(long, int) 将忽略最大记录长度参数。这旨在允许用户在他们愿意接受将内存分配到指定大小的风险时绕过硬编码的最大记录长度。它还允许施加比用于非常内存受限的系统的下限。注意:这是每个分配的限制,不允许限制分配的总和!使用 -1 来使用每个记录类型指定的限制。)
maxOverride
- The maximum number of bytes that should be possible to be allocated in one step.
(maxOverride - 一步可以分配的最大字节数。)
public static byte[] peekFirst8Bytes(java.io.InputStream stream) throws java.io.IOException, EmptyFileException
EmptyFileException
- if the stream is empty
(EmptyFileException - 如果流为空)
java.io.IOException
(java.io.IOException)
public static byte[] peekFirstNBytes(java.io.InputStream stream, int limit) throws java.io.IOException, EmptyFileException
EmptyFileException
- if the stream is empty
(EmptyFileException - 如果流为空)
java.io.IOException
(java.io.IOException)
public static byte[] toByteArray(java.io.InputStream stream) throws java.io.IOException
stream
- The byte stream of data to read.
(stream - 要读取的数据字节流。)
java.io.IOException
- If reading data fails or EOF is encountered too early for the given length.
(java.io.IOException - 如果读取数据失败或遇到给定长度的 EOF 太早。)
public static byte[] toByteArray(java.io.InputStream stream, int length) throws java.io.IOException
length
bytes from the input stream, and returns the bytes read.
(从输入流中读取最多 length 个字节,并返回读取的字节。)
stream
- The byte stream of data to read.
(stream - 要读取的数据字节流。)
length
- The maximum length to read, use Integer.MAX_VALUE to read the stream until EOF.
(length - 要读取的最大长度,使用 Integer.MAX_VALUE 读取流直到 EOF。)
java.io.IOException
- If reading data fails or EOF is encountered too early for the given length.
(java.io.IOException - 如果读取数据失败或遇到给定长度的 EOF 太早。)
public static byte[] toByteArray(java.io.InputStream stream, int length, int maxLength) throws java.io.IOException
length
bytes from the input stream, and returns the bytes read.
(从输入流中读取最多 length 个字节,并返回读取的字节。)
stream
- The byte stream of data to read.
(stream - 要读取的数据字节流。)
length
- The maximum length to read, use
Integer.MAX_VALUE
to read the stream until EOF
(length - 要读取的最大长度,使用 Integer.MAX_VALUE 读取流直到 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
(maxLength - 如果输入等于/长于 maxLength 字节,则抛出 IOException 抱怨长度。使用 Integer.MAX_VALUE 禁用检查)
java.io.IOException
- If reading data fails or EOF is encountered too early for the given length.
(java.io.IOException - 如果读取数据失败或遇到给定长度的 EOF 太早。)
public static byte[] toByteArray(java.nio.ByteBuffer buffer, int length)
public static int readFully(java.io.InputStream in, byte[] b) throws java.io.IOException
java.io.IOException
(java.io.IOException)
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.
(与普通的 InputStream.read(byte[], int, int) 相同,但会尝试确保读取整个 len 个字节数。如果在读取任何字节之前到达文件末尾,则返回 -1。如果在读取一些字节后到达文件末尾,则返回读取的字节数。如果在读取 len 个字节之前未到达文件末尾,则将返回 len 个字节。)in
- the stream from which the data is read.
(in - 从中读取数据的流。)
b
- the buffer into which the data is read.
(b - 读取数据的缓冲区。)
off
- the start offset in array
b at which the data is written.
(off - 数组 b 中写入数据的起始偏移量。)
len
- the maximum number of bytes to read.
(len - 要读取的最大字节数。)
java.io.IOException
(java.io.IOException)
public static int readFully(java.nio.channels.ReadableByteChannel channel, java.nio.ByteBuffer b) throws java.io.IOException
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.
(与普通的 channel.read(b) 相同,但尽可能确保缓冲区被完全填满,iebremaining() 返回 0。如果在读取任何字节之前到达文件末尾,则返回 -1。如果在读取一些字节后到达文件末尾,则返回读取的字节数。如果在缓冲区没有剩余容量之前未到达文件末尾,则将返回已读取的字节数。)java.io.IOException
(java.io.IOException)
public static long copy(java.io.InputStream inp, java.io.OutputStream out) throws java.io.IOException
inp
- The
InputStream
which provides the data
(inp - 提供数据的 InputStream)
out
- The
OutputStream
to write the data to
(out - 将数据写入的 OutputStream)
java.io.IOException
- If copying the data fails.
(java.io.IOException - 如果复制数据失败。)
public static long copy(java.io.InputStream inp, java.io.OutputStream out, long limit) throws java.io.IOException
inp
- The
InputStream
which provides the data
(inp - 提供数据的 InputStream)
out
- The
OutputStream
to write the data to
(out - 将数据写入的 OutputStream)
limit
- limit the copied bytes - use
-1
for no limit
(limit - 限制复制的字节数 - 使用 -1 表示无限制)
java.io.IOException
- If copying the data fails.
(java.io.IOException - 如果复制数据失败。)
public static long copy(java.io.InputStream srcStream, java.io.File destFile) throws java.io.IOException
srcStream
- The
InputStream
which provides the data
(srcStream - 提供数据的 InputStream)
destFile
- The file where the data should be stored
(destFile - 应该存储数据的文件)
java.io.IOException
- If the target directory does not exist and cannot be created or if copying the data fails.
(java.io.IOException - 如果目标目录不存在且无法创建,或者复制数据失败。)
public static long calculateChecksum(byte[] data)
public static long calculateChecksum(java.io.InputStream stream) throws java.io.IOException
IOUtils.calculateChecksum(IOUtils.toByteArray(stream))
(计算从输入流中读取的所有数据的校验和。这应该比等效代码 IOUtils.calculateChecksum(IOUtils.toByteArray(stream)) 更有效)
java.io.IOException
(java.io.IOException)
public static void closeQuietly(java.io.Closeable closeable)
IOUtils
class logger.
(安静地(没有例外)关闭可关闭资源。如果出现错误,它将被打印到 IOUtils 类记录器。)
closeable
- resource to close
(closeable - 要关闭的资源)
public static long skipFully(java.io.InputStream input, long toSkip) throws java.io.IOException
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
(从输入字节流中跳过字节。这个实现保证在放弃之前它会读取尽可能多的字节; InputStream 的子类中的 skip() 实现可能并非总是如此。请注意,该实现使用 InputStream.read(byte[], int, int) 而不是委托给 InputStream.skip(long)。这意味着该方法可能比使用实际的跳过实现效率低得多,这样做是为了保证跳过正确数量的字节。这模仿了 POI 的 readFully(InputStream, byte[])。如果在读取任何字节之前到达文件末尾,则返回 -1。如果在读取一些字节后到达文件末尾,则返回读取的字节数。如果在读取 len 个字节之前未到达文件末尾,则将返回 len 个字节。从 commons-io 41a3e9c 几乎逐字复制)input
- byte stream to skip
(输入 - 要跳过的字节流)
toSkip
- number of bytes to skip.
(toSkip - 要跳过的字节数。)
java.io.IOException
- if there is a problem reading the file
(java.io.IOException - 如果读取文件有问题)
java.lang.IllegalArgumentException
- if toSkip is negative
(java.lang.IllegalArgumentException - 如果 toSkip 为负)
InputStream.skip(long)
public static byte[] safelyAllocate(long length, int maxLength)
public static void safelyAllocateCheck(long length, int maxLength)
public static byte[] safelyClone(byte[] src, int offset, int length, int maxLength)
public static int readByte(java.io.InputStream is) throws java.io.IOException
is
- inputstream to read
(is - 要读取的输入流)
java.io.IOException
- on IOException or EOF if -1 is read
(java.io.IOException - 如果读取 -1,则为 IOException 或 EOF)
Copyright 2021 The Apache Software Foundation or its licensors, as applicable.