fix issue in IOUtils.toByteArrayWithMaxLength

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1898870 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2022-03-12 09:24:21 +00:00
parent 417f5e9a95
commit cbdf6d2330
2 changed files with 39 additions and 15 deletions

View File

@ -60,13 +60,19 @@ public final class IOUtils {
*/ */
private static int MAX_BYTE_ARRAY_INIT_SIZE = -1; private static int MAX_BYTE_ARRAY_INIT_SIZE = -1;
/**
* The default size of the bytearray used while reading input streams. This is meant to be pretty small.
*/
private static int DEFAULT_BUFFER_SIZE = 4096;
private IOUtils() { private IOUtils() {
// no instances of this class // no instances of this class
} }
/** /**
* @param maxOverride the max init size of ByteArrayOutputStream. * @param maxOverride the max init size of ByteArrayOutputStream.
* -1 (the default) means init size of ByteArrayOutputStream could be up to Integer.MAX_VALUE * -1 (the default) means init size of ByteArrayOutputStream could be up to {@link Integer#MAX_VALUE}
* @since POI 5.2.2
*/ */
public static void setMaxByteArrayInitSize(final int maxOverride) { public static void setMaxByteArrayInitSize(final int maxOverride) {
MAX_BYTE_ARRAY_INIT_SIZE = maxOverride; MAX_BYTE_ARRAY_INIT_SIZE = maxOverride;
@ -74,7 +80,8 @@ public final class IOUtils {
/** /**
* @return the max init size of ByteArrayOutputStream. * @return the max init size of ByteArrayOutputStream.
* -1 (the default) means init size of ByteArrayOutputStream could be up to Integer.MAX_VALUE * -1 (the default) means init size of ByteArrayOutputStream could be up to {@link Integer#MAX_VALUE}
* @since POI 5.2.2
*/ */
public static int getMaxByteArrayInitSize() { public static int getMaxByteArrayInitSize() {
return MAX_BYTE_ARRAY_INIT_SIZE; return MAX_BYTE_ARRAY_INIT_SIZE;
@ -167,7 +174,7 @@ public final class IOUtils {
* Reads up to {@code length} bytes from the input stream, and returns the bytes read. * Reads up to {@code length} bytes from the input stream, and returns the bytes read.
* *
* @param stream The byte stream of data to read. * @param stream The byte stream of data to read.
* @param length The maximum length to read, use Integer.MAX_VALUE to read the stream * @param length The maximum length to read, use {@link Integer#MAX_VALUE} to read the stream
* 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.
@ -224,15 +231,13 @@ public final class IOUtils {
} }
final int derivedLen = Math.min(length, derivedMaxLength); final int derivedLen = Math.min(length, derivedMaxLength);
int bufferLen = isLengthKnown ? derivedLen : Math.min(4096, derivedLen); final int byteArrayInitLen = calculateByteArrayInitLength(isLengthKnown, length, derivedMaxLength);
if (MAX_BYTE_ARRAY_INIT_SIZE > 0 && bufferLen > MAX_BYTE_ARRAY_INIT_SIZE) { final int internalBufferLen = DEFAULT_BUFFER_SIZE;
bufferLen = Math.min(bufferLen, MAX_BYTE_ARRAY_INIT_SIZE); try (UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream(byteArrayInitLen)) {
} byte[] buffer = new byte[internalBufferLen];
try (UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream(bufferLen)) {
byte[] buffer = new byte[4096];
int totalBytes = 0, readBytes; int totalBytes = 0, readBytes;
do { do {
readBytes = stream.read(buffer, 0, Math.min(buffer.length, derivedLen - totalBytes)); readBytes = stream.read(buffer, 0, Math.min(internalBufferLen, 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);
@ -255,6 +260,16 @@ public final class IOUtils {
} }
} }
//open for testing
static int calculateByteArrayInitLength(final boolean isLengthKnown, final int length, final int maxLength) {
final int derivedLen = Math.min(length, maxLength);
final int bufferLen = isLengthKnown ? derivedLen : Math.min(DEFAULT_BUFFER_SIZE, derivedLen);
if (MAX_BYTE_ARRAY_INIT_SIZE > 0 && bufferLen > MAX_BYTE_ARRAY_INIT_SIZE) {
return Math.min(bufferLen, MAX_BYTE_ARRAY_INIT_SIZE);
}
return bufferLen;
}
private static void checkLength(long length, int maxLength) { private static void checkLength(long length, int maxLength) {
if (BYTE_ARRAY_MAX_OVERRIDE > 0) { if (BYTE_ARRAY_MAX_OVERRIDE > 0) {
if (length > BYTE_ARRAY_MAX_OVERRIDE) { if (length > BYTE_ARRAY_MAX_OVERRIDE) {
@ -387,7 +402,7 @@ public final class IOUtils {
* @throws IOException If copying the data fails. * @throws IOException If copying the data fails.
*/ */
public static long copy(InputStream inp, OutputStream out, long limit) throws IOException { public static long copy(InputStream inp, OutputStream out, long limit) throws IOException {
final byte[] buff = new byte[4096]; final byte[] buff = new byte[DEFAULT_BUFFER_SIZE];
long totalCount = 0; long totalCount = 0;
int readBytes = -1; int readBytes = -1;
do { do {
@ -428,7 +443,7 @@ public final class IOUtils {
* Calculate checksum on input data * Calculate checksum on input data
*/ */
public static long calculateChecksum(byte[] data) { public static long calculateChecksum(byte[] data) {
Checksum sum = new CRC32(); final Checksum sum = new CRC32();
sum.update(data, 0, data.length); sum.update(data, 0, data.length);
return sum.getValue(); return sum.getValue();
} }
@ -440,9 +455,9 @@ public final class IOUtils {
* {@code IOUtils.calculateChecksum(IOUtils.toByteArray(stream))} * {@code IOUtils.calculateChecksum(IOUtils.toByteArray(stream))}
*/ */
public static long calculateChecksum(InputStream stream) throws IOException { public static long calculateChecksum(InputStream stream) throws IOException {
Checksum sum = new CRC32(); final Checksum sum = new CRC32();
byte[] buf = new byte[4096]; final byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
int count; int count;
while ((count = stream.read(buf)) != -1) { while ((count = stream.read(buf)) != -1) {
if (count > 0) { if (count > 0) {

View File

@ -167,6 +167,15 @@ final class TestIOUtils {
} }
} }
@Test
void testCalculateByteArrayInitLength() throws IOException {
assertEquals(4096, IOUtils.calculateByteArrayInitLength(false, 6000, 10000));
assertEquals(3000, IOUtils.calculateByteArrayInitLength(false, 3000, 10000));
assertEquals(3000, IOUtils.calculateByteArrayInitLength(false, 10000, 3000));
assertEquals(10000, IOUtils.calculateByteArrayInitLength(true, 10000, 12000));
assertEquals(10000, IOUtils.calculateByteArrayInitLength(true, 12000, 10000));
}
@Test @Test
void testSkipFully() throws IOException { void testSkipFully() throws IOException {
try (InputStream is = new FileInputStream(TMP)) { try (InputStream is = new FileInputStream(TMP)) {