summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/isode')
-rw-r--r--src/com/isode/stroke/base/ByteArray.java9
-rw-r--r--src/com/isode/stroke/compress/ZLibCodecompressor.java44
-rw-r--r--src/com/isode/stroke/compress/ZLibCompressor.java36
-rw-r--r--src/com/isode/stroke/compress/ZLibDecompressor.java39
4 files changed, 80 insertions, 48 deletions
diff --git a/src/com/isode/stroke/base/ByteArray.java b/src/com/isode/stroke/base/ByteArray.java
index 8b140ed..077c58c 100644
--- a/src/com/isode/stroke/base/ByteArray.java
+++ b/src/com/isode/stroke/base/ByteArray.java
@@ -149,8 +149,13 @@ public class ByteArray {
*
* @return a reference to the updated object
*/
- private ByteArray append(byte[] b) {
- for (int i = 0; i < b.length; i++) {
+ public ByteArray append(byte[] b) {
+ return append(b, b.length);
+ }
+
+ /** Mutable add */
+ public ByteArray append(byte[] b, int len) {
+ for (int i = 0; i < len; i++) {
append(b[i]);
}
return this;
diff --git a/src/com/isode/stroke/compress/ZLibCodecompressor.java b/src/com/isode/stroke/compress/ZLibCodecompressor.java
new file mode 100644
index 0000000..5f04442
--- /dev/null
+++ b/src/com/isode/stroke/compress/ZLibCodecompressor.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2010 Remko Tron¨on
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+/*
+ * Copyright (c) 2011, Isode Limited, London, England.
+ * All rights reserved.
+ */
+package com.isode.stroke.compress;
+
+import com.isode.stroke.base.ByteArray;
+import com.jcraft.jzlib.JZlib;
+import com.jcraft.jzlib.ZStream;
+
+public abstract class ZLibCodecompressor {
+ protected final int CHUNK_SIZE = 1024;
+ protected final ZStream stream_ = new ZStream();
+
+ public ByteArray process(ByteArray input) throws ZLibException {
+ ByteArray output = new ByteArray();
+ stream_.avail_in = input.getSize();
+ stream_.next_in = input.getData();
+ stream_.next_in_index = 0;
+ do {
+ byte[] outputArray = new byte[CHUNK_SIZE];
+ stream_.avail_out = CHUNK_SIZE;
+ stream_.next_out = outputArray;
+ stream_.next_out_index = 0;
+ int result = processZStream();
+ if (result != JZlib.Z_OK && result != JZlib.Z_BUF_ERROR) {
+ throw new ZLibException(/* stream_.msg */);
+ }
+ output.append(outputArray, CHUNK_SIZE - stream_.avail_out);
+ }
+ while (stream_.avail_out == 0);
+ if (stream_.avail_in != 0) {
+ throw new ZLibException();
+ }
+ return output;
+ }
+
+ protected abstract int processZStream();
+}
diff --git a/src/com/isode/stroke/compress/ZLibCompressor.java b/src/com/isode/stroke/compress/ZLibCompressor.java
index f5276c8..de22bee 100644
--- a/src/com/isode/stroke/compress/ZLibCompressor.java
+++ b/src/com/isode/stroke/compress/ZLibCompressor.java
@@ -1,33 +1,27 @@
/*
+ * Copyright (c) 2010 Remko Tron¨on
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+/*
* Copyright (c) 2011, Isode Limited, London, England.
* All rights reserved.
*/
package com.isode.stroke.compress;
-import com.isode.stroke.base.ByteArray;
-import java.util.zip.Deflater;
+import com.jcraft.jzlib.JZlib;
-/**
- *
- * @author Kev
- */
-public class ZLibCompressor {
+public class ZLibCompressor extends ZLibCodecompressor {
private static final int COMPRESSION_LEVEL = 9;
+
+
+ public ZLibCompressor() {
+ int result = stream_.deflateInit(COMPRESSION_LEVEL);
+ assert (result == JZlib.Z_OK);
+ }
- public ByteArray process(ByteArray data) throws ZLibException {
- Deflater compressor = new Deflater(COMPRESSION_LEVEL);
- compressor.setStrategy(Deflater.DEFAULT_STRATEGY);
- compressor.setInput(data.getData());
- compressor.finish();
- byte[] output = new byte[100];
- ByteArray result = new ByteArray();
- while (!compressor.finished()) {
- int size = compressor.deflate(output);
- for (int i = 0; i < size; i++) {
- result.append(output[i]); /* TODO: Terribly slow */
- }
- }
- return result;
+ protected int processZStream() {
+ return stream_.deflate(JZlib.Z_SYNC_FLUSH);
}
}
diff --git a/src/com/isode/stroke/compress/ZLibDecompressor.java b/src/com/isode/stroke/compress/ZLibDecompressor.java
index 2c78a57..82b9035 100644
--- a/src/com/isode/stroke/compress/ZLibDecompressor.java
+++ b/src/com/isode/stroke/compress/ZLibDecompressor.java
@@ -1,35 +1,24 @@
/*
+ * Copyright (c) 2010 Remko Tron¨on
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+/*
* Copyright (c) 2011, Isode Limited, London, England.
* All rights reserved.
*/
package com.isode.stroke.compress;
-import com.isode.stroke.base.ByteArray;
-import java.util.zip.DataFormatException;
-import java.util.zip.Inflater;
+import com.jcraft.jzlib.JZlib;
-/**
- *
- * @author Kev
- */
-public class ZLibDecompressor {
- Inflater inflater_ = new Inflater();
- public ByteArray process(ByteArray data) throws ZLibException {
- try {
- inflater_.setInput(data.getData());
- byte[] output = new byte[100];
- ByteArray result = new ByteArray();
- int size = 0;
- while ((size = inflater_.inflate(output)) != 0) {
- for (int i = 0; i < size; i++) {
- result.append(output[i]); /* TODO: Terribly slow */
- }
- }
- return result;
- }
- catch (DataFormatException e) {
- throw new ZLibException();
- }
+public class ZLibDecompressor extends ZLibCodecompressor {
+
+ public ZLibDecompressor() {
+ int result = stream_.inflateInit();
+ assert (result == JZlib.Z_OK);
}
+ protected int processZStream() {
+ return stream_.inflate(JZlib.Z_SYNC_FLUSH);
+ }
}