Example usage for io.netty.buffer ByteBuf writeByte

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

Introduction

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

Prototype

public abstract ByteBuf writeByte(int value);

Source Link

Document

Sets the specified byte at the current writerIndex and increases the writerIndex by 1 in this buffer.

Usage

From source file:com.witjit.game.client.Client.java

License:Apache License

public static void main(String[] args) throws Exception {

    EventLoopGroup group = new NioEventLoopGroup();
    try {/*from ww  w  . j av  a 2s.  c om*/
        Bootstrap b = new Bootstrap();
        b.group(group).channel(NioSocketChannel.class).handler(new ClientInitializer());

        // Start the connection attempt.
        Channel ch = b.connect(HOST, PORT).sync().channel();

        // Read commands from the stdin.
        ChannelFuture lastWriteFuture = null;
        //            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        //            for (;;) {
        //                String line = in.readLine();
        //                if (line == null) {
        //                    break;
        //                }
        //
        //                // Sends the received line to the server.
        //                lastWriteFuture = ch.writeAndFlush(line + "\r\n");
        //
        //                // If user typed the 'bye' command, wait until the server closes
        //                // the connection.
        //                if ("bye".equals(line.toLowerCase())) {
        //                    ch.closeFuture().sync();
        //                    break;
        //                }
        //            }

        ByteBuf outb = Unpooled.buffer(200);
        outb.writeShort(127);
        outb.writeShort(10);
        outb.writeInt(10001);
        outb.writeByte(40);
        outb.writeByte(50);
        // Sends the received line to the server.
        lastWriteFuture = ch.writeAndFlush(outb);
        // Wait until all messages are flushed before closing the channel.
        if (lastWriteFuture != null) {
            lastWriteFuture.sync();
        }

        // Wait until the connection is closed.
        ch.closeFuture().sync();
    } finally {
        group.shutdownGracefully();
    }
}

From source file:com.witjit.game.client.EchoClient.java

License:Apache License

private static void sendData(Channel ch) throws InterruptedException {
    ByteBuf outb = Unpooled.buffer(200);
    outb.writeShort(127);//from  w w w . j ava  2  s .  c  o m
    outb.writeShort(10);
    outb.writeInt(10001);
    outb.writeByte(40);
    outb.writeByte(50);
    // Sends the received line to the server.
    ChannelFuture writeFuture = ch.writeAndFlush(outb);
    // Wait until all messages are flushed before closing the channel.
    if (writeFuture != null) {
        writeFuture.sync();
    }

}

From source file:com.yahoo.pulsar.utils.NumberFormat.java

License:Apache License

static void format(ByteBuf out, long num) {
    if (num == 0) {
        out.writeByte('0');
        return;//from  w  w  w  . j  ava 2 s .  c  om
    }

    // Long.MIN_VALUE needs special handling since abs(Long.MIN_VALUE) = abs(Long.MAX_VALUE) + 1
    boolean encounteredMinValue = (num == Long.MIN_VALUE);
    if (num < 0) {
        out.writeByte('-');
        num += encounteredMinValue ? 1 : 0;
        num *= -1;
    }

    // Putting the number in bytebuf in reverse order
    int start = out.writerIndex();
    formatHelper(out, num);
    int end = out.writerIndex();

    if (encounteredMinValue) {
        out.setByte(start, out.getByte(start) + 1);
    }

    // Reversing the digits
    end--;
    for (int i = 0; i <= (end - start) / 2; i++) {
        byte tmp = out.getByte(end - i);
        out.setByte(end - i, out.getByte(start + i));
        out.setByte(start + i, tmp);
    }
}

From source file:com.yahoo.pulsar.utils.NumberFormat.java

License:Apache License

static void formatHelper(ByteBuf out, long num) {
    while (num != 0) {
        out.writeByte((int) ('0' + num % 10));
        num /= 10;/*  w ww .  ja v a2  s  .co m*/
    }
}

From source file:com.yea.remote.netty.codec.NettyMessageEncoder.java

License:Apache License

