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: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  a v 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;/*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()) {
        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.core.network.serializers.SerializerLong.java

License:Minecraft Mod Public

@Override
public void write(ByteBuf data, Object o, SerializationContext context) {
    Long i = (Long) o;

    data.writeLong(i);
}

From source file:com.almuradev.guide.server.network.play.S00PageInformation.java

License:MIT License

@Override
public void toBytes(ByteBuf buf) {
    ByteBufUtils.writeUTF8String(buf, identifier);
    buf.writeInt(index);//w  ww  .  ja  v a2s .co m
    ByteBufUtils.writeUTF8String(buf, title);
    buf.writeLong(created.getTime());
    ByteBufUtils.writeUTF8String(buf, author);
    buf.writeLong(lastModified.getTime());
    ByteBufUtils.writeUTF8String(buf, lastContributor);
    ByteBufUtils.writeUTF8String(buf, contents);
}

From source file:com.book.netty5.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;/* w  ww  .j a  v a  2  s.  c  om*/
    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:com.builtbroken.atomic.network.packet.PacketBase.java

/**
 * Called to write data without manually defining the write
 *
 * @param object - object to write/*from www. ja  va 2  s .  c  o  m*/
 * @param buffer - location to write to
 */
protected void writeData(Object object, ByteBuf buffer) {
    if (object.getClass().isArray()) {
        for (int i = 0; i < Array.getLength(object); i++) {
            writeData(Array.get(object, i), buffer);
        }
    } else if (object instanceof Collection) {
        for (Object o : (Collection) object) {
            writeData(o, buffer);
        }
    } else if (object instanceof Byte) {
        buffer.writeByte((Byte) object);
    } else if (object instanceof Integer) {
        buffer.writeInt((Integer) object);
    } else if (object instanceof Short) {
        buffer.writeShort((Short) object);
    } else if (object instanceof Long) {
        buffer.writeLong((Long) object);
    } else if (object instanceof Float) {
        buffer.writeFloat((Float) object);
    } else if (object instanceof Double) {
        buffer.writeDouble((Double) object);
    } else if (object instanceof Boolean) {
        buffer.writeBoolean((Boolean) object);
    } else if (object instanceof String) {
        ByteBufUtils.writeUTF8String(buffer, (String) object);
    } else if (object instanceof NBTTagCompound) {
        ByteBufUtils.writeTag(buffer, (NBTTagCompound) object);
    } else if (object instanceof ItemStack) {
        ByteBufUtils.writeItemStack(buffer, (ItemStack) object);
    } else if (object instanceof FluidTank) {
        ByteBufUtils.writeTag(buffer, ((FluidTank) object).writeToNBT(new NBTTagCompound()));
    } else if (object instanceof Pos) {
        ((Pos) object).writeByteBuf(buffer);
    } else if (object instanceof IByteBufWriter) {
        ((IByteBufWriter) object).writeBytes(buffer);
    } else if (object instanceof Enum) {
        buffer.writeInt(((Enum) object).ordinal());
    } else {
        throw new IllegalArgumentException("PacketBase: Unsupported write data type " + object);
    }
}

From source file:com.cc.nettytest.proxy.encoder.CCLengthFieldPrepender.java

License:Apache License

@Override
public void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {

    int length = lengthIncludesLengthFieldLength ? msg.readableBytes() + lengthFieldLength
            : msg.readableBytes();/*from w  w  w.  java2  s.c o m*/
    switch (lengthFieldLength) {
    case 1:
        if (length >= 256) {
            throw new IllegalArgumentException("length does not fit into a byte: " + length);
        }
        out.writeByte((byte) length);
        break;
    case 2:
        if (length >= 65536) {
            throw new IllegalArgumentException("length does not fit into a short integer: " + length);
        }
        out.writeShort((short) length);
        break;
    case 3:
        if (length >= 16777216) {
            throw new IllegalArgumentException("length does not fit into a medium integer: " + length);
        }
        out.writeMedium(length);
        break;
    case 4:
        out.writeInt(ByteBufUtil.swapInt((int) length)); //SWAP FOR UIMANAGER
        break;
    case 8:
        out.writeLong(length);
        break;
    default:
        throw new Error("should not reach here");
    }

    out.writeBytes(msg, msg.readerIndex(), msg.readableBytes());
}

From source file:com.couchbase.client.core.endpoint.binary.BinaryHandler.java

License:Open Source License

/**
 * Encodes a {@link CounterRequest} into its lower level representation.
 *
 * Depending on if the {@link CounterRequest#delta} is positive or negative, either the incr or decr memcached
 * commands are utilized. The value is converted to its absolute variant to conform with the protocol.
 *
 * @return a ready {@link BinaryMemcacheRequest}.
 *//*from www . j  a v a2 s. c  o m*/
private static BinaryMemcacheRequest handleCounterRequest(final ChannelHandlerContext ctx,
        final CounterRequest msg) {
    ByteBuf extras = ctx.alloc().buffer();
    extras.writeLong(Math.abs(msg.delta()));
    extras.writeLong(msg.initial());
    extras.writeInt(msg.expiry());

    String key = msg.key();
    short keyLength = (short) key.length();
    byte extrasLength = (byte) extras.readableBytes();
    BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key, extras);
    request.setOpcode(msg.delta() < 0 ? OP_COUNTER_DECR : OP_COUNTER_INCR);
    request.setKeyLength(keyLength);
    request.setTotalBodyLength(keyLength + extrasLength);
    request.setExtrasLength(extrasLength);
    return request;
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java

License:Apache License

/**
 * Encodes a {@link CounterRequest} into its lower level representation.
 *
 * Depending on if the {@link CounterRequest#delta} is positive or negative, either the incr or decr memcached
 * commands are utilized. The value is converted to its absolute variant to conform with the protocol.
 *
 * @return a ready {@link BinaryMemcacheRequest}.
 *//*ww  w.  j  a v a 2 s. c o m*/
private static BinaryMemcacheRequest handleCounterRequest(final ChannelHandlerContext ctx,
        final CounterRequest msg) {
    ByteBuf extras = ctx.alloc().buffer();
    extras.writeLong(Math.abs(msg.delta()));
    extras.writeLong(msg.initial());
    extras.writeInt(msg.expiry());

    byte[] key = msg.keyBytes();
    short keyLength = (short) key.length;
    byte extrasLength = (byte) extras.readableBytes();
    BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key, extras);
    request.setOpcode(msg.delta() < 0 ? OP_COUNTER_DECR : OP_COUNTER_INCR);
    request.setKeyLength(keyLength);
    request.setTotalBodyLength(keyLength + extrasLength);
    request.setExtrasLength(extrasLength);
    return request;
}

From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandler.java

License:Apache License

private static BinaryMemcacheRequest handleObserveSeqnoRequest(final ChannelHandlerContext ctx,
        final ObserveSeqnoRequest msg) {
    ByteBuf content = ctx.alloc().buffer();
    content.writeLong(msg.vbucketUUID());

    BinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(EMPTY_BYTES, Unpooled.EMPTY_BUFFER,
            content);//from  w  w w .  jav  a2  s.  co m
    request.setOpcode(OP_OBSERVE_SEQ);
    request.setTotalBodyLength(content.readableBytes());
    return request;
}