Example usage for io.netty.buffer ByteBuf writeShort

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

Introduction

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

Prototype

public abstract ByteBuf writeShort(int value);

Source Link

Document

Sets the specified 16-bit short integer at the current writerIndex and increases the writerIndex by 2 in this buffer.

Usage

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

License:Apache License

/**
 * Creates the HELLO request to ask for certain supported features.
 *
 * @return the request to send over the wire
 *//*from w w w  .  ja va 2 s. c  o m*/
private FullBinaryMemcacheRequest helloRequest() {
    byte[] key = userAgent.getBytes(CharsetUtil.UTF_8);
    short keyLength = (short) key.length;

    ByteBuf wanted = Unpooled.buffer(features.size() * 2);
    for (ServerFeatures feature : features) {
        wanted.writeShort(feature.value());
    }

    LOGGER.debug("Requesting supported features: {}", features);
    FullBinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(key, Unpooled.EMPTY_BUFFER,
            wanted);
    request.setOpcode(HELLO_CMD);
    request.setKeyLength(keyLength);
    request.setTotalBodyLength(keyLength + wanted.readableBytes());
    return request;
}

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

License:Apache License

/**
 * Encodes a {@link ObserveRequest} into its lower level representation.
 *
 * @return a ready {@link BinaryMemcacheRequest}.
 *//*from ww w.  j a v a  2 s. c  o  m*/
private static BinaryMemcacheRequest handleObserveRequest(final ChannelHandlerContext ctx,
        final ObserveRequest msg) {
    String key = msg.key();
    short keyLength = (short) msg.keyBytes().length;
    ByteBuf content = ctx.alloc().buffer();
    content.writeShort(msg.partition());
    content.writeShort(keyLength);
    content.writeBytes(key.getBytes(CHARSET));

    BinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(EMPTY_BYTES, Unpooled.EMPTY_BUFFER,
            content);
    request.setOpcode(OP_OBSERVE);
    request.setTotalBodyLength(content.readableBytes());
    return request;
}

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

License:Apache License

private static BinaryMemcacheRequest handleSubdocumentRequest(ChannelHandlerContext ctx,
        BinarySubdocRequest msg) {/*from   w  ww.  j  a  v a 2s .  c om*/
    byte[] key = msg.keyBytes();
    short keyLength = (short) key.length;

    ByteBuf extras = ctx.alloc().buffer(3, 7); //extras can be 7 bytes if there is an expiry
    byte extrasLength = 3; //by default 2 bytes for pathLength + 1 byte for "command" flags
    extras.writeShort(msg.pathLength());

    long cas = 0L;

    if (msg instanceof BinarySubdocMutationRequest) {
        BinarySubdocMutationRequest mut = (BinarySubdocMutationRequest) msg;
        //for now only possible command flag is MKDIR_P (and it makes sense in mutations only)
        if (mut.createIntermediaryPath()) {
            extras.writeByte(0 | SUBDOC_BITMASK_MKDIR_P);
        } else {
            extras.writeByte(0);
        }
        if (mut.expiration() != 0L) {
            extrasLength = 7;
            extras.writeInt(mut.expiration());
        }

        cas = mut.cas();
    } else {
        extras.writeByte(0);
    }

    FullBinaryMemcacheRequest request = new DefaultFullBinaryMemcacheRequest(key, extras, msg.content());
    request.setOpcode(msg.opcode()).setKeyLength(keyLength).setExtrasLength(extrasLength)
            .setTotalBodyLength(keyLength + msg.content().readableBytes() + extrasLength).setCAS(cas);

    return request;
}

From source file:com.couchbase.client.core.message.kv.subdoc.multi.SubMultiLookupRequest.java

License:Apache License

