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.github.milenkovicm.kafka.protocol.Convert.java

License:Apache License

public static void encodeByte(int integer, ByteBuf buf) {
    buf.writeByte(integer);
}

From source file:com.heelenyc.im.coder.MessageEncoder.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");

    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 .ja  va 2  s.c o m
    byte[] keyArray = null;
    Object value = null;
    for (Map.Entry<String, Object> param : msg.getHeader().getAttachment().entrySet()) {
        // write key
        key = param.getKey();
        keyArray = key.getBytes(Constans.ATTACHMENT_KEY_CHARACTSET);
        sendBuf.writeInt(keyArray.length);
        sendBuf.writeBytes(keyArray);
        // write value
        value = param.getValue();
        encoder.encode(value, sendBuf);
    }
    key = null;
    keyArray = null;
    value = null;
    if (msg.getBody() != null) {
        encoder.encode(msg.getBody(), sendBuf);
    } else
        sendBuf.writeInt(0);
    int totalSize = sendBuf.readableBytes() - Constans.MESSAGE_LENGTH_FIELD_OFFSET
            - Constans.MESSAGE_LENGTH_FIELD_LENGTH;
    // fix head length field
    msg.getHeader().setLength(totalSize);
    // ?
    sendBuf.setInt(Constans.MESSAGE_LENGTH_FIELD_OFFSET, totalSize);
    logger.debug("encode :" + msg);
}

From source file:com.heliosapm.ohcrs.core.AbstractDriverCodec.java

License:Apache License

/**
 * {@inheritDoc}//w w  w .j  a va 2 s. c  om
 * @see com.heliosapm.ohcrs.core.DriverCodec#write(byte, io.netty.buffer.ByteBuf)
 */
@Override
public int write(final Byte p, final ByteBuf b) throws SQLException {
    prefix(p, DBType.TINYINT, b);
    b.writeByte(p);
    return b.writerIndex();
}

From source file:com.heliosapm.ohcrs.core.AbstractDriverCodec.java

License:Apache License

/**
 * {@inheritDoc}//from   www  . java 2 s  .com
 * @see com.heliosapm.ohcrs.core.DriverCodec#write(byte, io.netty.buffer.ByteBuf)
 */
@Override
public int write(final byte p, final ByteBuf b) throws SQLException {
    prefix(P, DBType.TINYINT, b);
    b.writeByte(p);
    return b.writerIndex();
}

From source file:com.heliosapm.streams.metrics.aggregation.StreamedMetricAggregation.java

License:Apache License

/**
 * Returns this aggregation as a byte array
 * @return a byte array//from ww w .j  ava  2s.  c  o m
 */
public byte[] toByteArray() {
    final ByteBuf b = BufferManager.getInstance().buffer(size == -1 ? 128 : size);
    try {
        b.writeByte(sticky ? 1 : 0);
        b.writeByte(doubleType ? 1 : 0);
        b.writeLong(createTime);
        b.writeLong(period);
        b.writeByte(periodUnit.ordinal());
        values.position(0);
        b.writeBytes(values);
        b.writeByte(tags.size());
        BufferManager.writeUTF(metricName, b);
        for (Map.Entry<String, String> entry : tags.entrySet()) {
            BufferManager.writeUTF(entry.getKey(), b);
            BufferManager.writeUTF(entry.getValue(), b);
        }
        return ByteBufUtil.getBytes(b);
    } finally {
        try {
            b.release();
        } catch (Exception x) {
            /* No Op */}
    }
}

From source file:com.heliosapm.streams.metrics.StreamedMetric.java

License:Open Source License

/**
 * Returns a byte array containing the serialized streammetric
 * @return a byte array /*  w  w w .j a  v a  2s .c o m*/
 */
public byte[] toByteArray() {
    final ByteBuf buff = BufferManager.getInstance().directBuffer(byteSize);
    try {
        buff.writeByte(TYPE_CODE);
        writeByteArray(buff);
        return ByteBufUtil.getBytes(buff, 0, buff.readableBytes());

    } finally {
        try {
            buff.release();
        } catch (Exception x) {
            /* No Op */}
    }
}

From source file:com.heliosapm.streams.metrics.StreamedMetric.java

License:Open Source License

/**
 * Returns this streamed metric serialized into a byte buf
 * @return the byte buff/*w  ww  .j a  v  a  2s  .c  om*/
 */
public ByteBuf toByteBuff() {
    final ByteBuf buff = BufferManager.getInstance().directBuffer(byteSize);
    buff.writeByte(TYPE_CODE);
    writeByteArray(buff);
    return buff;
}

From source file:com.heliosapm.streams.metrics.StreamedMetric.java

License:Open Source License

/**
 * Writes this metric into the passed buffer
 * @param buf The buffer to write this metric into
 *///from   ww w.j av a 2 s.co m
public void intoByteBuf(final ByteBuf buf) {
    buf.writeByte(TYPE_CODE);
    writeByteArray(buf);
}

From source file:com.heliosapm.streams.metrics.StreamedMetric.java

License:Open Source License

/**
 * Returns a byte array containing the serialized streammetric
 * @param buff The buffer to write into/*from   w w  w.j ava2  s.  c o m*/
 */
void writeByteArray(final ByteBuf buff) {
    buff.writeByte(valueType == null ? 0 : valueType.ordinal() + 1);
    buff.writeLong(timestamp);
    BufferManager.writeUTF(metricName, buff);
    buff.writeByte(tags.size());
    for (Map.Entry<String, String> entry : tags.entrySet()) {
        BufferManager.writeUTF(entry.getKey(), buff);
        BufferManager.writeUTF(entry.getValue(), buff);
    }
}

From source file:com.heliosapm.streams.metrics.StreamedMetricValue.java

License:Apache License

/**
 * Writes a metric definition to the passed buffer
 * @param buff The buffer to write to//from   w  w w  .  ja va  2  s  .  c  om
 * @param valueType The value type
 * @param metricName The metric name
 * @param timestamp The metric timestamp
 * @param tags The metric tags
 * @return the number of bytes written
 */
public static int write(final ByteBuf buff, final ValueType valueType, final String metricName,
        final long timestamp, final Map<String, String> tags) {
    final int offset = buff.writerIndex();
    buff.writeByte(TYPE_CODE);
    buff.writeByte(valueType == null ? 0 : valueType.ordinal() + 1);
    buff.writeLong(timestamp);
    BufferManager.writeUTF(metricName, buff);
    buff.writeByte(tags.size());
    for (Map.Entry<String, String> entry : tags.entrySet()) {
        BufferManager.writeUTF(entry.getKey(), buff);
        BufferManager.writeUTF(entry.getValue(), buff);
    }
    return buff.writerIndex() - offset;
}