Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Implemented 'Deflated' method.

  • Loading branch information...
commit b09fe043420f77c6ee5346998b9f6807438fce72 1 parent a4db87c
Erik Søe Sørensen eriksoe authored
5 src/main/java/com/trifork/deltazip/ChunkedMethod.java
@@ -33,13 +33,12 @@
33 33 */
34 34 private static final int LIMIT_SO_DEFLATED_FITS_IN_64KB = 65000;
35 35
36   - //==================== API fulfillment ==============================
37   - public int methodNumber() {return DeltaZip.METHOD_CHUNKED;}
38   -
39 36 private static final int CHUNK_METHOD_DEFLATE = 0;
40 37 private static final int CHUNK_METHOD_PREFIX_COPY = 1;
41 38 private static final int CHUNK_METHOD_OFFSET_COPY = 2;
42 39
  40 + //==================== API fulfillment ==============================
  41 + public int methodNumber() {return DeltaZip.METHOD_CHUNKED;}
43 42
44 43 //==================== Uncompression: ========================================
45 44 public byte[] uncompress(ByteBuffer org, byte[] ref_data, Inflater inflater) throws IOException {
11 src/main/java/com/trifork/deltazip/DZUtil.java
@@ -108,6 +108,15 @@ public Dictionary(byte[] data, int off, int len) {
108 108 }
109 109 }
110 110
  111 + public static byte[] inflate(Inflater inflater, ByteBuffer src, int uncomp_length, Dictionary dict) {
  112 + ByteArrayOutputStream baos = new ByteArrayOutputStream();
  113 + try {
  114 + inflate(inflater, src, uncomp_length, baos, dict);
  115 + baos.close();
  116 + } catch (IOException ioe) {throw new RuntimeException(ioe);}
  117 + return baos.toByteArray();
  118 + }
  119 +
111 120 public static void inflate(Inflater inflater, ByteBuffer src, int comp_length, OutputStream dst, Dictionary dict) throws IOException {
112 121 inflater.reset();
113 122 InflaterOutputStream zos = new InflaterOutputStream(dst, inflater);
@@ -129,7 +138,7 @@ public static void inflate(Inflater inflater, ByteBuffer src, int comp_length, O
129 138 return baos.toByteArray();
130 139 }
131 140
132   - public static void deflate(Deflater deflater, ByteBuffer src, int uncomp_length, ByteArrayOutputStream dst, Dictionary dict) throws IOException {
  141 + public static void deflate(Deflater deflater, ByteBuffer src, int uncomp_length, OutputStream dst, Dictionary dict) throws IOException {
133 142 deflater.reset();
134 143 DeflaterOutputStream zos = new DeflaterOutputStream(dst, deflater);
135 144 WritableByteChannel channel = Channels.newChannel(zos);
24 src/main/java/com/trifork/deltazip/DeflatedMethod.java
... ... @@ -0,0 +1,24 @@
  1 +package com.trifork.deltazip;
  2 +
  3 +import java.io.IOException;
  4 +import java.io.OutputStream;
  5 +
  6 +import java.nio.ByteBuffer;
  7 +
  8 +import java.util.zip.Deflater;
  9 +import java.util.zip.Inflater;
  10 +
  11 +class DeflatedMethod extends DeltaZip.CompressionMethod {
  12 + //==================== API fulfillment ==============================
  13 + public int methodNumber() {return DeltaZip.METHOD_DEFLATED;}
  14 +
  15 + public byte[] uncompress(ByteBuffer org, byte[] _ref_data, Inflater inflater) throws IOException {
  16 + return DZUtil.inflate(inflater, org, org.remaining(), null);
  17 + }
  18 +
  19 + public void compress(ByteBuffer org, byte[] _ref_data, OutputStream dst) throws IOException {
  20 + Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, true);
  21 + DZUtil.deflate(deflater, org, org.remaining(), dst, null);
  22 + }
  23 +}
  24 +
9 src/main/java/com/trifork/deltazip/DeltaZip.java
@@ -185,7 +185,12 @@ protected void pack_compressed(ByteBuffer version, byte[] ref_version, ExtByteAr
185 185 protected void pack_entry(ByteBuffer version, byte[] ref_version, CompressionMethod cm, ExtByteArrayOutputStream dst) {
186 186 int tag_blank = dst.insertBlank(4);
187 187 int size_before = dst.size();
188   - cm.compress(version.duplicate(), ref_version, dst);
  188 + try {
  189 + cm.compress(version.duplicate(), ref_version, dst);
  190 + } catch (IOException ioe) {
  191 + // Shouldn't happen; it's a ByteArrayOutputStream.
  192 + throw new RuntimeException(ioe);
  193 + }
189 194 int size_after = dst.size();
190 195 int length = size_after - size_before;
191 196
@@ -198,7 +203,7 @@ protected void pack_entry(ByteBuffer version, byte[] ref_version, CompressionMet
198 203 //==================== Compression methods =============================
199 204 protected static abstract class CompressionMethod {
200 205 public abstract int methodNumber();
201   - public abstract void compress(ByteBuffer org, byte[] ref_data, OutputStream dst);
  206 + public abstract void compress(ByteBuffer org, byte[] ref_data, OutputStream dst) throws IOException;
202 207 public abstract byte[] uncompress(ByteBuffer org, byte[] ref_data, Inflater inflater) throws IOException;
203 208 }
204 209

0 comments on commit b09fe04

Please sign in to comment.
Something went wrong with that request. Please try again.