Mercurial > hg > Database > Alice
view src/main/java/alice/datasegment/ReceiveData.java @ 452:f68d103498e0 dispose
refactor (InputDataSegment holder class changed)
author | sugi |
---|---|
date | Tue, 28 Oct 2014 17:24:16 +0900 |
parents | |
children | 41d80f5e1fb2 |
line wrap: on
line source
package alice.datasegment; import java.io.ByteArrayOutputStream; import java.io.IOException; 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; // both flag have to be true or false except DataSegment is byteArray; private boolean compressed = false; private boolean serialized = false; public ReceiveData(Object obj, boolean cFlag, boolean sFlag){ val = obj; compressed = cFlag; serialized = sFlag; } public boolean compressed(){ return compressed; } public boolean serialized(){ return serialized; } public Object getObj(){ return val; } public String asString(){ if (serialized){ return asClass(String.class); } else { return (String) val; } } public int asInteger(){ if (serialized){ return asClass(Integer.class); } else { return (Integer) val; } } public Float asFloat(){ if (serialized){ return asClass(Float.class); } else { return (Float) val; } } public Value getVal(){ if (serialized){ return asClass(Value.class); } else { try { return SingletonMessage.getInstance().unconvert(val); } catch (IOException e) { e.printStackTrace(); } return null; } } @SuppressWarnings("unchecked") public <T> T asClass(Class<T> clazz) { try { if (!serialized) return (T) val; byte[] b = null; if (compressed) { b = unzip((byte[]) val); } else { b = (byte[]) val; } return SingletonMessage.getInstance().read(b, clazz); } catch (IOException 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(); } }