You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2017/02/06 05:26:19 UTC

[2/2] commons-compress git commit: COMPRESS-271 tests for prefill

COMPRESS-271 tests for prefill

and clarify AbstractLZ77CompressorInputStream javadocs


Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/de2738ae
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/de2738ae
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/de2738ae

Branch: refs/heads/master
Commit: de2738ae40b56965218fd3359cff20f520239f2c
Parents: 9d03179
Author: Stefan Bodewig <bo...@apache.org>
Authored: Mon Feb 6 06:25:34 2017 +0100
Committer: Stefan Bodewig <bo...@apache.org>
Committed: Mon Feb 6 06:25:34 2017 +0100

----------------------------------------------------------------------
 .../AbstractLZ77CompressorInputStream.java      | 10 ++-
 .../AbstractLZ77CompressorInputStreamTest.java  | 85 ++++++++++++++++++++
 2 files changed, 94 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/de2738ae/src/main/java/org/apache/commons/compress/compressors/lz77support/AbstractLZ77CompressorInputStream.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/compressors/lz77support/AbstractLZ77CompressorInputStream.java b/src/main/java/org/apache/commons/compress/compressors/lz77support/AbstractLZ77CompressorInputStream.java
index a406b85..29bb320 100644
--- a/src/main/java/org/apache/commons/compress/compressors/lz77support/AbstractLZ77CompressorInputStream.java
+++ b/src/main/java/org/apache/commons/compress/compressors/lz77support/AbstractLZ77CompressorInputStream.java
@@ -31,7 +31,15 @@ import org.apache.commons.compress.utils.IOUtils;
  *
  * <p>Assumes the stream consists of blocks of literal data and
  * back-references (called copies) in any order. Of course the first
- * block must be a literal block for the scheme to work.</p>
+ * block must be a literal block for the scheme to work - unless the
+ * {@link #prefill prefill} method has been used to provide initial
+ * data that is never returned by {@link #read read} but only used for
+ * back-references.</p>
+ *
+ * <p>Subclasses must override the three-arg {@link #read read} method
+ * as the no-arg version delegates to it and the default
+ * implementation delegates to the no-arg version, leading to infinite
+ * mutual recursion and a {@code StackOverflowError} otherwise.</p>
  *
  * @since 1.14
  */

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/de2738ae/src/test/java/org/apache/commons/compress/compressors/lz77support/AbstractLZ77CompressorInputStreamTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/compress/compressors/lz77support/AbstractLZ77CompressorInputStreamTest.java b/src/test/java/org/apache/commons/compress/compressors/lz77support/AbstractLZ77CompressorInputStreamTest.java
new file mode 100644
index 0000000..1b8f7f9
--- /dev/null
+++ b/src/test/java/org/apache/commons/compress/compressors/lz77support/AbstractLZ77CompressorInputStreamTest.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.commons.compress.compressors.lz77support;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+public class AbstractLZ77CompressorInputStreamTest {
+
+    private static class TestStream extends AbstractLZ77CompressorInputStream {
+        private boolean literal;
+        TestStream(InputStream in) throws IOException {
+            super(in, 1024);
+        }
+        @Override
+        public int read(final byte[] b, final int off, final int len) throws IOException {
+            if (literal) {
+                return readLiteral(b, off, len);
+            }
+            return readBackReference(b, off, len);
+        }
+        void literal(int len) {
+            startLiteral(len);
+            literal = true;
+        }
+    }
+
+    @Test(expected = IllegalStateException.class)
+    public void cantPrefillAfterDataHasBeenRead() throws IOException {
+        byte[] data = new byte[] { 1, 2, 3, 4 };
+        try (TestStream s = new TestStream(new ByteArrayInputStream(data))) {
+            s.literal(3);
+            assertEquals(1, s.read());
+            s.prefill(new byte[] { 1, 2, 3 });
+        }
+    }
+
+    @Test
+    public void prefillCanBeUsedForBackReferences() throws IOException {
+        byte[] data = new byte[] { 1, 2, 3, 4 };
+        try (TestStream s = new TestStream(new ByteArrayInputStream(new byte[0]))) {
+            s.prefill(data);
+            s.startBackReference(2, 4);
+            byte[] r = new byte[4];
+            assertEquals(4, s.read(r));
+            assertArrayEquals(new byte[] { 3, 4, 3, 4 }, r);
+        }
+    }
+
+    @Test
+    public void ifPrefillExceedsWindowSizeTheLastBytesAreUsed() throws IOException {
+        byte[] data = new byte[2048];
+        data[2046] = 3;
+        data[2047] = 4;
+        try (TestStream s = new TestStream(new ByteArrayInputStream(new byte[0]))) {
+            s.prefill(data);
+            s.startBackReference(2, 4);
+            byte[] r = new byte[4];
+            assertEquals(4, s.read(r));
+            assertArrayEquals(new byte[] { 3, 4, 3, 4 }, r);
+        }
+    }
+}