changeset 464:8434ff6a4b27 dispose

use InflaterOutputStream
author sugi
date Wed, 05 Nov 2014 22:41:43 +0900
parents f1293bbad9ac
children cf0eb5b2b4d2
files src/main/java/alice/datasegment/ReceiveData.java
diffstat 1 files changed, 10 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- 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<ByteBuffer> bufferList = new LinkedList<ByteBuffer>();
-        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;
-    }
 }