Browse Source

refactor IOUtils.toByteArray

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1898290 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_5_2_1
PJ Fanning 2 years ago
parent
commit
8fc59014fe
1 changed files with 26 additions and 21 deletions
  1. 26
    21
      poi/src/main/java/org/apache/poi/util/IOUtils.java

+ 26
- 21
poi/src/main/java/org/apache/poi/util/IOUtils.java View File

* 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 {@link Integer#MIN_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
* @param maxLength if the input is equal to/longer than {@code maxLength} bytes, * @param maxLength if the input is equal to/longer than {@code maxLength} bytes,
* then throw an {@link IOException} complaining about the length. * then throw an {@link IOException} complaining about the length.
* @throws RecordFormatException If the requested length is invalid. * @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 < 0 && length != Integer.MIN_VALUE) || maxLength < 0) {
return toByteArray(stream, length, maxLength, true);
}

/**
* Reads the input stream, and returns the bytes read.
*
* @param stream The byte stream of data to read.
* @param maxLength if the input is equal to/longer than {@code maxLength} bytes,
* then throw an {@link IOException} complaining about the length.
* use {@link Integer#MAX_VALUE} to disable the check - if {@link #setByteArrayMaxOverride(int)} is
* set then that max of that value and this maxLength is used
* @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 RecordFormatException If the requested length is invalid.
* @since POI 5.2.1
*/
public static byte[] toByteArrayWithMaxLength(InputStream stream, final int maxLength) throws IOException {
return toByteArray(stream, maxLength, maxLength, false);
}

private static byte[] toByteArray(InputStream stream, final int length, final int maxLength,
final boolean checkEOFException) throws IOException {
if (length < 0 || 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 derivedLen = length == Integer.MIN_VALUE ? derivedMaxLength : Math.min(length, derivedMaxLength);
final int derivedLen = Math.min(length, derivedMaxLength);
try (UnsynchronizedByteArrayOutputStream baos = try (UnsynchronizedByteArrayOutputStream baos =
new UnsynchronizedByteArrayOutputStream(derivedLen == Integer.MAX_VALUE ? 4096 : derivedLen)) { new UnsynchronizedByteArrayOutputStream(derivedLen == Integer.MAX_VALUE ? 4096 : derivedLen)) {
byte[] buffer = new byte[4096]; byte[] buffer = new byte[4096];
throw new IOException("MaxLength (" + derivedMaxLength + ") reached - stream seems to be invalid."); throw new IOException("MaxLength (" + derivedMaxLength + ") reached - stream seems to be invalid.");
} }


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


} }
} }


/**
* Reads the input stream, and returns the bytes read.
*
* @param stream The byte stream of data to read.
* @param maxLength if the input is equal to/longer than {@code maxLength} bytes,
* then throw an {@link IOException} complaining about the length.
* use {@link Integer#MAX_VALUE} to disable the check - if {@link #setByteArrayMaxOverride(int)} is
* set then that max of that value and this maxLength is used
* @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 RecordFormatException If the requested length is invalid.
* @since POI 5.2.1
*/
public static byte[] toByteArrayWithMaxLength(InputStream stream, final int maxLength) throws IOException {
return toByteArray(stream, Integer.MIN_VALUE, maxLength);
}

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) {

Loading…
Cancel
Save