Search in sources :

Example 1 with GZIPCompressedMessage

use of com.jme3.network.message.GZIPCompressedMessage in project jmonkeyengine by jMonkeyEngine.

the class GZIPSerializer method readObject.

@SuppressWarnings("unchecked")
public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
    try {
        GZIPCompressedMessage result = new GZIPCompressedMessage();
        byte[] byteArray = new byte[data.remaining()];
        data.get(byteArray);
        GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(byteArray));
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] tmp = new byte[9012];
        int read;
        while (in.available() > 0 && ((read = in.read(tmp)) > 0)) {
            out.write(tmp, 0, read);
        }
        result.setMessage((Message) Serializer.readClassAndObject(ByteBuffer.wrap(out.toByteArray())));
        return (T) result;
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException(e.toString());
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) GZIPCompressedMessage(com.jme3.network.message.GZIPCompressedMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IOException(java.io.IOException)

Example 2 with GZIPCompressedMessage

use of com.jme3.network.message.GZIPCompressedMessage in project jmonkeyengine by jMonkeyEngine.

the class GZIPSerializer method writeObject.

public void writeObject(ByteBuffer buffer, Object object) throws IOException {
    if (!(object instanceof GZIPCompressedMessage))
        return;
    Message message = ((GZIPCompressedMessage) object).getMessage();
    ByteBuffer tempBuffer = ByteBuffer.allocate(512000);
    Serializer.writeClassAndObject(tempBuffer, message);
    ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutput = new GZIPOutputStream(byteArrayOutput);
    tempBuffer.flip();
    gzipOutput.write(tempBuffer.array(), 0, tempBuffer.limit());
    gzipOutput.flush();
    gzipOutput.finish();
    gzipOutput.close();
    buffer.put(byteArrayOutput.toByteArray());
}
Also used : GZIPCompressedMessage(com.jme3.network.message.GZIPCompressedMessage) GZIPCompressedMessage(com.jme3.network.message.GZIPCompressedMessage) Message(com.jme3.network.Message) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer)

Aggregations

GZIPCompressedMessage (com.jme3.network.message.GZIPCompressedMessage)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 Message (com.jme3.network.Message)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1