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.seagate.kinetic.common.protocol.codec.KineticEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, KineticMessage km, ByteBuf out) throws Exception {

    try {//from   w w w. j  ava  2  s  .  c om

        // get value to write separately
        // byte[] value = builder.getValue().toByteArray();
        byte[] value = km.getValue();

        // 1. write magic number
        out.writeByte((byte) 'F');

        //get message
        Message.Builder messageBuilder = (Builder) km.getMessage();

        // build message
        Message msg = messageBuilder.build();

        // get proto message bytes
        byte[] protoMessageBytes = msg.toByteArray();

        // 2. write protobuf message message size, 4 byte
        out.writeInt(protoMessageBytes.length);

        // 3. write attached value size, 4 byte
        if (value != null) {
            out.writeInt(value.length);
        } else {
            out.writeInt(0);
        }

        // 4. write protobuf message byte[]
        out.writeBytes(protoMessageBytes);

        // 5 (optional) write attached value if any
        if (value != null && value.length > 0) {
            // write value
            out.writeBytes(value);
        }

        // log message out
        if (printMessage) {

            logger.info("outbound protocol message:");

            String printMsg = ProtocolMessageUtil.toString(km);

            logger.info(printMsg);
        }

    } catch (Exception e) {
        logger.log(Level.WARNING, e.getMessage(), e);
        throw e;
    }
}

From source file:com.shekar.msrp.codec.MsrpResponseStatus.java

void encode(ByteBuf buf) {
    if (bytes == null) {
        MsrpHeaders.encodeUtf8(String.valueOf(code()), buf);
        buf.writeByte(MsrpConstants.SP);
        if (comment() != null) {
            MsrpHeaders.encodeUtf8(comment(), buf);
        }/*from w  ww . j  a  va 2s.c o  m*/
    } else {
        buf.writeBytes(bytes);
    }
}

From source file:com.spotify.folsom.client.binary.BinaryMemcacheDecoderTest.java

License:Apache License

@Test
public void test() throws Exception {
    GetRequest request = new GetRequest(KEY, OpCode.GET, 123, OPAQUE);
    BinaryMemcacheDecoder decoder = new BinaryMemcacheDecoder();

    ByteBuf cb = Unpooled.buffer(30);
    cb.writeByte(0x81);
    cb.writeByte(OpCode.GET);/*from w  w w. ja v  a2 s .co  m*/
    cb.writeShort(3);
    cb.writeByte(0);
    cb.writeZero(1);
    cb.writeShort(0);
    cb.writeInt(6);
    cb.writeInt(request.getOpaque());
    cb.writeLong(258);
    cb.writeBytes(KEY.getBytes());
    cb.writeBytes(VALUE.getBytes());

    List<Object> out = Lists.newArrayList();
    decoder.decode(null, cb, out);
    @SuppressWarnings("unchecked")
    List<ResponsePacket> replies = (List<ResponsePacket>) out.get(0);
    request.handle(replies);

    GetResult<byte[]> getResult = request.get();
    assertEquals(258, getResult.getCas());
    assertEquals(VALUE, TRANSCODER.decode(getResult.getValue()));
}

From source file:com.spotify.netty.handler.codec.zmtp.ZMTPUtils.java

License:Apache License

/**
 * Helper to encode a zmtp length field/*from ww  w . jav a2 s.  c  o  m*/
 */
static public void encodeLength(final long size, final ByteBuf out, boolean forceLong) {
    if (size < 255 && !forceLong) {

        // Encoded as a single byte
        out.writeByte((byte) size);
    } else {
        out.writeByte(0xFF);
        writeLong(out, size);
    }
}

From source file:com.spotify.netty.handler.codec.zmtp.ZMTPUtils.java

License:Apache License

static void encodeZMTP2FrameHeader(final long size, final byte flags, final ByteBuf out) {
    if (size < 256) {
        out.writeByte(flags);
        out.writeByte((byte) size);
    } else {//from  w ww  . j  a v  a  2 s . co m
        out.writeByte(flags | 0x02);
        writeLong(out, size);
    }
}

