packets.lib.ByteQueue.size()

Here are the examples of the java api packets.lib.ByteQueue.size() taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

1 Examples 7

15 Source : PacketBuilder.java
with GNU General Public License v3.0
from mircokroon

/**
 * If we're building a packet and we are given a compressionManager, that means we need to compress the packet
 * if it's large enough. The compression manager does compressing, but we still need to prefix the size and let the
 * client know if it was actually compressed.
 */
public ByteQueue build(CompressionManager compressionManager) {
    if (!compressionManager.isCompressionEnabled()) {
        return build();
    }
    byte[] original = bytes.toArray();
    byte[] compressed = compressionManager.compressPacket(original);
    // no compression happened
    if (compressed == original) {
        // without compression the prefix is packet length + 0 byte
        ByteQueue prefix = createVarInt(original.length + 1);
        prefix.insert((byte) 0);
        this.bytes.prepend(prefix);
    } else {
        // with compression we need to first prefix a varInt of the uncompressed data length
        ByteQueue dataLen = createVarInt(original.length);
        // ...and then prefix the length of the entire packet
        ByteQueue packetLen = createVarInt(dataLen.size() + compressed.length);
        byte[] res = new byte[compressed.length + dataLen.size() + packetLen.size()];
        System.arraycopy(packetLen.toArray(), 0, res, 0, packetLen.size());
        System.arraycopy(dataLen.toArray(), 0, res, packetLen.size(), dataLen.size());
        System.arraycopy(compressed, 0, res, packetLen.size() + dataLen.size(), compressed.length);
        return new ByteQueue(res);
    }
    return bytes;
}