@Override
protected void encode(ChannelHandlerContext ctx, Message msg, ByteBuf sendBuf) throws Exception {
    if (msg == null || msg.getHeader() == null) {
        throw new Exception("The encode message is null");
    }//from ww w.j  a v  a  2 s.  co m
    ISerializer serializer = serializePool.borrow();
    try {
        long basedate = new Date().getTime();
        sendBuf.writeInt((msg.getHeader().getCrcCode()));// 4
        sendBuf.writeInt((msg.getHeader().getLength()));// 4Header+BodyHeader18
        sendBuf.writeBytes((msg.getHeader().getSessionID()));// 16
        sendBuf.writeByte((msg.getHeader().getType()));// 1
        sendBuf.writeByte((msg.getHeader().getPriority()));// 1
        sendBuf.writeByte((msg.getHeader().getResult()));// 1
        if (compressionAlgorithm != null && compressionAlgorithm.code() > 0) {
            // 
            serializer.setCompress(new Compress().setCompressionAlgorithm(compressionAlgorithm.algorithm()));
            sendBuf.writeByte(compressionAlgorithm.ordinal());
        } else {
            sendBuf.writeByte(RemoteConstants.CompressionAlgorithm.NONE.ordinal());
        }
        sendBuf.writeLong(basedate);// 8??
        if (msg.getHeader().getAttachment() != null && !msg.getHeader().getAttachment().isEmpty()) {
            sendBuf.writeByte(msg.getHeader().getAttachment().size());
            Map<String, Number> mapDateType = new HashMap<String, Number>();// Date
            Map<String, String> mapStringType = new HashMap<String, String>();// String

            int lengthPos = sendBuf.writerIndex();
            sendBuf.writeBytes(new byte[1]);// 1?Date?
            byte[] keyArray = null;
            byte[] valueArray = null;
            for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) {
                if (param.getValue() instanceof Date) {
                    //Date???byte
                    long time = basedate - ((Date) param.getValue()).getTime();
                    if (time > Integer.MAX_VALUE) {
                        mapDateType.put(param.getKey(), new Long(time));
                    } else if (time > Short.MAX_VALUE) {
                        mapDateType.put(param.getKey(), new Integer((int) time));
                    } else if (time > Byte.MAX_VALUE) {
                        mapDateType.put(param.getKey(), new Short((short) time));
                    } else {
                        mapDateType.put(param.getKey(), new Byte((byte) time));
                    }
                } else if (param.getValue() instanceof String) {
                    //??getBytes()?
                    mapStringType.put(param.getKey(), (String) param.getValue());
                } else {
                    keyArray = param.getKey().getBytes("ISO-8859-1");
                    sendBuf.writeShort(keyArray.length);
                    sendBuf.writeBytes(keyArray);

                    valueArray = serializer.serialize(param.getValue());
                    sendBuf.writeShort(valueArray.length);
                    sendBuf.writeBytes(valueArray);
                }
            }
            sendBuf.setByte(lengthPos,
                    msg.getHeader().getAttachment().size() - mapDateType.size() - mapStringType.size());

            if (mapDateType.isEmpty()) {
                sendBuf.writeByte(0);// 1Date?0
            } else {
                sendBuf.writeByte(mapDateType.size());// 1Date?
                for (Map.Entry<String, Number> param : mapDateType.entrySet()) {
                    keyArray = param.getKey().getBytes("ISO-8859-1");
                    sendBuf.writeShort(keyArray.length);
                    sendBuf.writeBytes(keyArray);

                    if (param.getValue() instanceof Long) {
                        sendBuf.writeByte(8);
                        sendBuf.writeLong((Long) param.getValue());
                    } else if (param.getValue() instanceof Integer) {
                        sendBuf.writeByte(4);
                        sendBuf.writeInt((Integer) param.getValue());
                    } else if (param.getValue() instanceof Short) {
                        sendBuf.writeByte(2);
                        sendBuf.writeShort((Short) param.getValue());
                    } else {
                        sendBuf.writeByte(1);
                        sendBuf.writeByte((Byte) param.getValue());
                    }
                }
            }

            if (mapStringType.isEmpty()) {
                sendBuf.writeByte(0);// 1String?0
            } else {
                sendBuf.writeByte(mapStringType.size());// 1String?
                for (Map.Entry<String, String> param : mapStringType.entrySet()) {
                    keyArray = param.getKey().getBytes("ISO-8859-1");
                    sendBuf.writeShort(keyArray.length);
                    sendBuf.writeBytes(keyArray);

                    valueArray = param.getValue().getBytes("ISO-8859-1");
                    sendBuf.writeShort(valueArray.length);
                    sendBuf.writeBytes(valueArray);
                }
            }
        } else {
            sendBuf.writeByte(0);// 20
        }

        if (msg.getBody() != null) {
            byte[] objArray = serializer.serialize(msg.getBody());
            int lengthPos = sendBuf.writerIndex();
            sendBuf.writeBytes(LENGTH_PLACEHOLDER);// 4Body
            sendBuf.writeBytes(objArray);
            sendBuf.setInt(lengthPos, sendBuf.writerIndex() - lengthPos - 4);// Body
        } else {
            sendBuf.writeInt(0);
        }
        sendBuf.setInt(4, sendBuf.readableBytes() - 8);// Header+Body
    } finally {
        serializePool.restore(serializer);
    }
}

