Example usage for io.netty.buffer ByteBuf writeBytes

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

Introduction

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

Prototype

public abstract ByteBuf writeBytes(ByteBuffer src);

Source Link

Document

Transfers the specified source buffer's data to this buffer starting at the current writerIndex until the source buffer's position reaches its limit, and increases the writerIndex by the number of the transferred bytes.

Usage

From source file:blazingcache.network.netty.DodoMessageUtils.java

License:Apache License

private static void writeUTF8String(ByteBuf buf, String s) {
    byte[] asarray = s.getBytes(StandardCharsets.UTF_8);
    buf.writeInt(asarray.length);//from   www  .  j  a  v  a  2 s  .  co  m
    buf.writeBytes(asarray);
}

From source file:blazingcache.network.netty.DodoMessageUtils.java

License:Apache License

private static void writeEncodedSimpleValue(ByteBuf encoded, Object o) {
    if (o == null) {
        encoded.writeByte(OPCODE_NULL_VALUE);
    } else if (o instanceof String) {
        encoded.writeByte(OPCODE_STRING_VALUE);
        writeUTF8String(encoded, (String) o);
    } else if (o instanceof Integer) {
        encoded.writeByte(OPCODE_INT_VALUE);
        encoded.writeInt((Integer) o);
    } else if (o instanceof Long) {
        encoded.writeByte(OPCODE_LONG_VALUE);
        encoded.writeLong((Long) o);
    } else if (o instanceof Set) {
        Set set = (Set) o;
        encoded.writeByte(OPCODE_SET_VALUE);
        encoded.writeInt(set.size());/*  www . ja va  2  s.co m*/
        for (Object o2 : set) {
            writeEncodedSimpleValue(encoded, o2);
        }
    } else if (o instanceof List) {
        List set = (List) o;
        encoded.writeByte(OPCODE_LIST_VALUE);
        encoded.writeInt(set.size());
        for (Object o2 : set) {
            writeEncodedSimpleValue(encoded, o2);
        }

    } else if (o instanceof byte[]) {
        byte[] set = (byte[]) o;
        encoded.writeByte(OPCODE_BYTEARRAY_VALUE);
        encoded.writeInt(set.length);
        encoded.writeBytes(set);
    } else if (o instanceof Map) {
        Map set = (Map) o;
        encoded.writeByte(OPCODE_MAP_VALUE);
        encoded.writeInt(set.size());
        for (Map.Entry entry : (Iterable<Entry>) set.entrySet()) {
            writeEncodedSimpleValue(encoded, entry.getKey());
            writeEncodedSimpleValue(encoded, entry.getValue());
        }
    } else {
        throw new RuntimeException("unsupported class " + o.getClass());
    }
}

From source file:blazingcache.network.netty.MessageUtils.java

License:Apache License

private static void writeEncodedSimpleValue(ByteBuf encoded, Object o) {
    if (o == null) {
        encoded.writeByte(OPCODE_NULL_VALUE);
    } else if (o instanceof String) {
        encoded.writeByte(OPCODE_STRING_VALUE);
        writeUTF8String(encoded, (String) o);
    } else if (o instanceof RawString) {
        encoded.writeByte(OPCODE_STRING_VALUE);
        writeUTF8String(encoded, ((RawString) o).toString());
    } else if (o instanceof Integer) {
        encoded.writeByte(OPCODE_INT_VALUE);
        encoded.writeInt((Integer) o);
    } else if (o instanceof Long) {
        encoded.writeByte(OPCODE_LONG_VALUE);
        encoded.writeLong((Long) o);
    } else if (o instanceof Set) {
        Set set = (Set) o;
        encoded.writeByte(OPCODE_SET_VALUE);
        encoded.writeInt(set.size());//w  w  w  .  java2 s .com
        for (Object o2 : set) {
            writeEncodedSimpleValue(encoded, o2);
        }
    } else if (o instanceof List) {
        List set = (List) o;
        encoded.writeByte(OPCODE_LIST_VALUE);
        encoded.writeInt(set.size());
        for (Object o2 : set) {
            writeEncodedSimpleValue(encoded, o2);
        }

    } else if (o instanceof byte[]) {
        byte[] set = (byte[]) o;
        encoded.writeByte(OPCODE_BYTEARRAY_VALUE);
        encoded.writeInt(set.length);
        encoded.writeBytes(set);
    } else if (o instanceof Map) {
        Map set = (Map) o;
        encoded.writeByte(OPCODE_MAP_VALUE);
        encoded.writeInt(set.size());
        for (Map.Entry entry : (Iterable<Entry>) set.entrySet()) {
            writeEncodedSimpleValue(encoded, entry.getKey());
            writeEncodedSimpleValue(encoded, entry.getValue());
        }
    } else {
        throw new RuntimeException("unsupported class " + o.getClass());
    }
}

From source file:book.netty.n2defualtC4P2.TimeClientHandler.java

License:Apache License

@Override
public void channelActive(ChannelHandlerContext ctx) {
    ByteBuf message = null;
    for (int i = 0; i < 100; i++) {
        message = Unpooled.buffer(req.length);
        message.writeBytes(req);
        ctx.writeAndFlush(message);/*  ww  w  .j a  v  a 2s.com*/
    }
}

From source file:books.netty.protocol.netty.codec.MarshallingEncoder.java

License:Apache License

