Search in sources :

Example 1 with InflaterInputStream

use of com.jcraft.jzlib.InflaterInputStream in project netxms by netxms.

the class NXCPMessage method createFromStream.

/**
 * Create NXCPMessage from prepared input byte stream
 * @param inputStream
 * @param byteArrayInputStream
 * @throws IOException
 */
private void createFromStream(NXCPDataInputStream inputStream) throws IOException {
    messageFlags = inputStream.readUnsignedShort();
    // Message size
    inputStream.skipBytes(4);
    messageId = (long) inputStream.readInt();
    if ((messageFlags & MF_BINARY) == MF_BINARY) {
        final int size = inputStream.readInt();
        binaryData = new byte[size];
        if (((messageFlags & MF_COMPRESSED) == MF_COMPRESSED) && ((messageFlags & MF_STREAM) == 0)) {
            // Compressed message
            // skip original message length
            inputStream.skip(4);
            inputStream = new NXCPDataInputStream(new InflaterInputStream(inputStream));
        }
        inputStream.readFully(binaryData);
    } else if ((messageFlags & MF_CONTROL) == MF_CONTROL) {
        controlData = inputStream.readUnsignedInt();
    } else {
        final int numVars = inputStream.readInt();
        if ((messageFlags & MF_COMPRESSED) == MF_COMPRESSED) {
            // Compressed message
            // skip original message length
            inputStream.skip(4);
            inputStream = new NXCPDataInputStream(new InflaterInputStream(inputStream));
        }
        for (int i = 0; i < numVars; i++) {
            // Read first 8 bytes - any DF (data field) is at least 8 bytes long
            byte[] df = new byte[32];
            inputStream.readFully(df, 0, 8);
            switch(df[4]) {
                case NXCPMessageField.TYPE_INT16:
                    break;
                // all these types requires additional 8 bytes
                case NXCPMessageField.TYPE_FLOAT:
                case NXCPMessageField.TYPE_INTEGER:
                case NXCPMessageField.TYPE_INT64:
                    inputStream.readFully(df, 8, 8);
                    break;
                // all these types has 4-byte length field followed by actual content
                case NXCPMessageField.TYPE_STRING:
                case NXCPMessageField.TYPE_BINARY:
                    int size = inputStream.readInt();
                    df = Arrays.copyOf(df, size + 12);
                    intToBytes(size, df, 8);
                    inputStream.readFully(df, 12, size);
                    // Each df aligned to 8-bytes boundary
                    final int rem = (size + 12) % 8;
                    if (rem != 0) {
                        inputStream.skipBytes(8 - rem);
                    }
                    break;
                case // address always 32 byte long
                NXCPMessageField.TYPE_INETADDR:
                    inputStream.readFully(df, 8, 24);
                    break;
            }
            final NXCPMessageField variable = new NXCPMessageField(df);
            fields.put(variable.getVariableId(), variable);
        }
    }
}
Also used : InflaterInputStream(com.jcraft.jzlib.InflaterInputStream)

Example 2 with InflaterInputStream

use of com.jcraft.jzlib.InflaterInputStream in project netxms by netxms.

the class ZlibTest method testCompression.

public void testCompression() throws Exception {
    ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
    bytesOut.write(new byte[] { 0x01, 0x02, 0x03, 0x04 });
    DeflaterOutputStream zout = new DeflaterOutputStream(bytesOut, new Deflater(JZlib.Z_BEST_COMPRESSION));
    byte[] bytes = TEXT.getBytes();
    zout.write(bytes);
    zout.close();
    byte[] zbytes = bytesOut.toByteArray();
    ByteArrayInputStream bytesIn = new ByteArrayInputStream(zbytes);
    bytesIn.skip(4);
    InflaterInputStream zin = new InflaterInputStream(bytesIn);
    byte[] dbytes = new byte[bytes.length];
    DataInputStream din = new DataInputStream(zin);
    din.readFully(dbytes);
    assertTrue(Arrays.equals(bytes, dbytes));
    zin.close();
    System.out.println(String.format("Size: clear text %d, compressed %d", bytes.length, zbytes.length));
}
Also used : Deflater(com.jcraft.jzlib.Deflater) ByteArrayInputStream(java.io.ByteArrayInputStream) InflaterInputStream(com.jcraft.jzlib.InflaterInputStream) DeflaterOutputStream(com.jcraft.jzlib.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream)

Aggregations

InflaterInputStream (com.jcraft.jzlib.InflaterInputStream)2 Deflater (com.jcraft.jzlib.Deflater)1 DeflaterOutputStream (com.jcraft.jzlib.DeflaterOutputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataInputStream (java.io.DataInputStream)1