allow callback to throw IOException

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

Branch: refs/heads/master
Commit: b8101b95c1d647b59697fed229bbbd8b5493b17c
Parents: e4658ae
Author: Stefan Bodewig <[email protected]>
Authored: Mon Jan 9 15:30:01 2017 +0100
Committer: Stefan Bodewig <[email protected]>
Committed: Mon Jan 9 15:31:51 2017 +0100

----------------------------------------------------------------------
 .../compressors/lz77support/LZ77Compressor.java | 26 ++++++++++++++------
 .../lz77support/LZ77CompressorTest.java         | 26 ++++++++++----------
 2 files changed, 31 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/b8101b95/src/main/java/org/apache/commons/compress/compressors/lz77support/LZ77Compressor.java
----------------------------------------------------------------------
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 7e10715..1eb5cfe 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
@@ -18,6 +18,8 @@
  */
 package org.apache.commons.compress.compressors.lz77support;
 
+import java.io.IOException;
+
 /**
  * Helper class for compression algorithms that use the ideas of LZ77.
  *
@@ -170,7 +172,12 @@ public class LZ77Compressor {
      * execution of {@link #compress} or {@link #finish}.</p>
      */
     public interface Callback /* extends Consumer<Block> */ {
-        void accept(Block b);
+        /**
+         * Consumes a block.
+         * @param b the block to consume
+         * @throws IOException in case of an error
+         */
+        void accept(Block b) throws IOException;
     }
 
     static final int NUMBER_OF_BYTES_IN_HASH = 3;
