Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 33A9D200C61 for ; Tue, 25 Apr 2017 20:46:05 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 3260F160BB6; Tue, 25 Apr 2017 18:46:05 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 5117B160BB3 for ; Tue, 25 Apr 2017 20:46:04 +0200 (CEST) Received: (qmail 99045 invoked by uid 500); 25 Apr 2017 18:46:01 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 98676 invoked by uid 99); 25 Apr 2017 18:46:01 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 25 Apr 2017 18:46:01 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 7EBD5DFC6A; Tue, 25 Apr 2017 18:46:01 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: bodewig@apache.org To: commits@commons.apache.org Date: Tue, 25 Apr 2017 18:46:09 -0000 Message-Id: In-Reply-To: <99494d56a5d341378ea86dcaba703929@git.apache.org> References: <99494d56a5d341378ea86dcaba703929@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [09/10] commons-compress git commit: merge hard reset -- move from other change list...ugh archived-at: Tue, 25 Apr 2017 18:46:05 -0000 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 Authored: Tue Apr 25 09:01:28 2017 -0400 Committer: tballison 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 {