From source file:com.spotify.netty.handler.codec.zmtp.ZMTPUtils.java

License:Apache License

/**
 * Writes a ZMTP frame to a buffer./*from   ww w  .  j ava  2  s  . com*/
 *
 * @param frame  The frame to write.
 * @param buffer The target buffer.
 * @param more   True to write a more flag, false to write a final flag.
 */
public static void writeFrame(final ZMTPFrame frame, final ByteBuf buffer, final boolean more,
        final int version) {
    if (version == 1) {
        encodeLength(frame.size() + 1, buffer);
        buffer.writeByte(more ? MORE_FLAG : FINAL_FLAG);
    } else { // version == 2
        encodeZMTP2FrameHeader(frame.size(), more ? MORE_FLAG : FINAL_FLAG, buffer);
    }
    if (frame.hasData()) {
        //      final ByteBuf source = frame.getDataBuffer();
        byte[] data = frame.getData();
        buffer.ensureWritable(data.length);

        buffer.writeBytes(data);
        //      source.getBytes(source.readerIndex(), buffer, source.readableBytes());
    }
}

From source file:com.spotify.netty4.handler.codec.zmtp.benchmarks.CustomReqRepBenchmark.java

License:Apache License

private static void writeAscii(final ZMTPWriter writer, final CharSequence s) {
    final ByteBuf frame = writer.frame(s.length(), true);
    if (s instanceof AsciiString) {
        ((AsciiString) s).write(frame);/*  w  ww.j  a  v a  2  s.  c o m*/
    } else {
        for (int i = 0; i < s.length(); i++) {
            frame.writeByte(s.charAt(i));
        }
    }
}

From source file:com.spotify.netty4.handler.codec.zmtp.benchmarks.CustomReqRepBenchmark.java

License:Apache License

private static void writePayload(final ZMTPWriter writer, final ByteBuffer payload) {
    final ByteBuf buf = writer.frame(payload.remaining(), false);
    if (payload.hasArray()) {
        buf.writeBytes(payload.array(), payload.arrayOffset() + payload.position(), payload.remaining());
    } else {/*from  ww  w .j ava2 s  . co m*/
        final int pos = payload.position();
        for (int i = 0; i < payload.remaining(); i++) {
            buf.writeByte(payload.get(pos + i));
        }
        payload.position(pos);
    }
}

From source file:com.spotify.netty4.handler.codec.zmtp.ProtocolViolationTests.java

License:Apache License

@Theory
public void protocolErrorsCauseException(@TestedOn(ints = { 16, 17, 27, 32, 48, 53 }) final int payloadSize)
        throws Exception {
    final Bootstrap b = new Bootstrap();
    b.group(new NioEventLoopGroup());
    b.channel(NioSocketChannel.class);
    b.handler(new ChannelInitializer<NioSocketChannel>() {
        @Override//from   w  w w  .  j  av a 2 s  .  c o  m
        protected void initChannel(final NioSocketChannel ch) throws Exception {
            ch.pipeline().addLast(new MockHandler());
        }
    });

    final Channel channel = b.connect(serverAddress).awaitUninterruptibly().channel();

    final ByteBuf payload = Unpooled.buffer(payloadSize);
    for (int i = 0; i < payloadSize; i++) {
        payload.writeByte(0);
    }
    channel.writeAndFlush(payload);

    mockHandler.active.get(5, SECONDS);
    mockHandler.exception.get(5, SECONDS);
    mockHandler.inactive.get(5, SECONDS);
    assertFalse(mockHandler.handshaked);
    assertFalse(mockHandler.read);
}

From source file:com.spotify.netty4.handler.codec.zmtp.ZMTP10WireFormat.java

License:Apache License

/**
 * Write a ZMTP/1.0 frame length./*from w w  w . j  a  va2 s .c  o  m*/
 *
 * @param out       Target buffer.
 * @param maxLength The maximum length of the field.
 * @param length    The length.
 */
static void writeLength(final ByteBuf out, final long maxLength, final long length) {
    if (maxLength < 255) {
        out.writeByte((byte) length);
    } else {
        out.writeByte(0xFF);
        out.writeLong(length);
    }
}