@@ -241,8 +248,9 @@ public class LZ77Compressor {
      * more blocks to the callback during the execution of this
      * method.
      * @param data the data to compress - must not be null
+     * @throws IOException if the callback throws an exception
      */
-    public void compress(byte[] data) {
+    public void compress(byte[] data) throws IOException {
         compress(data, 0, data.length);
     }
 
@@ -253,8 +261,9 @@ public class LZ77Compressor {
      * @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 IOException if the callback throws an exception
      */
-    public void compress(byte[] data, int off, int len) {
+    public void compress(byte[] data, int off, int len) throws IOException {
         final int wSize = params.getWindowSize();
         while (len > wSize) {
             doCompress(data, off, wSize);
@@ -273,8 +282,9 @@ public class LZ77Compressor {
      * <p>The compressor will in turn emit at least one block ({@link
      * EOD}) but potentially multiple blocks to the callback during
      * the execution of this method.</p>
+     * @throws IOException if the callback throws an exception
      */
-    public void finish() {
+    public void finish() throws IOException {
         if (blockStart != currentPosition || lookahead > 0) {
             currentPosition += lookahead;
             flushLiteralBlock();
@@ -301,7 +311,7 @@ public class LZ77Compressor {
     }
 
     // performs the actual algorithm with the pre-condition len <= windowSize
-    private void doCompress(byte[] data, int off, int len) {
+    private void doCompress(byte[] data, int off, int len) throws IOException {
         int spaceLeft = window.length - currentPosition - lookahead;
         if (len > spaceLeft) {
             slide();
@@ -339,7 +349,7 @@ public class LZ77Compressor {
         initialized = true;
     }
 
-    private void compress() {
+    private void compress() throws IOException {
         final int minMatch = params.getMinMatchLength();
 
         while (lookahead >= minMatch) {
@@ -405,11 +415,11 @@ public class LZ77Compressor {
         }
     }
 
-    private void flushBackReference(int matchLength) {
+    private void flushBackReference(int matchLength) throws IOException {
         callback.accept(new BackReference(currentPosition - matchStart, 
matchLength));
     }
 
-    private void flushLiteralBlock() {
+    private void flushLiteralBlock() throws IOException {
         callback.accept(new LiteralBlock(window, blockStart, currentPosition - 
blockStart));
     }
 

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/b8101b95/src/test/java/org/apache/commons/compress/compressors/lz77support/LZ77CompressorTest.java
----------------------------------------------------------------------
diff --git 
a/src/test/java/org/apache/commons/compress/compressors/lz77support/LZ77CompressorTest.java
 
b/src/test/java/org/apache/commons/compress/compressors/lz77support/LZ77CompressorTest.java
index 049a085..f2fa1f3 100644
--- 
a/src/test/java/org/apache/commons/compress/compressors/lz77support/LZ77CompressorTest.java
+++ 
b/src/test/java/org/apache/commons/compress/compressors/lz77support/LZ77CompressorTest.java
@@ -18,7 +18,7 @@
  */
 package org.apache.commons.compress.compressors.lz77support;
 
-import java.io.UnsupportedEncodingException;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -58,13 +58,13 @@ public class LZ77CompressorTest {
                    + "\n"
                    + "I do not like them, Sam-I-am.\n"
                    + "I do not like green eggs and ham.").getBytes("ASCII");
-        } catch (UnsupportedEncodingException ex) {
+        } catch (IOException ex) {
             throw new RuntimeException("ASCII not supported");
         }
         ONE_TO_TEN = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
     }
 
-    private List<LZ77Compressor.Block> compress(Parameters params, byte[]... 
chunks) {
+    private List<LZ77Compressor.Block> compress(Parameters params, byte[]... 
chunks) throws IOException {
         final List<LZ77Compressor.Block> blocks = new ArrayList<>();
         LZ77Compressor c = new LZ77Compressor(params, new 
LZ77Compressor.Callback() {
                 @Override
@@ -91,14 +91,14 @@ public class LZ77CompressorTest {
     }
 
     @Test
-    public void nonCompressableWithLengthSmallerThanLiteralMax() {
+    public void nonCompressableWithLengthSmallerThanLiteralMax() throws 
IOException {
         List<LZ77Compressor.Block> blocks = compress(new Parameters(128), 
ONE_TO_TEN);
         assertSize(2, blocks);
         assertLiteralBlock(ONE_TO_TEN, blocks.get(0));
     }
 
     @Test
-    public void 
nonCompressableWithLengthGreaterThanLiteralMaxButLessThanTwiceWindowSize() {
+    public void 
nonCompressableWithLengthGreaterThanLiteralMaxButLessThanTwiceWindowSize() 
throws IOException {
         List<LZ77Compressor.Block> blocks = compress(new Parameters(8), 
ONE_TO_TEN);
         assertSize(3, blocks);
         assertLiteralBlock(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }, 
blocks.get(0));
@@ -106,7 +106,7 @@ public class LZ77CompressorTest {
     }
 
     @Test
-    public void nonCompressableWithLengthThatForcesWindowSlide() {
+    public void nonCompressableWithLengthThatForcesWindowSlide() throws 
IOException {
         List<LZ77Compressor.Block> blocks = compress(new Parameters(4), 
ONE_TO_TEN);
         assertSize(4, blocks);
         assertLiteralBlock(new byte[] { 1, 2, 3, 4, }, blocks.get(0));
@@ -115,7 +115,7 @@ public class LZ77CompressorTest {
     }
 
     @Test
-    public void nonCompressableSentAsSingleBytes() {
+    public void nonCompressableSentAsSingleBytes() throws IOException {
         List<LZ77Compressor.Block> blocks = compress(new Parameters(8), 
stagger(ONE_TO_TEN));
         assertSize(3, blocks);
         assertLiteralBlock(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }, 
blocks.get(0));
@@ -124,7 +124,7 @@ public class LZ77CompressorTest {
 
     @Test
     public void blaExampleWithFullArrayAvailableForCompression()
-        throws UnsupportedEncodingException {
+        throws IOException {
         List<LZ77Compressor.Block> blocks = compress(new Parameters(128), BLA);
         assertSize(4, blocks);
         assertLiteralBlock("Blah b", blocks.get(0));
@@ -133,7 +133,7 @@ public class LZ77CompressorTest {
     }
 
     @Test
-    public void blaExampleWithShorterMatchLength() throws 
UnsupportedEncodingException {
+    public void blaExampleWithShorterMatchLength() throws IOException {
         List<LZ77Compressor.Block> blocks = compress(new Parameters(128, 3, 5, 
0, 0), BLA);
         assertSize(7, blocks);
         assertLiteralBlock("Blah b", blocks.get(0));
@@ -145,7 +145,7 @@ public class LZ77CompressorTest {
     }
 
     @Test
-    public void blaExampleSmallerWindowSize() throws 
UnsupportedEncodingException {
+    public void blaExampleSmallerWindowSize() throws IOException {
         List<LZ77Compressor.Block> blocks = compress(new Parameters(8), BLA);
         assertSize(5, blocks);
         assertLiteralBlock("Blah b", blocks.get(0));
@@ -156,7 +156,7 @@ public class LZ77CompressorTest {
     }
 
     @Test
-    public void blaExampleWithSingleByteWrites() throws 
UnsupportedEncodingException {
+    public void blaExampleWithSingleByteWrites() throws IOException {
         List<LZ77Compressor.Block> blocks = compress(new Parameters(128), 
stagger(BLA));
         assertEquals(9, blocks.size());
         assertLiteralBlock("Blah b", blocks.get(0));
@@ -170,7 +170,7 @@ public class LZ77CompressorTest {
     }
 
     @Test
-    public void samIAmExampleWithFullArrayAvailableForCompression() throws 
UnsupportedEncodingException {
+    public void samIAmExampleWithFullArrayAvailableForCompression() throws 
IOException {
         List<LZ77Compressor.Block> blocks = compress(new Parameters(1024), 
SAM);
         assertEquals(21, blocks.size());
         assertLiteralBlock("I am Sam\n\n", blocks.get(0));
@@ -201,7 +201,7 @@ public class LZ77CompressorTest {
     }
 
     private static final void assertLiteralBlock(String expectedContent, 
LZ77Compressor.Block block)
-        throws UnsupportedEncodingException {
+        throws IOException {
         assertLiteralBlock(expectedContent.getBytes("ASCII"), block);
     }
 

Reply via email to