protected void encode(Object msg, ByteBuf out) throws Exception {
    try {// w  w w. j  ava 2 s .c o  m
        int lengthPos = out.writerIndex();
        out.writeBytes(LENGTH_PLACEHOLDER);
        ChannelBufferByteOutput output = new ChannelBufferByteOutput(out);
        marshaller.start(output);
        marshaller.writeObject(msg);
        marshaller.finish();
        out.setInt(lengthPos, out.writerIndex() - lengthPos - 4);
    } finally {
        marshaller.close();
    }
}

From source file:books.netty.protocol.netty.codec.NettyMessageEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, NettyMessage msg, ByteBuf sendBuf) throws Exception {
    if (msg == null || msg.getHeader() == null)
        throw new Exception("The encode message is null");
    sendBuf.writeInt((msg.getHeader().getCrcCode()));
    sendBuf.writeInt((msg.getHeader().getLength()));
    sendBuf.writeLong((msg.getHeader().getSessionID()));
    sendBuf.writeByte((msg.getHeader().getType()));
    sendBuf.writeByte((msg.getHeader().getPriority()));
    sendBuf.writeInt((msg.getHeader().getAttachment().size()));
    String key = null;//from  w ww  . j av  a2 s  .  c  o  m
    byte[] keyArray = null;
    Object value = null;
    for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) {
        key = param.getKey();
        keyArray = key.getBytes("UTF-8");
        sendBuf.writeInt(keyArray.length);
        sendBuf.writeBytes(keyArray);
        value = param.getValue();
        marshallingEncoder.encode(value, sendBuf);
    }
    key = null;
    keyArray = null;
    value = null;
    if (msg.getBody() != null) {
        marshallingEncoder.encode(msg.getBody(), sendBuf);
    } else
        sendBuf.writeInt(0);
    sendBuf.setInt(4, sendBuf.readableBytes() - 8);
}

From source file:books.netty.protocol.netty.codec.TestCodeC.java

License:Apache License

public ByteBuf encode(NettyMessage msg) throws Exception {
    ByteBuf sendBuf = Unpooled.buffer();
    sendBuf.writeInt((msg.getHeader().getCrcCode()));
    sendBuf.writeInt((msg.getHeader().getLength()));
    sendBuf.writeLong((msg.getHeader().getSessionID()));
    sendBuf.writeByte((msg.getHeader().getType()));
    sendBuf.writeByte((msg.getHeader().getPriority()));
    sendBuf.writeInt((msg.getHeader().getAttachment().size()));
    String key = null;/*from www  .j a v  a  2  s . c  o  m*/
    byte[] keyArray = null;
    Object value = null;

    for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) {
        key = param.getKey();
        keyArray = key.getBytes("UTF-8");
        sendBuf.writeInt(keyArray.length);
        sendBuf.writeBytes(keyArray);
        value = param.getValue();
        marshallingEncoder.encode(value, sendBuf);
    }
    key = null;
    keyArray = null;
    value = null;
    if (msg.getBody() != null) {
        marshallingEncoder.encode(msg.getBody(), sendBuf);
    } else
        sendBuf.writeInt(0);
    sendBuf.setInt(4, sendBuf.readableBytes());
    return sendBuf;
}

From source file:buildcraft.commander.ContainerZonePlan.java

License:Minecraft Mod Public

private void computeMap(int cx, int cz, int width, int height, int blocksPerPixel, EntityPlayer player) {
    final byte[] textureData = new byte[width * height];

    int startX = cx - width * blocksPerPixel / 2;
    int startZ = cz - height * blocksPerPixel / 2;

    for (int i = 0; i < width; ++i) {
        for (int j = 0; j < height; ++j) {
            int x = startX + i * blocksPerPixel;
            int z = startZ + j * blocksPerPixel;
            int ix = x - (map.chunkStartX << 4);
            int iz = z - (map.chunkStartZ << 4);

            if (ix >= 0 && iz >= 0 && ix < TileZonePlan.RESOLUTION && iz < TileZonePlan.RESOLUTION) {
                textureData[i + j * width] = map.colors[ix + iz * TileZonePlan.RESOLUTION];
            }//from   ww w. j av a 2s .  c  om
        }
    }

    BuildCraftCore.instance.sendToPlayer(player, new PacketCommand(this, "receiveImage", new CommandWriter() {
        public void write(ByteBuf data) {
            data.writeMedium(textureData.length);
            data.writeBytes(textureData);
        }
    }));
}

From source file:buildcraft.core.lib.network.PacketGuiReturn.java

License:Minecraft Mod Public

@Override
public void writeData(ByteBuf data) {
    data.writeInt(obj.getWorld().provider.dimensionId);

    if (obj instanceof TileEntity) {
        TileEntity tile = (TileEntity) obj;
        data.writeBoolean(true);//from   w  w w.  j a v  a2s .co m
        data.writeInt(tile.xCoord);
        data.writeInt(tile.yCoord);
        data.writeInt(tile.zCoord);
    } else if (obj instanceof Entity) {
        Entity entity = (Entity) obj;
        data.writeBoolean(false);
        data.writeInt(entity.getEntityId());
    } else {
        return;
    }

    obj.writeGuiData(data);

    if (extraData != null) {
        data.writeBytes(extraData);
    }
}

From source file:buildcraft.core.lib.network.PacketGuiWidget.java

License:Minecraft Mod Public

@Override
public void writeData(ByteBuf data) {
    data.writeByte(windowId);
    data.writeByte(widgetId);
    data.writeBytes(payload);
}