summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'src/com/isode/stroke/compress/ZLibCodecompressor.java')
-rw-r--r--src/com/isode/stroke/compress/ZLibCodecompressor.java44
1 files changed, 44 insertions, 0 deletions
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();
+}