private static ByteBuf encode(List<LookupCommand> commands) {
    CompositeByteBuf compositeBuf = Unpooled.compositeBuffer(commands.size()); //FIXME pooled allocator?
    for (LookupCommand command : commands) {
        byte[] pathBytes = command.path().getBytes(CharsetUtil.UTF_8);
        short pathLength = (short) pathBytes.length;

        ByteBuf commandBuf = Unpooled.buffer(4 + pathLength); //FIXME a way of using the pooled allocator?
        commandBuf.writeByte(command.opCode());
        commandBuf.writeByte(0); //no flags supported for lookup
        commandBuf.writeShort(pathLength);
        //no value length
        commandBuf.writeBytes(pathBytes);

        compositeBuf.addComponent(commandBuf);
        compositeBuf.writerIndex(compositeBuf.writerIndex() + commandBuf.readableBytes());
    }/*from  ww w .  j ava 2  s. com*/
    return compositeBuf;
}

From source file:com.couchbase.client.core.message.kv.subdoc.multi.SubMultiMutationRequest.java

License:Apache License

private static ByteBuf encode(List<MutationCommand> commands) {
    //FIXME a way of using the pooled allocator?
    CompositeByteBuf compositeBuf = Unpooled.compositeBuffer(commands.size());
    for (MutationCommand command : commands) {
        byte[] pathBytes = command.path().getBytes(CharsetUtil.UTF_8);
        short pathLength = (short) pathBytes.length;

        ByteBuf commandBuf = Unpooled.buffer(4 + pathLength + command.fragment().readableBytes());
        commandBuf.writeByte(command.opCode());
        if (command.createIntermediaryPath()) {
            commandBuf.writeByte(KeyValueHandler.SUBDOC_BITMASK_MKDIR_P); //0 | SUBDOC_BITMASK_MKDIR_P
        } else {/* w  w  w. j  a  v  a 2  s  .c om*/
            commandBuf.writeByte(0);
        }
        commandBuf.writeShort(pathLength);
        commandBuf.writeInt(command.fragment().readableBytes());
        commandBuf.writeBytes(pathBytes);

        //copy the fragment but don't move indexes (in case it is retained and reused)
        commandBuf.writeBytes(command.fragment(), command.fragment().readerIndex(),
                command.fragment().readableBytes());
        //eagerly release the fragment once it's been copied
        command.fragment().release();

        //add the command to the composite buffer
        compositeBuf.addComponent(commandBuf);
        compositeBuf.writerIndex(compositeBuf.writerIndex() + commandBuf.readableBytes());
    }
    return compositeBuf;
}

From source file:com.datastax.driver.core.CBUtil.java

License:Apache License

public static void writeString(String str, ByteBuf cb) {
    byte[] bytes = str.getBytes(CharsetUtil.UTF_8);
    cb.writeShort(bytes.length);
    cb.writeBytes(bytes);//from  www.  j a  va  2s. com
}

From source file:com.datastax.driver.core.CBUtil.java

License:Apache License

public static void writeBytes(byte[] bytes, ByteBuf cb) {
    cb.writeShort(bytes.length);
    cb.writeBytes(bytes);
}

From source file:com.datastax.driver.core.CBUtil.java

License:Apache License

public static void writeBytes(ByteBuffer bytes, ByteBuf cb) {
    cb.writeShort(bytes.remaining());
    cb.writeBytes(bytes.duplicate());
}

From source file:com.datastax.driver.core.CBUtil.java

License:Apache License

public static void writeBytesMap(Map<String, ByteBuffer> m, ByteBuf cb) {
    cb.writeShort(m.size());
    for (Map.Entry<String, ByteBuffer> entry : m.entrySet()) {
        writeString(entry.getKey(), cb);
        ByteBuffer value = entry.getValue();
        if (value == Statement.NULL_PAYLOAD_VALUE)
            value = null;/*from   www .  j  ava2 s  .c  o m*/
        writeValue(value, cb);
    }
}

From source file:com.datastax.driver.core.CBUtil.java

License:Apache License

public static void writeConsistencyLevel(ConsistencyLevel consistency, ByteBuf cb) {
    cb.writeShort(consistency.code);
}