Permalink
Browse files

Implemented 'Deflated' method.

  • Loading branch information...
1 parent a4db87c commit b09fe043420f77c6ee5346998b9f6807438fce72 @eriksoe eriksoe committed Jun 23, 2011
View
5 src/main/java/com/trifork/deltazip/ChunkedMethod.java
@@ -33,13 +33,12 @@
*/
private static final int LIMIT_SO_DEFLATED_FITS_IN_64KB = 65000;
- //==================== API fulfillment ==============================
- public int methodNumber() {return DeltaZip.METHOD_CHUNKED;}
-
private static final int CHUNK_METHOD_DEFLATE = 0;
private static final int CHUNK_METHOD_PREFIX_COPY = 1;
private static final int CHUNK_METHOD_OFFSET_COPY = 2;
+ //==================== API fulfillment ==============================
+ public int methodNumber() {return DeltaZip.METHOD_CHUNKED;}
//==================== Uncompression: ========================================
public byte[] uncompress(ByteBuffer org, byte[] ref_data, Inflater inflater) throws IOException {
View
11 src/main/java/com/trifork/deltazip/DZUtil.java
@@ -108,6 +108,15 @@ public Dictionary(byte[] data, int off, int len) {
}
}
+ public static byte[] inflate(Inflater inflater, ByteBuffer src, int uncomp_length, Dictionary dict) {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ try {
+ inflate(inflater, src, uncomp_length, baos, dict);
+ baos.close();
+ } catch (IOException ioe) {throw new RuntimeException(ioe);}
+ return baos.toByteArray();
+ }
+
public static void inflate(Inflater inflater, ByteBuffer src, int comp_length, OutputStream dst, Dictionary dict) throws IOException {
inflater.reset();
InflaterOutputStream zos = new InflaterOutputStream(dst, inflater);
@@ -129,7 +138,7 @@ public static void inflate(Inflater inflater, ByteBuffer src, int comp_length, O
return baos.toByteArray();
}
- public static void deflate(Deflater deflater, ByteBuffer src, int uncomp_length, ByteArrayOutputStream dst, Dictionary dict) throws IOException {
+ public static void deflate(Deflater deflater, ByteBuffer src, int uncomp_length, OutputStream dst, Dictionary dict) throws IOException {
deflater.reset();
DeflaterOutputStream zos = new DeflaterOutputStream(dst, deflater);
WritableByteChannel channel = Channels.newChannel(zos);
View
24 src/main/java/com/trifork/deltazip/DeflatedMethod.java
@@ -0,0 +1,24 @@
+package com.trifork.deltazip;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import java.nio.ByteBuffer;
+
+import java.util.zip.Deflater;
+import java.util.zip.Inflater;
+
+class DeflatedMethod extends DeltaZip.CompressionMethod {
+ //==================== API fulfillment ==============================
+ public int methodNumber() {return DeltaZip.METHOD_DEFLATED;}
+
+ public byte[] uncompress(ByteBuffer org, byte[] _ref_data, Inflater inflater) throws IOException {
+ return DZUtil.inflate(inflater, org, org.remaining(), null);
+ }
+
+ public void compress(ByteBuffer org, byte[] _ref_data, OutputStream dst) throws IOException {
+ Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, true);
+ DZUtil.deflate(deflater, org, org.remaining(), dst, null);
+ }
+}
+
View
9 src/main/java/com/trifork/deltazip/DeltaZip.java
@@ -185,7 +185,12 @@ protected void pack_compressed(ByteBuffer version, byte[] ref_version, ExtByteAr
protected void pack_entry(ByteBuffer version, byte[] ref_version, CompressionMethod cm, ExtByteArrayOutputStream dst) {
int tag_blank = dst.insertBlank(4);
int size_before = dst.size();
- cm.compress(version.duplicate(), ref_version, dst);
+ try {
+ cm.compress(version.duplicate(), ref_version, dst);
+ } catch (IOException ioe) {
+ // Shouldn't happen; it's a ByteArrayOutputStream.
+ throw new RuntimeException(ioe);
+ }
int size_after = dst.size();
int length = size_after - size_before;
@@ -198,7 +203,7 @@ protected void pack_entry(ByteBuffer version, byte[] ref_version, CompressionMet
//==================== Compression methods =============================
protected static abstract class CompressionMethod {
public abstract int methodNumber();
- public abstract void compress(ByteBuffer org, byte[] ref_data, OutputStream dst);
+ public abstract void compress(ByteBuffer org, byte[] ref_data, OutputStream dst) throws IOException;
public abstract byte[] uncompress(ByteBuffer org, byte[] ref_data, Inflater inflater) throws IOException;
}

0 comments on commit b09fe04

Please sign in to comment.