Search in sources :

Example 1 with ZIPCompressedMessage

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

the class ZIPSerializer method readObject.

@SuppressWarnings("unchecked")
public <T> T readObject(ByteBuffer data, Class<T> c) throws IOException {
    try {
        ZIPCompressedMessage result = new ZIPCompressedMessage();
        byte[] byteArray = new byte[data.remaining()];
        data.get(byteArray);
        ZipInputStream in = new ZipInputStream(new ByteArrayInputStream(byteArray));
        in.getNextEntry();
        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);
        }
        in.closeEntry();
        out.flush();
        in.close();
        result.setMessage((Message) Serializer.readClassAndObject(ByteBuffer.wrap(out.toByteArray())));
        return (T) result;
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException(e.toString());
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ZIPCompressedMessage(com.jme3.network.message.ZIPCompressedMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IOException(java.io.IOException)

Example 2 with ZIPCompressedMessage

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

the class ZIPSerializer method writeObject.

public void writeObject(ByteBuffer buffer, Object object) throws IOException {
    if (!(object instanceof ZIPCompressedMessage))
        return;
    ZIPCompressedMessage zipMessage = (ZIPCompressedMessage) object;
    Message message = zipMessage.getMessage();
    ByteBuffer tempBuffer = ByteBuffer.allocate(512000);
    Serializer.writeClassAndObject(tempBuffer, message);
    ByteArrayOutputStream byteArrayOutput = new ByteArrayOutputStream();
    ZipOutputStream zipOutput = new ZipOutputStream(byteArrayOutput);
    zipOutput.setLevel(zipMessage.getLevel());
    ZipEntry zipEntry = new ZipEntry("zip");
    zipOutput.putNextEntry(zipEntry);
    tempBuffer.flip();
    zipOutput.write(tempBuffer.array(), 0, tempBuffer.limit());
    zipOutput.flush();
    zipOutput.closeEntry();
    zipOutput.close();
    buffer.put(byteArrayOutput.toByteArray());
}
Also used : Message(com.jme3.network.Message) ZIPCompressedMessage(com.jme3.network.message.ZIPCompressedMessage) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) ZIPCompressedMessage(com.jme3.network.message.ZIPCompressedMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ZIPCompressedMessage (com.jme3.network.message.ZIPCompressedMessage)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 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 ZipOutputStream (java.util.zip.ZipOutputStream)1