diff --git a/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java index 9423d8a6407..a109a5388c2 100644 --- a/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/ar/ArArchiveInputStream.java @@ -435,13 +435,9 @@ private ArArchiveEntry parseEntry(final byte[] headerBuf) throws IOException { } } - /* - * (non-Javadoc) - * - * @see InputStream#read(byte[], int, int) - */ @Override public int read(final byte[] b, final int off, final int len) throws IOException { + org.apache.commons.io.IOUtils.checkFromIndexSize(b, off, len); if (len == 0) { return 0; } diff --git a/src/main/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStream.java index 44aca37590a..de81ffbedfc 100644 --- a/src/main/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/arj/ArjArchiveInputStream.java @@ -211,6 +211,7 @@ public ArjArchiveEntry getNextEntry() throws IOException { @Override public int read(final byte[] b, final int off, final int len) throws IOException { + org.apache.commons.io.IOUtils.checkFromIndexSize(b, off, len); if (len == 0) { return 0; } diff --git a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java index d996954072c..4b64ed8418a 100644 --- a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveInputStream.java @@ -371,17 +371,17 @@ public CpioArchiveEntry getNextEntry() throws IOException { * @param off the start offset of the data * @param len the maximum number of bytes read * @return the actual number of bytes read, or -1 if the end of the entry is reached + * @throws NullPointerException if b is null + * @throws IndexOutOfBoundsException if {@code off} or {@code len} are negative, or if {@code off + len} is greater than {@code b.length}. * @throws IOException if an I/O error has occurred or if a CPIO file error has occurred */ @Override public int read(final byte[] b, final int off, final int len) throws IOException { - checkOpen(); - if (off < 0 || len < 0 || off > b.length - len) { - throw new IndexOutOfBoundsException(); - } + org.apache.commons.io.IOUtils.checkFromIndexSize(b, off, len); if (len == 0) { return 0; } + checkOpen(); if (entry == null || entryEOF) { return -1; } diff --git a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java index e0e7d2361f7..54a1709e6ca 100644 --- a/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/cpio/CpioArchiveOutputStream.java @@ -31,6 +31,7 @@ import org.apache.commons.compress.archivers.ArchiveOutputStream; import org.apache.commons.compress.archivers.zip.ZipEncoding; import org.apache.commons.compress.archivers.zip.ZipEncodingHelper; +import org.apache.commons.io.IOUtils; /** * CpioArchiveOutputStream is a stream for writing CPIO streams. All formats of CPIO are supported (old ASCII, old binary, new portable format and the new @@ -313,17 +314,17 @@ public void putArchiveEntry(final CpioArchiveEntry entry) throws IOException { * @param b the data to be written * @param off the start offset in the data * @param len the number of bytes that are written + * @throws NullPointerException if b is null + * @throws IndexOutOfBoundsException if {@code off} or {@code len} are negative, or if {@code off + len} is greater than {@code b.length}. * @throws IOException if an I/O error has occurred or if a CPIO file error has occurred */ @Override public void write(final byte[] b, final int off, final int len) throws IOException { - checkOpen(); - if (off < 0 || len < 0 || off > b.length - len) { - throw new IndexOutOfBoundsException(); - } + IOUtils.checkFromIndexSize(b, off, len); if (len == 0) { return; } + checkOpen(); if (this.entry == null) { throw new ArchiveException("No current CPIO entry"); } diff --git a/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java index be4aba459db..38b9ba846d7 100644 --- a/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/dump/DumpArchiveInputStream.java @@ -390,18 +390,13 @@ public DumpArchiveSummary getSummary() { } /** - * Reads bytes from the current dump archive entry. + * {@inheritDoc} * - * This method is aware of the boundaries of the current entry in the archive and will deal with them as if they were this stream's start and EOF. - * - * @param buf The buffer into which to place bytes read. - * @param off The offset at which to place bytes read. - * @param len The number of bytes to read. - * @return The number of bytes read, or -1 at EOF. - * @throws IOException on error + *

This method is aware of the boundaries of the current entry in the archive and will deal with them as if they were this stream's start and EOF.

*/ @Override public int read(final byte[] buf, int off, int len) throws IOException { + org.apache.commons.io.IOUtils.checkFromIndexSize(buf, off, len); if (len == 0) { return 0; } diff --git a/src/main/java/org/apache/commons/compress/archivers/dump/TapeInputStream.java b/src/main/java/org/apache/commons/compress/archivers/dump/TapeInputStream.java index 3aeb8e8a5e2..48ed69d59e8 100644 --- a/src/main/java/org/apache/commons/compress/archivers/dump/TapeInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/dump/TapeInputStream.java @@ -127,10 +127,10 @@ public int read() throws IOException { *

* * @param len length to read, must be a multiple of the stream's record size. - * @throws IOException Thrown if an I/O error occurs. */ @Override public int read(final byte[] b, int off, final int len) throws IOException { + org.apache.commons.io.IOUtils.checkFromIndexSize(b, off, len); if (len == 0) { return 0; } diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java index 14a233157e5..8618577a02f 100644 --- a/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java +++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/AES256SHA256Decoder.java @@ -37,6 +37,7 @@ import org.apache.commons.compress.PasswordRequiredException; import org.apache.commons.compress.archivers.ArchiveException; +import org.apache.commons.io.IOUtils; final class AES256SHA256Decoder extends AbstractCoder { @@ -169,6 +170,7 @@ private void flushBuffer() throws IOException { @Override public void write(final byte[] b, final int off, final int len) throws IOException { + IOUtils.checkFromIndexSize(b, off, len); int gap = len + count > cipherBlockSize ? cipherBlockSize - count : len; System.arraycopy(b, off, cipherBlockBuffer, count, gap); count += gap; diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/BoundedSeekableByteChannelInputStream.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/BoundedSeekableByteChannelInputStream.java index 083364c414e..14076c7847f 100644 --- a/src/main/java/org/apache/commons/compress/archivers/sevenz/BoundedSeekableByteChannelInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/BoundedSeekableByteChannelInputStream.java @@ -23,6 +23,8 @@ import java.nio.ByteBuffer; import java.nio.channels.SeekableByteChannel; +import org.apache.commons.io.IOUtils; + final class BoundedSeekableByteChannelInputStream extends InputStream { private static final int MAX_BUF_LEN = 8192; private final ByteBuffer buffer; @@ -63,9 +65,18 @@ public int read() throws IOException { *

* This implementation may return 0 if the underlying {@link SeekableByteChannel} is non-blocking and currently hasn't got any bytes available. *

+ * + * @param b the buffer into which the data is read. + * @param off the start offset in array b at which the data is written. + * @param len the maximum number of bytes to read. + * @return the total number of bytes read into the buffer, or -1 if EOF is reached. + * @throws NullPointerException if b is null. + * @throws IndexOutOfBoundsException if {@code off} or {@code len} are negative, or if {@code off + len} is greater than {@code b.length}. + * @throws IOException if an I/O error occurs. */ @Override public int read(final byte[] b, final int off, final int len) throws IOException { + IOUtils.checkFromIndexSize(b, off, len); if (len == 0) { return 0; } diff --git a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java index 5a35824c151..5db75811c10 100644 --- a/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java +++ b/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java @@ -55,6 +55,7 @@ import org.apache.commons.compress.archivers.ArchiveEntry; import org.apache.commons.compress.archivers.ArchiveException; +import org.apache.commons.io.IOUtils; import org.apache.commons.io.file.attribute.FileTimes; import org.apache.commons.io.output.CountingOutputStream; @@ -87,6 +88,7 @@ public void write(final byte[] b) throws IOException { @Override public void write(final byte[] b, final int off, final int len) throws IOException { + IOUtils.checkFromIndexSize(b, off, len); if (len > BUF_SIZE) { channel.write(ByteBuffer.wrap(b, off, len)); } else { diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java index 80dd6859fb1..55d5705b0cb 100644 --- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveInputStream.java @@ -634,10 +634,14 @@ public boolean markSupported() { * @param offset The offset at which to place bytes read. * @param numToRead The number of bytes to read. * @return The number of bytes read, or -1 at EOF. + * @throws NullPointerException if {@code buf} is null + * @throws IndexOutOfBoundsException if {@code offset} or {@code numToRead} are negative, + * or if {@code offset + numToRead} is greater than {@code buf.length}. * @throws IOException on error */ @Override public int read(final byte[] buf, final int offset, int numToRead) throws IOException { + org.apache.commons.io.IOUtils.checkFromIndexSize(buf, offset, numToRead); if (numToRead == 0) { return 0; } diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java index 4ab04832a72..7024810fce8 100644 --- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveOutputStream.java @@ -39,6 +39,7 @@ import org.apache.commons.compress.archivers.zip.ZipEncodingHelper; import org.apache.commons.compress.utils.FixedLengthBlockOutputStream; import org.apache.commons.io.Charsets; +import org.apache.commons.io.IOUtils; import org.apache.commons.io.file.attribute.FileTimes; import org.apache.commons.io.output.CountingOutputStream; import org.apache.commons.lang3.ArrayFill; @@ -627,10 +628,14 @@ private void transferModTime(final TarArchiveEntry from, final TarArchiveEntry t * @param wBuf The buffer to write to the archive. * @param wOffset The offset in the buffer from which to get bytes. * @param numToWrite The number of bytes to write. + * @throws NullPointerException if {@code wBuf} is null + * @throws IndexOutOfBoundsException if {@code wOffset} or {@code numToWrite} are negative, + * or if {@code wOffset + numToWrite} is greater than {@code wBuf.length}. * @throws IOException on error */ @Override public void write(final byte[] wBuf, final int wOffset, final int numToWrite) throws IOException { + IOUtils.checkFromIndexSize(wBuf, wOffset, numToWrite); if (!haveUnclosedEntry) { throw new IllegalStateException("No current tar entry"); } diff --git a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveSparseZeroInputStream.java b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveSparseZeroInputStream.java index 513ed072222..263619a2a27 100644 --- a/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveSparseZeroInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/tar/TarArchiveSparseZeroInputStream.java @@ -18,7 +18,11 @@ */ package org.apache.commons.compress.archivers.tar; +import java.io.IOException; import java.io.InputStream; +import java.util.Arrays; + +import org.apache.commons.io.IOUtils; /** * This is an InputStream that always return 0, this is used when reading the "holes" of a sparse file @@ -35,6 +39,16 @@ public int read() { return 0; } + @Override + public int read(byte[] b, int off, int len) throws IOException { + IOUtils.checkFromIndexSize(b, off, len); + if (len == 0) { + return 0; + } + Arrays.fill(b, off, off + len, (byte) 0); + return len; + } + /** * Returns the input. * diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java index d02e625688a..1ea753abf1f 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java @@ -1033,6 +1033,7 @@ private void pushback(final byte[] buf, final int offset, final int length) thro @Override public int read(final byte[] buffer, final int offset, final int length) throws IOException { + org.apache.commons.io.IOUtils.checkFromIndexSize(buffer, offset, length); if (length == 0) { return 0; } diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java index 07d3492c5aa..4cbdf4f1e47 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java @@ -41,6 +41,7 @@ import org.apache.commons.compress.archivers.ArchiveException; import org.apache.commons.compress.archivers.ArchiveOutputStream; import org.apache.commons.io.Charsets; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.ArrayUtils; /** @@ -1568,10 +1569,14 @@ private int versionNeededToExtractMethod(final int zipMethod) { * @param b the byte array to write. * @param offset the start position to write from. * @param length the number of bytes to write. + * @throws NullPointerException if {@code b} is null + * @throws IndexOutOfBoundsException if {@code offset} or {@code length} are negative, + * or if {@code offset + length} is greater than {@code b.length}. * @throws IOException on error. */ @Override public void write(final byte[] b, final int offset, final int length) throws IOException { + IOUtils.checkFromIndexSize(b, offset, length); if (entry == null) { throw new IllegalStateException("No current entry"); } diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitOutputStream.java index accc0b8a041..834859bc4ed 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitOutputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitOutputStream.java @@ -33,6 +33,7 @@ import java.util.TreeMap; import org.apache.commons.compress.archivers.ArchiveException; +import org.apache.commons.io.IOUtils; import org.apache.commons.io.file.PathUtils; /** @@ -252,10 +253,14 @@ public void write(final byte[] b) throws IOException { * @param b data to write * @param off offset of the start of data in param b * @param len the length of data to write + * @throws NullPointerException if {@code b} is null + * @throws IndexOutOfBoundsException if {@code off} or {@code len} are negative, + * or if {@code off + len} is greater than {@code b.length}. * @throws IOException if an I/O error occurs. */ @Override public void write(final byte[] b, final int off, final int len) throws IOException { + IOUtils.checkFromIndexSize(b, off, len); if (len <= 0) { return; } diff --git a/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java index 40110dbdbe0..2f3784bc539 100644 --- a/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java +++ b/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorInputStream.java @@ -33,6 +33,7 @@ import org.apache.commons.compress.compressors.CompressorInputStream; import org.apache.commons.compress.utils.BitInputStream; import org.apache.commons.compress.utils.InputStreamStatistics; +import org.apache.commons.io.IOUtils; import org.apache.commons.io.input.CloseShieldInputStream; /** @@ -712,28 +713,15 @@ public int read() throws IOException { throw new CompressorException("Stream closed"); } - /* - * (non-Javadoc) - * - * @see InputStream#read(byte[], int, int) - */ @Override public int read(final byte[] dest, final int offs, final int len) throws IOException { - if (offs < 0) { - throw new IndexOutOfBoundsException("offs(" + offs + ") < 0."); - } - if (len < 0) { - throw new IndexOutOfBoundsException("len(" + len + ") < 0."); - } - if (offs + len > dest.length) { - throw new IndexOutOfBoundsException("offs(" + offs + ") + len(" + len + ") > dest.length(" + dest.length + ")."); + IOUtils.checkFromIndexSize(dest, offs, len); + if (len == 0) { + return 0; } if (this.bin == null) { throw new CompressorException("Stream closed"); } - if (len == 0) { - return 0; - } final int hi = offs + len; int destOffs = offs; diff --git a/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorOutputStream.java b/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorOutputStream.java index d3a9fd9089e..fb48afbb59f 100644 --- a/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorOutputStream.java +++ b/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorOutputStream.java @@ -23,6 +23,7 @@ import java.util.Arrays; import org.apache.commons.compress.compressors.CompressorOutputStream; +import org.apache.commons.io.IOUtils; /** * An output stream that compresses into the BZip2 format into another stream. @@ -1149,15 +1150,7 @@ private void sendMTFValues7() throws IOException { @Override public void write(final byte[] buf, int offs, final int len) throws IOException { - if (offs < 0) { - throw new IndexOutOfBoundsException("offs(" + offs + ") < 0."); - } - if (len < 0) { - throw new IndexOutOfBoundsException("len(" + len + ") < 0."); - } - if (offs + len > buf.length) { - throw new IndexOutOfBoundsException("offs(" + offs + ") + len(" + len + ") > buf.length(" + buf.length + ")."); - } + IOUtils.checkFromIndexSize(buf, offs, len); checkOpen(); for (final int hi = offs + len; offs < hi;) { write0(buf[offs++]); diff --git a/src/main/java/org/apache/commons/compress/compressors/deflate/DeflateCompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/deflate/DeflateCompressorInputStream.java index 9a0974da97e..274c8ef4437 100644 --- a/src/main/java/org/apache/commons/compress/compressors/deflate/DeflateCompressorInputStream.java +++ b/src/main/java/org/apache/commons/compress/compressors/deflate/DeflateCompressorInputStream.java @@ -110,12 +110,8 @@ public int read() throws IOException { return ret; } - /** {@inheritDoc} */ @Override public int read(final byte[] buf, final int off, final int len) throws IOException { - if (len == 0) { - return 0; - } final int ret = in.read(buf, off, len); count(ret); return ret; diff --git a/src/main/java/org/apache/commons/compress/compressors/deflate64/Deflate64CompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/deflate64/Deflate64CompressorInputStream.java index 0b5623b79d6..d0f35c51586 100644 --- a/src/main/java/org/apache/commons/compress/compressors/deflate64/Deflate64CompressorInputStream.java +++ b/src/main/java/org/apache/commons/compress/compressors/deflate64/Deflate64CompressorInputStream.java @@ -102,11 +102,9 @@ public int read() throws IOException { } } - /** - * @throws java.io.EOFException if the underlying stream is exhausted before the end of deflated data was reached. - */ @Override public int read(final byte[] b, final int off, final int len) throws IOException { + IOUtils.checkFromIndexSize(b, off, len); if (len == 0) { return 0; } diff --git a/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStream.java index 916f7672098..681d7961608 100644 --- a/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStream.java +++ b/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorInputStream.java @@ -445,6 +445,7 @@ public int read() throws IOException { */ @Override public int read(final byte[] b, int off, int len) throws IOException { + IOUtils.checkFromIndexSize(b, off, len); if (len == 0) { return 0; } diff --git a/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStream.java b/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStream.java index a62cee24426..e36b4cb7021 100644 --- a/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStream.java +++ b/src/main/java/org/apache/commons/compress/compressors/gzip/GzipCompressorOutputStream.java @@ -29,6 +29,7 @@ import org.apache.commons.compress.compressors.CompressorException; import org.apache.commons.compress.compressors.CompressorOutputStream; +import org.apache.commons.io.IOUtils; /** * Compressed output stream using the gzip format. This implementation improves over the standard {@link GZIPOutputStream} class by allowing the configuration @@ -127,6 +128,7 @@ public void write(final byte[] buffer) throws IOException { */ @Override public void write(final byte[] buffer, final int offset, final int length) throws IOException { + IOUtils.checkFromIndexSize(buffer, offset, length); checkOpen(); if (deflater.finished()) { throw new CompressorException("Cannot write more data, the end of the compressed data stream has been reached."); diff --git a/src/main/java/org/apache/commons/compress/compressors/lz4/BlockLZ4CompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/lz4/BlockLZ4CompressorInputStream.java index cb8d13364a0..5dffa27cc68 100644 --- a/src/main/java/org/apache/commons/compress/compressors/lz4/BlockLZ4CompressorInputStream.java +++ b/src/main/java/org/apache/commons/compress/compressors/lz4/BlockLZ4CompressorInputStream.java @@ -24,6 +24,7 @@ import org.apache.commons.compress.compressors.CompressorException; import org.apache.commons.compress.compressors.lz77support.AbstractLZ77CompressorInputStream; import org.apache.commons.compress.utils.ByteUtils; +import org.apache.commons.io.IOUtils; /** * CompressorInputStream for the LZ4 block format. @@ -89,11 +90,9 @@ private boolean initializeBackReference() throws IOException { return true; } - /** - * {@inheritDoc} - */ @Override public int read(final byte[] b, final int off, final int len) throws IOException { + IOUtils.checkFromIndexSize(b, off, len); if (len == 0) { return 0; } diff --git a/src/main/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorInputStream.java index 9128e534e9d..243fc5d2e4c 100644 --- a/src/main/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorInputStream.java +++ b/src/main/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorInputStream.java @@ -235,9 +235,9 @@ public int read() throws IOException { return read(oneByte, 0, 1) == -1 ? -1 : oneByte[0] & 0xFF; } - /** {@inheritDoc} */ @Override public int read(final byte[] b, final int off, final int len) throws IOException { + org.apache.commons.io.IOUtils.checkFromIndexSize(b, off, len); if (len == 0) { return 0; } diff --git a/src/main/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorOutputStream.java b/src/main/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorOutputStream.java index d53088dde81..f9f2c5b7acc 100644 --- a/src/main/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorOutputStream.java +++ b/src/main/java/org/apache/commons/compress/compressors/lz4/FramedLZ4CompressorOutputStream.java @@ -24,6 +24,7 @@ import org.apache.commons.compress.compressors.CompressorOutputStream; import org.apache.commons.compress.utils.ByteUtils; +import org.apache.commons.io.IOUtils; /** * CompressorOutputStream for the LZ4 frame format. @@ -271,6 +272,7 @@ private void flushBlock() throws IOException { @Override public void write(final byte[] data, int off, int len) throws IOException { + IOUtils.checkFromIndexSize(data, off, len); if (params.withContentChecksum) { contentHash.update(data, off, len); } diff --git a/src/main/java/org/apache/commons/compress/compressors/lz77support/LZ77Compressor.java b/src/main/java/org/apache/commons/compress/compressors/lz77support/LZ77Compressor.java index 9105187ac94..07e19766416 100644 --- a/src/main/java/org/apache/commons/compress/compressors/lz77support/LZ77Compressor.java +++ b/src/main/java/org/apache/commons/compress/compressors/lz77support/LZ77Compressor.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.util.Objects; +import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.ArrayFill; /** @@ -414,9 +415,12 @@ public void compress(final byte[] data) throws IOException { * @param data the data to compress - must not be null * @param off the start offset of the data * @param len the number of bytes to compress + * @throws NullPointerException if data is {@code null} + * @throws IndexOutOfBoundsException if {@code off} or {@code len} are negative, or if {@code off + len} is bigger than {@code data.length}. * @throws IOException if the callback throws an exception */ public void compress(final byte[] data, int off, int len) throws IOException { + IOUtils.checkFromIndexSize(data, off, len); final int wSize = params.getWindowSize(); while (len > wSize) { // chop into windowSize sized chunks doCompress(data, off, wSize); diff --git a/src/main/java/org/apache/commons/compress/compressors/lzw/LZWInputStream.java b/src/main/java/org/apache/commons/compress/compressors/lzw/LZWInputStream.java index 2015b96de87..54bd8841130 100644 --- a/src/main/java/org/apache/commons/compress/compressors/lzw/LZWInputStream.java +++ b/src/main/java/org/apache/commons/compress/compressors/lzw/LZWInputStream.java @@ -27,6 +27,7 @@ import org.apache.commons.compress.compressors.CompressorInputStream; import org.apache.commons.compress.utils.BitInputStream; import org.apache.commons.compress.utils.InputStreamStatistics; +import org.apache.commons.io.IOUtils; /** *

@@ -276,6 +277,7 @@ public int read() throws IOException { @Override public int read(final byte[] b, final int off, final int len) throws IOException { + IOUtils.checkFromIndexSize(b, off, len); if (len == 0) { return 0; } diff --git a/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java index f83f5251ea9..733be9582c2 100644 --- a/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java +++ b/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorInputStream.java @@ -198,9 +198,9 @@ public int read() throws IOException { return read(oneByte, 0, 1) == -1 ? -1 : oneByte[0] & 0xFF; } - /** {@inheritDoc} */ @Override public int read(final byte[] b, final int off, final int len) throws IOException { + org.apache.commons.io.IOUtils.checkFromIndexSize(b, off, len); if (len == 0) { return 0; } diff --git a/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorOutputStream.java b/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorOutputStream.java index ce3be3c3c4b..b5e733b7e2f 100644 --- a/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorOutputStream.java +++ b/src/main/java/org/apache/commons/compress/compressors/snappy/FramedSnappyCompressorOutputStream.java @@ -26,6 +26,7 @@ import org.apache.commons.compress.compressors.CompressorOutputStream; import org.apache.commons.compress.compressors.lz77support.Parameters; import org.apache.commons.compress.utils.ByteUtils; +import org.apache.commons.io.IOUtils; /** * CompressorOutputStream for the framing Snappy format. @@ -125,6 +126,7 @@ private void flushBuffer() throws IOException { @Override public void write(final byte[] data, int off, int len) throws IOException { + IOUtils.checkFromIndexSize(data, off, len); int blockDataRemaining = buffer.length - currentIndex; while (len > 0) { final int copyLen = Math.min(len, blockDataRemaining); diff --git a/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorInputStream.java index 03790331499..ec9b31fbcd7 100644 --- a/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorInputStream.java +++ b/src/main/java/org/apache/commons/compress/compressors/snappy/SnappyCompressorInputStream.java @@ -24,6 +24,7 @@ import org.apache.commons.compress.compressors.CompressorException; import org.apache.commons.compress.compressors.lz77support.AbstractLZ77CompressorInputStream; import org.apache.commons.compress.utils.ByteUtils; +import org.apache.commons.io.IOUtils; /** * CompressorInputStream for the raw Snappy format. @@ -192,11 +193,9 @@ public long getUncompressedSize() { return uncompressedSize; } - /** - * {@inheritDoc} - */ @Override public int read(final byte[] b, final int off, final int len) throws IOException { + IOUtils.checkFromIndexSize(b, off, len); if (len == 0) { return 0; } diff --git a/src/main/java/org/apache/commons/compress/compressors/xz/XZCompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/xz/XZCompressorInputStream.java index 9f8e2356efc..bc97d0f2d7a 100644 --- a/src/main/java/org/apache/commons/compress/compressors/xz/XZCompressorInputStream.java +++ b/src/main/java/org/apache/commons/compress/compressors/xz/XZCompressorInputStream.java @@ -240,9 +240,6 @@ public int read() throws IOException { @Override public int read(final byte[] buf, final int off, final int len) throws IOException { - if (len == 0) { - return 0; - } try { final int ret = in.read(buf, off, len); count(ret); diff --git a/src/main/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStream.java index 8b218738997..32b934568f4 100644 --- a/src/main/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStream.java +++ b/src/main/java/org/apache/commons/compress/compressors/zstandard/ZstdCompressorInputStream.java @@ -113,9 +113,6 @@ public int read(final byte[] b) throws IOException { @Override public int read(final byte[] buf, final int off, final int len) throws IOException { - if (len == 0) { - return 0; - } final int ret = decIS.read(buf, off, len); count(ret); return ret; diff --git a/src/main/java/org/apache/commons/compress/utils/BoundedArchiveInputStream.java b/src/main/java/org/apache/commons/compress/utils/BoundedArchiveInputStream.java index 2808964d938..c66b24a1cdf 100644 --- a/src/main/java/org/apache/commons/compress/utils/BoundedArchiveInputStream.java +++ b/src/main/java/org/apache/commons/compress/utils/BoundedArchiveInputStream.java @@ -22,6 +22,8 @@ import java.io.InputStream; import java.nio.ByteBuffer; +import org.apache.commons.io.IOUtils; + /** * NIO backed bounded input stream for reading a predefined amount of data. * @@ -69,6 +71,10 @@ public synchronized int read() throws IOException { @Override public synchronized int read(final byte[] b, final int off, final int len) throws IOException { + IOUtils.checkFromIndexSize(b, off, len); + if (len == 0) { + return 0; + } if (loc >= end) { return -1; } diff --git a/src/main/java/org/apache/commons/compress/utils/FixedLengthBlockOutputStream.java b/src/main/java/org/apache/commons/compress/utils/FixedLengthBlockOutputStream.java index 805b61413c9..ce7d341d1bd 100644 --- a/src/main/java/org/apache/commons/compress/utils/FixedLengthBlockOutputStream.java +++ b/src/main/java/org/apache/commons/compress/utils/FixedLengthBlockOutputStream.java @@ -201,6 +201,7 @@ private void padBlock() { @Override public void write(final byte[] b, final int offset, final int length) throws IOException { + IOUtils.checkFromIndexSize(b, offset, length); if (!isOpen()) { throw new ClosedChannelException(); }