diff options
author | Kevin Smith <git@kismith.co.uk> | 2011-07-01 09:19:49 (GMT) |
---|---|---|
committer | Kevin Smith <git@kismith.co.uk> | 2011-07-01 09:19:49 (GMT) |
commit | 2da71a8a85486a494343f1662d64fb5ae5a2a44e (patch) | |
tree | 23992f9f2a00bac23b345e5c2cc9c1194efc25be /src/com/isode/stroke/compress | |
download | stroke-2da71a8a85486a494343f1662d64fb5ae5a2a44e.zip stroke-2da71a8a85486a494343f1662d64fb5ae5a2a44e.tar.bz2 |
Initial import
Diffstat (limited to 'src/com/isode/stroke/compress')
-rw-r--r-- | src/com/isode/stroke/compress/ZLibCompressor.java | 33 | ||||
-rw-r--r-- | src/com/isode/stroke/compress/ZLibDecompressor.java | 35 | ||||
-rw-r--r-- | src/com/isode/stroke/compress/ZLibException.java | 19 |
3 files changed, 87 insertions, 0 deletions
diff --git a/src/com/isode/stroke/compress/ZLibCompressor.java b/src/com/isode/stroke/compress/ZLibCompressor.java new file mode 100644 index 0000000..f5276c8 --- /dev/null +++ b/src/com/isode/stroke/compress/ZLibCompressor.java @@ -0,0 +1,33 @@ +/* + * 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; + +/** + * + * @author Kev + */ +public class ZLibCompressor { + + private static final int COMPRESSION_LEVEL = 9; + + 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; + } +} diff --git a/src/com/isode/stroke/compress/ZLibDecompressor.java b/src/com/isode/stroke/compress/ZLibDecompressor.java new file mode 100644 index 0000000..2c78a57 --- /dev/null +++ b/src/com/isode/stroke/compress/ZLibDecompressor.java @@ -0,0 +1,35 @@ +/* + * 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; + +/** + * + * @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(); + } + } + +} diff --git a/src/com/isode/stroke/compress/ZLibException.java b/src/com/isode/stroke/compress/ZLibException.java new file mode 100644 index 0000000..48a2d31 --- /dev/null +++ b/src/com/isode/stroke/compress/ZLibException.java @@ -0,0 +1,19 @@ +/* + * 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; + +/** + * + * @author Kev + */ +public class ZLibException extends Exception { + +} |