Example usage for io.netty.buffer ByteBuf setByte

List of usage examples for io.netty.buffer ByteBuf setByte

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf setByte.

Prototype

public abstract ByteBuf setByte(int index, int value);

Source Link

Document

Sets the specified byte at the specified absolute index in this buffer.

Usage

From source file:se.sics.caracaldb.operations.OperationSerializer.java

License:Open Source License

@Override
public void toBinary(Object o, ByteBuf buf) {
    if (o instanceof CaracalMsg) {
        int flagPos = buf.writerIndex();
        buf.writeByte(0); // reserve for flags
        BitBuffer flags = BitBuffer.create(MSG); // 0
        toBinaryMsg((CaracalMsg) o, buf, flags);
        byte[] flagsB = flags.finalise();
        buf.setByte(flagPos, flagsB[0]);
        return;/*from  w  ww  . ja  v a2 s  . c  o m*/
    }
    if (o instanceof CaracalOp) {
        int flagPos = buf.writerIndex();
        buf.writeByte(0); // reserve for flags
        BitBuffer flags = BitBuffer.create(OP); // 0
        toBinaryOp((CaracalOp) o, buf, flags);
        byte[] flagsB = flags.finalise();
        buf.setByte(flagPos, flagsB[0]);
        return;
    }
    LOG.warn("Couldn't serialize {}: {}", o, o.getClass());
}

From source file:starbounddata.packets.Packet.java

License:Open Source License

/**
 * Recommended: For internal use with StarNub packet decoding
 * <p>//from  w  w w . j  a v a 2  s . com
 * Uses: This will write a s{@link starbounddata.types.variants.VLQ} to a {@link io.netty.buffer.ByteBuf}
 * <p>
 * Notes: This will not create a VLQ object and should be used
 * <p>
 *
 * @param out   ByteBuf in which is to be read
 * @param value long representing the VLQ value to be written out
 */
public static void writeSVLQPacketEncoder(ByteBuf out, long value) {
    if (value < 0) {
        value = ((-(value + 1)) << 1) | 1;
    } else {
        value = value << 1;
    }
    int numRelevantBits = 64 - Long.numberOfLeadingZeros(value);
    int numBytes = (numRelevantBits + 6) / 7;
    if (numBytes == 0) {
        numBytes = 1;
    }
    out.writerIndex(numBytes + 1); /* Sets the write index at the number of bytes + 1 byte for packet id */
    for (int i = numBytes - 1; i >= 0; i--) {
        int curByte = (int) (value & 0x7F);
        if (i != (numBytes - 1)) {
            curByte |= 0x80;
        }
        out.setByte(i + 1, curByte); /* Sets the byte at index + 1 byte for packet id */
        value >>>= 7;
    }
}

From source file:uk.co.thinkofdeath.thinkcraft.bukkit.world.ChunkManager.java

License:Apache License

private void gzipChunk(ChunkSnapshot chunk, ByteBuf out) {
    int mask = 0;
    int count = 0;
    for (int i = 0; i < 16; i++) {
        if (!chunk.isSectionEmpty(i)) {
            mask |= 1 << i;//  ww w .ja  v  a2  s  . c o m
            count++;
        }
    }
    ByteBuf data = allocator.buffer(16 * 16 * 16 * 4 * count + 3 + 256);
    data.writeByte(1); // The chunk exists
    data.writeShort(mask);
    int offset = 0;
    int blockDataOffset = 16 * 16 * 16 * 2 * count;
    int skyDataOffset = blockDataOffset + 16 * 16 * 16 * count;
    for (int i = 0; i < 16; i++) {
        if (!chunk.isSectionEmpty(i)) {
            for (int oy = 0; oy < 16; oy++) {
                for (int oz = 0; oz < 16; oz++) {
                    for (int ox = 0; ox < 16; ox++) {
                        int y = oy + (i << 4);
                        int id = chunk.getBlockTypeId(ox, y, oz);
                        int dValue = chunk.getBlockData(ox, y, oz);
                        data.setShort((offset << 1) + 3, (id << 4) | dValue);

                        data.setByte(blockDataOffset + offset + 3, chunk.getBlockEmittedLight(ox, y, oz));
                        data.setByte(skyDataOffset + offset + 3, chunk.getBlockSkyLight(ox, y, oz));

                        offset++;
                    }
                }
            }
        }
    }
    for (int x = 0; x < 16; x++) {
        for (int z = 0; z < 16; z++) {
            data.setByte(skyDataOffset + offset + 3 + x + z * 16, ThinkBiome.bukkitToId(chunk.getBiome(x, z)));
        }
    }
    data.writerIndex(data.capacity());
    try {
        GZIPOutputStream gzip = new GZIPOutputStream(new ByteBufOutputStream(out));
        byte[] bytes = new byte[data.readableBytes()];
        data.readBytes(bytes);
        gzip.write(bytes);
        gzip.close();
    } catch (IOException e) {
        throw new RuntimeException();
    } finally {
        data.release();
    }
}