Mercurial > hg > Database > Alice
changeset 463:f1293bbad9ac dispose
reuse inflater instance
author | sugi |
---|---|
date | Wed, 05 Nov 2014 01:44:31 +0900 |
parents | b8b8ce9032f8 |
children | 8434ff6a4b27 |
files | src/main/java/alice/datasegment/ReceiveData.java |
diffstat | 1 files changed, 50 insertions(+), 25 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/alice/datasegment/ReceiveData.java Tue Nov 04 13:40:25 2014 +0900 +++ b/src/main/java/alice/datasegment/ReceiveData.java Wed Nov 05 01:44:31 2014 +0900 @@ -1,51 +1,51 @@ 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; private boolean byteArray = false; - + public ReceiveData(Object obj, boolean cFlag, boolean sFlag){ val = obj; compressed = cFlag; serialized = sFlag; } - + public ReceiveData(byte[] obj, boolean cFlag, boolean sFlag){ val = obj; byteArray = true; compressed = cFlag; serialized = sFlag; } - + public boolean isByteArray(){ return byteArray; } - + public boolean compressed(){ return compressed; } - + public boolean serialized(){ return serialized; } - + public Object getObj(){ return val; } - + public String asString(){ if (serialized){ return asClass(String.class); @@ -53,7 +53,7 @@ return (String) val; } } - + public int asInteger(){ if (serialized){ return asClass(Integer.class); @@ -61,7 +61,7 @@ return (Integer) val; } } - + public Float asFloat(){ if (serialized){ return asClass(Float.class); @@ -69,7 +69,7 @@ return (Float) val; } } - + public Value getVal(){ if (serialized){ return asClass(Value.class); @@ -82,7 +82,7 @@ return null; } } - + @SuppressWarnings("unchecked") public <T> T asClass(Class<T> clazz) { try { @@ -95,24 +95,49 @@ } else { b = (byte[]) val; } - + if (serialized) { return SingletonMessage.getInstance().read(b, clazz); } else { return (T) b; } - } catch (IOException e) { + } catch (IOException | DataFormatException e) { e.printStackTrace(); return null; } } - - public byte[] unzip(byte[] zipped) throws IOException{ - Inflater inflater = new Inflater(); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - InflaterOutputStream ios = new InflaterOutputStream(os, inflater); - ios.write(zipped); - ios.finish(); - return os.toByteArray(); + + 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(); + } + + public void setInflater(Inflater inflater) { + this.inflater = inflater; } }