Example usage for io.netty.buffer ByteBuf writeLong

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

Introduction

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

Prototype

public abstract ByteBuf writeLong(long value);

Source Link

Document

Sets the specified 64-bit long integer at the current writerIndex and increases the writerIndex by 8 in this buffer.

Usage

From source file:com.gmail.socraticphoenix.forge.randore.packet.RandoresSeedPacket.java

License:Open Source License

@Override
public void toBytes(ByteBuf buf) {
    buf.writeLong(this.getSeed());
    buf.writeInt(this.getOreCount());
}

From source file:com.hazelcast.simulator.protocol.core.ResponseCodec.java

License:Open Source License

public static void encodeByteBuf(Response response, ByteBuf buffer) {
    buffer.writeInt(HEADER_SIZE + response.size() * DATA_ENTRY_SIZE);
    buffer.writeInt(MAGIC_BYTES);/*from  w w  w.  ja va  2 s .  c  o  m*/

    buffer.writeLong(response.getMessageId());
    SimulatorAddressCodec.encodeByteBuf(response.getDestination(), buffer);

    for (Map.Entry<SimulatorAddress, ResponseType> entry : response.entrySet()) {
        SimulatorAddressCodec.encodeByteBuf(entry.getKey(), buffer);
        buffer.writeInt(entry.getValue().toInt());
    }
}

From source file:com.hazelcast.simulator.protocol.core.SimulatorMessageCodec.java

License:Open Source License

public static void encodeByteBuf(SimulatorMessage msg, ByteBuf buffer) {
    byte[] data = msg.getOperationData().getBytes(UTF_8);

    buffer.writeInt(HEADER_SIZE + data.length);
    buffer.writeInt(MAGIC_BYTES);//from w w  w  . ja v  a  2 s.  c om

    SimulatorAddressCodec.encodeByteBuf(msg.getDestination(), buffer);
    SimulatorAddressCodec.encodeByteBuf(msg.getSource(), buffer);

    buffer.writeLong(msg.getMessageId());
    buffer.writeInt(msg.getOperationType().toInt());

    buffer.writeBytes(data);
}

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  w w w  .  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()) {
        // 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}//from   ww  w  .j av a2  s . c o m
 * @see com.heliosapm.ohcrs.core.DriverCodec#write(long, io.netty.buffer.ByteBuf)
 */
@Override
public int write(final Long p, final ByteBuf b) throws SQLException {
    prefix(p, DBType.BIGINT, b);
    b.writeLong(p);
    return b.writerIndex();
}

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

License:Apache License

/**
 * {@inheritDoc}// ww w. j  a  v a 2 s .c om
 * @see com.heliosapm.ohcrs.core.DriverCodec#write(long, io.netty.buffer.ByteBuf)
 */
@Override
public int write(final long p, final ByteBuf b) throws SQLException {
    prefix(P, DBType.BIGINT, b);
    b.writeLong(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   w w  w. j  a  v  a 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
 * @param buff The buffer to write into/*from   w  ww .  j a v  a  2  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 .  j  a  v a 2s  . c  o  m
 * @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;
}

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

License:Apache License

/**
 * Writes a metric to the passed buffer/* w w w  .j  av  a2  s . c  o  m*/
 * @param buff The buffer to write to
 * @param valueType The value type
 * @param metricName The metric name
 * @param timestamp The metric timestamp
 * @param value The metric value
 * @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 long value, final Map<String, String> tags) {
    final int defSize = write(buff, valueType, metricName, timestamp, tags);
    buff.writeByte(1);
    buff.writeLong(value);
    return defSize + 9;
}