浏览代码

add extra max size config

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1898249 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_5_2_1
PJ Fanning 2 年前
父节点
当前提交
82f29944d3
共有 1 个文件被更改,包括 13 次插入31 次删除
  1. 13
    31
      poi/src/main/java/org/apache/poi/util/IOUtils.java

+ 13
- 31
poi/src/main/java/org/apache/poi/util/IOUtils.java 查看文件

* @param stream The byte stream of data to read. * @param stream The byte stream of data to read.
* @return A byte array with the read bytes. * @return A byte array with the read bytes.
* @throws IOException If reading data fails or EOF is encountered too early for the given length. * @throws IOException If reading data fails or EOF is encountered too early for the given length.
* @throws RecordFormatException If the requested length is invalid.
*/ */
public static byte[] toByteArray(InputStream stream) throws IOException { public static byte[] toByteArray(InputStream stream) throws IOException {
return toByteArray(stream, Integer.MAX_VALUE); return toByteArray(stream, Integer.MAX_VALUE);
* until EOF. * until EOF.
* @return A byte array with the read bytes. * @return A byte array with the read bytes.
* @throws IOException If reading data fails or EOF is encountered too early for the given length. * @throws IOException If reading data fails or EOF is encountered too early for the given length.
* @throws RecordFormatException If the requested length is invalid.
*/ */
public static byte[] toByteArray(InputStream stream, final int length) throws IOException { public static byte[] toByteArray(InputStream stream, final int length) throws IOException {
return toByteArray(stream, length, Integer.MAX_VALUE); return toByteArray(stream, length, Integer.MAX_VALUE);
* set then that max of that value and this maxLength is used * set then that max of that value and this maxLength is used
* @return A byte array with the read bytes. * @return A byte array with the read bytes.
* @throws IOException If reading data fails or EOF is encountered too early for the given length. * @throws IOException If reading data fails or EOF is encountered too early for the given length.
* @throws RecordFormatException If the requested length is invalid.
*/ */
public static byte[] toByteArray(InputStream stream, final int length, final int maxLength) throws IOException { public static byte[] toByteArray(InputStream stream, final int length, final int maxLength) throws IOException {
if (length < 0L || maxLength < 0L) {
if ((length < 0 && length != Integer.MIN_VALUE) || maxLength < 0) {
throw new RecordFormatException("Can't allocate an array of length < 0"); throw new RecordFormatException("Can't allocate an array of length < 0");
} }
final int derivedMaxLength = BYTE_ARRAY_MAX_OVERRIDE <= 0 ? maxLength : Math.max(maxLength, BYTE_ARRAY_MAX_OVERRIDE); final int derivedMaxLength = BYTE_ARRAY_MAX_OVERRIDE <= 0 ? maxLength : Math.max(maxLength, BYTE_ARRAY_MAX_OVERRIDE);
checkLength(length, derivedMaxLength); checkLength(length, derivedMaxLength);
} }


final int len = Math.min(length, derivedMaxLength);
try (UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream(len == Integer.MAX_VALUE ? 4096 : len)) {
final int derivedLen = length == Integer.MIN_VALUE ? derivedMaxLength : Math.min(length, derivedMaxLength);
try (UnsynchronizedByteArrayOutputStream baos =
new UnsynchronizedByteArrayOutputStream(derivedLen == Integer.MAX_VALUE ? 4096 : derivedLen)) {
byte[] buffer = new byte[4096]; byte[] buffer = new byte[4096];
int totalBytes = 0, readBytes; int totalBytes = 0, readBytes;
do { do {
readBytes = stream.read(buffer, 0, Math.min(buffer.length, len - totalBytes));
readBytes = stream.read(buffer, 0, Math.min(buffer.length, derivedLen - totalBytes));
totalBytes += Math.max(readBytes, 0); totalBytes += Math.max(readBytes, 0);
if (readBytes > 0) { if (readBytes > 0) {
baos.write(buffer, 0, readBytes); baos.write(buffer, 0, readBytes);
} }


checkByteSizeLimit(totalBytes); checkByteSizeLimit(totalBytes);
} while (totalBytes < len && readBytes > -1);
} while (totalBytes < derivedLen && readBytes > -1);


if (derivedMaxLength != Integer.MAX_VALUE && totalBytes == derivedMaxLength) { if (derivedMaxLength != Integer.MAX_VALUE && totalBytes == derivedMaxLength) {
throw new IOException("MaxLength (" + derivedMaxLength + ") reached - stream seems to be invalid."); throw new IOException("MaxLength (" + derivedMaxLength + ") reached - stream seems to be invalid.");
} }


if (len != Integer.MAX_VALUE && totalBytes < len) {
throw new EOFException("unexpected EOF - expected len: " + len + " - actual len: " + totalBytes);
if (length != Integer.MIN_VALUE && derivedLen != Integer.MAX_VALUE && totalBytes < derivedLen) {
throw new EOFException("unexpected EOF - expected len: " + derivedLen + " - actual len: " + totalBytes);
} }


return baos.toByteArray(); return baos.toByteArray();
* set then that max of that value and this maxLength is used * set then that max of that value and this maxLength is used
* @return A byte array with the read bytes. * @return A byte array with the read bytes.
* @throws IOException If reading data fails or EOF is encountered too early for the given length. * @throws IOException If reading data fails or EOF is encountered too early for the given length.
* @throws RecordFormatException If the requested length is invalid.
* @since POI 5.2.1 * @since POI 5.2.1
*/ */
public static byte[] toByteArrayWithMaxLength(InputStream stream, final int maxLength) throws IOException { public static byte[] toByteArrayWithMaxLength(InputStream stream, final int maxLength) throws IOException {
if (maxLength < 0L) {
throw new RecordFormatException("Can't allocate an array of length < 0");
}
final int derivedMaxLength = BYTE_ARRAY_MAX_OVERRIDE <= 0 ? maxLength : Math.max(maxLength, BYTE_ARRAY_MAX_OVERRIDE);

try (UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream(derivedMaxLength == Integer.MAX_VALUE ? 4096 : derivedMaxLength)) {
byte[] buffer = new byte[4096];
int totalBytes = 0, readBytes;
do {
readBytes = stream.read(buffer, 0, Math.min(buffer.length, derivedMaxLength - totalBytes));
totalBytes += Math.max(readBytes, 0);
if (readBytes > 0) {
baos.write(buffer, 0, readBytes);
}

checkByteSizeLimit(totalBytes);
} while (totalBytes < derivedMaxLength && readBytes > -1);

if (derivedMaxLength != Integer.MAX_VALUE && totalBytes == derivedMaxLength) {
throw new IOException("MaxLength (" + derivedMaxLength + ") reached - stream seems to be invalid.");
}

return baos.toByteArray();
}
return toByteArray(stream, Integer.MIN_VALUE, maxLength);
} }


private static void checkLength(long length, int maxLength) { private static void checkLength(long length, int maxLength) {

正在加载...
取消
保存