From source file:com.yogpc.mc_lib.APacketTile.java

License:Open Source License

@Override
public final Packet getDescriptionPacket() {
    final ByteBuf buf = Unpooled.buffer();
    buf.writeByte(0);
    new YogpstopPacket(this).writeData(buf);
    return new FMLProxyPacket(buf, "YogpstopLib");
}

From source file:com.zz.learning.netty5.chap12.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");
    }/*w ww . ja v a  2 s.com*/
    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;
    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,
            //? crcCode,length
            sendBuf.readableBytes() - 8);
}

From source file:cubicchunks.network.PacketCubeBlockChange.java

License:MIT License

@SuppressWarnings("deprecation")
@Override//from ww w. j  ava  2  s.com
public void toBytes(ByteBuf out) {
    out.writeInt(cubePos.getX());
    out.writeInt(cubePos.getY());
    out.writeInt(cubePos.getZ());
    out.writeShort(localAddresses.length);
    for (int i = 0; i < localAddresses.length; i++) {
        out.writeShort(localAddresses[i]);
        ByteBufUtils.writeVarInt(out, Block.BLOCK_STATE_IDS.get(blockStates[i]), 4);
    }
    out.writeByte(heightValues.length);
    for (int v : heightValues) {
        out.writeInt(v);
    }
}

From source file:dan200.qcraft.shared.QCraftPacket.java

License:Open Source License

@Override
public void toBytes(ByteBuf buffer) {
    buffer.writeByte(packetType);
    if (dataString != null) {
        buffer.writeByte(dataString.length);
    } else {/*from ww  w  .j a  va  2s.  com*/
        buffer.writeByte(0);
    }
    if (dataInt != null) {
        buffer.writeByte(dataInt.length);
    } else {
        buffer.writeByte(0);
    }
    if (dataByte != null) {
        buffer.writeInt(dataByte.length);
    } else {
        buffer.writeInt(0);
    }
    if (dataString != null) {
        for (String s : dataString) {
            if (s != null) {
                try {
                    byte[] b = s.getBytes("UTF-8");
                    buffer.writeBoolean(true);
                    buffer.writeInt(b.length);
                    buffer.writeBytes(b);
                } catch (UnsupportedEncodingException e) {
                    buffer.writeBoolean(false);
                }
            } else {
                buffer.writeBoolean(false);
            }
        }
    }
    if (dataInt != null) {
        for (int i : dataInt) {
            buffer.writeInt(i);
        }
    }
    if (dataByte != null) {
        for (byte[] bytes : dataByte) {
            if (bytes != null) {
                buffer.writeInt(bytes.length);
                buffer.writeBytes(bytes);
            } else {
                buffer.writeInt(0);
            }
        }
    }
}

From source file:de.gandev.modjn.entity.func.ModbusError.java

License:Apache License

@Override
public ByteBuf encode() {
    ByteBuf buf = Unpooled.buffer(calculateLength());
    buf.writeByte(getFunctionCode());
    buf.writeByte(exceptionCode);//w  ww .j av a  2s.  c  om

    return buf;
}