# HG changeset patch # User sugi # Date 1415194903 -32400 # Node ID 8434ff6a4b272f53e7539be4a7a96566bb9a3da8 # Parent f1293bbad9ac7f05c7dd3588c0b971650cccbbba use InflaterOutputStream diff -r f1293bbad9ac -r 8434ff6a4b27 src/main/java/alice/datasegment/ReceiveData.java --- a/src/main/java/alice/datasegment/ReceiveData.java Wed Nov 05 01:44:31 2014 +0900 +++ b/src/main/java/alice/datasegment/ReceiveData.java Wed Nov 05 22:41:43 2014 +0900 @@ -1,17 +1,18 @@ package alice.datasegment; +import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.LinkedList; import java.util.zip.DataFormatException; import java.util.zip.Inflater; +import java.util.zip.InflaterOutputStream; + import org.msgpack.type.Value; import alice.codesegment.SingletonMessage; public class ReceiveData { private Object val; - private Inflater inflater; + // both flag have to be true or false except DataSegment is byteArray; private boolean compressed = false; private boolean serialized = false; @@ -108,36 +109,12 @@ } public byte[] unzip(byte[] input) throws IOException, DataFormatException{ - LinkedList bufferList = new LinkedList(); - inflater.setInput(input); - ByteBuffer buf = ByteBuffer.allocate(1024 * 100); - int position = 0; - do { - int len = inflater.inflate(buf.array()); - if (len > 0) { - position +=len; - bufferList.add(buf); - buf = ByteBuffer.allocate(1024 * 100); - } - } while (!inflater.needsInput()); - - ByteBuffer output = ByteBuffer.allocate(position); - for (ByteBuffer b : bufferList) { - if (position - b.limit() > 0) { - b.get(output.array(), output.position(), b.limit()); - output.position(output.position() + b.limit()); - position -=b.limit(); - } - else { - b.get(output.array(), output.position(), position); - output.position(output.limit()); - output.flip(); - } - } - return output.array(); + Inflater inflater = new Inflater(); + ByteArrayOutputStream os = new ByteArrayOutputStream(); + InflaterOutputStream ios = new InflaterOutputStream(os, inflater); + ios.write(input); + ios.finish(); + return os.toByteArray(); } - public void setInflater(Inflater inflater) { - this.inflater = inflater; - } }