merge hard reset -- move from other change list...ugh
Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/2247ff96 Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/2247ff96 Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/2247ff96 Branch: refs/heads/master Commit: 2247ff9601374845012558ecf5963ebb7e1066c5 Parents: 00b5e39 Author: tballison <talli...@mitre.org> Authored: Tue Apr 25 09:01:28 2017 -0400 Committer: tballison <talli...@mitre.org> Committed: Tue Apr 25 09:01:28 2017 -0400 ---------------------------------------------------------------------- .../compressors/CompressorStreamFactory.java | 36 ++++++++++++++++---- .../compressors/DetectCompressorTestCase.java | 19 ++--------- 2 files changed, 32 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-compress/blob/2247ff96/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java b/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java index 7daa291..f3433d9 100644 --- a/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java +++ b/src/main/java/org/apache/commons/compress/compressors/CompressorStreamFactory.java @@ -349,28 +349,52 @@ public class CompressorStreamFactory implements CompressorStreamProvider { */ private volatile boolean decompressConcatenated = false; + private final int memoryLimitInKb; /** * Create an instance with the decompress Concatenated option set to false. */ public CompressorStreamFactory() { this.decompressUntilEOF = null; + this.memoryLimitInKb = -1; } /** * Create an instance with the provided decompress Concatenated option. - * + * * @param decompressUntilEOF * if true, decompress until the end of the input; if false, stop * after the first stream and leave the input position to point * to the next byte after the stream. This setting applies to the * gzip, bzip2 and xz formats only. - * @since 1.10 + * + * @param memoryLimitInKb + * Some streams require allocation of potentially significant + * byte arrays/tables, and they can offer checks to prevent OOMs + * on corrupt files. Set the maximum allowed memory allocation in KBs. + * + * @since 1.14 */ - public CompressorStreamFactory(final boolean decompressUntilEOF) { + public CompressorStreamFactory(final boolean decompressUntilEOF, final int memoryLimitInKb) { this.decompressUntilEOF = Boolean.valueOf(decompressUntilEOF); // Also copy to existing variable so can continue to use that as the // current value this.decompressConcatenated = decompressUntilEOF; + this.memoryLimitInKb = memoryLimitInKb; + } + + + /** + * Create an instance with the provided decompress Concatenated option. + * + * @param decompressUntilEOF + * if true, decompress until the end of the input; if false, stop + * after the first stream and leave the input position to point + * to the next byte after the stream. This setting applies to the + * gzip, bzip2 and xz formats only. + * @since 1.10 + */ + public CompressorStreamFactory(final boolean decompressUntilEOF) { + this(decompressUntilEOF, -1); } /** @@ -505,14 +529,14 @@ public class CompressorStreamFactory implements CompressorStreamProvider { if (!XZUtils.isXZCompressionAvailable()) { throw new CompressorException("XZ compression is not available."); } - return new XZCompressorInputStream(in, actualDecompressConcatenated); + return new XZCompressorInputStream(in, actualDecompressConcatenated, memoryLimitInKb); } if (LZMA.equalsIgnoreCase(name)) { if (!LZMAUtils.isLZMACompressionAvailable()) { throw new CompressorException("LZMA compression is not available"); } - return new LZMACompressorInputStream(in); + return new LZMACompressorInputStream(in, memoryLimitInKb); } if (PACK200.equalsIgnoreCase(name)) { @@ -528,7 +552,7 @@ public class CompressorStreamFactory implements CompressorStreamProvider { } if (Z.equalsIgnoreCase(name)) { - return new ZCompressorInputStream(in); + return new ZCompressorInputStream(in, memoryLimitInKb); } if (DEFLATE.equalsIgnoreCase(name)) { http://git-wip-us.apache.org/repos/asf/commons-compress/blob/2247ff96/src/test/java/org/apache/commons/compress/compressors/DetectCompressorTestCase.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/compress/compressors/DetectCompressorTestCase.java b/src/test/java/org/apache/commons/compress/compressors/DetectCompressorTestCase.java index 2be690a..6fde36d 100644 --- a/src/test/java/org/apache/commons/compress/compressors/DetectCompressorTestCase.java +++ b/src/test/java/org/apache/commons/compress/compressors/DetectCompressorTestCase.java @@ -31,7 +31,6 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import org.apache.commons.compress.MemoryLimit; import org.apache.commons.compress.MemoryLimitException; import org.apache.commons.compress.MockEvilInputStream; import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream; @@ -39,8 +38,6 @@ import org.apache.commons.compress.compressors.deflate.DeflateCompressorInputStr import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; import org.apache.commons.compress.compressors.pack200.Pack200CompressorInputStream; import org.apache.commons.compress.compressors.xz.XZCompressorInputStream; -import org.junit.AfterClass; -import org.junit.Before; import org.junit.Test; @SuppressWarnings("deprecation") // deliberately tests setDecompressConcatenated @@ -74,18 +71,6 @@ public final class DetectCompressorTestCase { } } - @Before - public void setUp() { - //make sure to reset this before each test - MemoryLimit.setMemoryLimitInKb(MemoryLimit.NO_LIMIT); - } - - @AfterClass - public static void tearDown() { - //make sure this is really, truly reset after all the tests - MemoryLimit.setMemoryLimitInKb(MemoryLimit.NO_LIMIT); - } - private final TestData[] tests = { new TestData("multiple.bz2", new char[]{'a','b'}, factoryTrue, true), new TestData("multiple.bz2", new char[]{'a','b'}, factorySetTrue, true), @@ -217,8 +202,8 @@ public final class DetectCompressorTestCase { } private InputStream getStreamFor(final String fileName, final int memoryLimitInKb) throws Exception { - MemoryLimit.setMemoryLimitInKb(memoryLimitInKb); - CompressorStreamFactory fac = new CompressorStreamFactory(true); + CompressorStreamFactory fac = new CompressorStreamFactory(true, + memoryLimitInKb); InputStream is = new BufferedInputStream( new FileInputStream(getFile(fileName))); try {