Example usage for io.netty.buffer ByteBuf writeBytes

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

Introduction

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

Prototype

public abstract ByteBuf writeBytes(ByteBuffer src);

Source Link

Document

Transfers the specified source buffer's data to this buffer starting at the current writerIndex until the source buffer's position reaches its limit, and increases the writerIndex by the number of the transferred bytes.

Usage

From source file:com.gemstone.gemfire.internal.redis.Coder.java

License:Apache License

public static final ByteBuf getBulkStringArrayResponse(ByteBufAllocator alloc, List<String> items) {
    Iterator<String> it = items.iterator();
    ByteBuf response = alloc.buffer();
    response.writeByte(ARRAY_ID);/*from w w w.  j  a v  a  2s . co m*/
    response.writeBytes(intToBytes(items.size()));
    response.writeBytes(CRLFar);
    while (it.hasNext()) {
        String next = it.next();
        response.writeByte(BULK_STRING_ID);
        response.writeBytes(intToBytes(next.length()));
        response.writeBytes(CRLFar);
        response.writeBytes(stringToBytes(next));
        response.writeBytes(CRLFar);
    }
    return response;
}

From source file:com.gemstone.gemfire.internal.redis.Coder.java

License:Apache License

public static final ByteBuf getBulkStringArrayResponse(ByteBufAllocator alloc,
        Collection<ByteArrayWrapper> items) {
    Iterator<ByteArrayWrapper> it = items.iterator();
    ByteBuf response = alloc.buffer();
    response.writeByte(ARRAY_ID);//  w  ww. j av a  2 s .c  om
    response.writeBytes(intToBytes(items.size()));
    response.writeBytes(CRLFar);
    while (it.hasNext()) {
        ByteArrayWrapper nextWrapper = it.next();
        if (nextWrapper != null) {
            response.writeByte(BULK_STRING_ID);
            response.writeBytes(intToBytes(nextWrapper.length()));
            response.writeBytes(CRLFar);
            response.writeBytes(nextWrapper.toBytes());
            response.writeBytes(CRLFar);
        } else
            response.writeBytes(getNilResponse(alloc));
    }

    return response;
}

From source file:com.gemstone.gemfire.internal.redis.Coder.java

License:Apache License

public static final ByteBuf getKeyValArrayResponse(ByteBufAllocator alloc,
        Collection<Entry<ByteArrayWrapper, ByteArrayWrapper>> items) {
    Iterator<Map.Entry<ByteArrayWrapper, ByteArrayWrapper>> it = items.iterator();
    ByteBuf response = alloc.buffer();//from w  ww  . j a  v a  2  s.co  m
    response.writeByte(ARRAY_ID);

    int size = 0;
    ByteBuf tmp = alloc.buffer();
    while (it.hasNext()) {
        Map.Entry<ByteArrayWrapper, ByteArrayWrapper> next = it.next();
        byte[] key;
        byte[] nextByteArray;
        try {
            key = next.getKey().toBytes();
            nextByteArray = next.getValue().toBytes();
        } catch (EntryDestroyedException e) {
            continue;
        }
        tmp.writeByte(BULK_STRING_ID); // Add key
        tmp.writeBytes(intToBytes(key.length));
        tmp.writeBytes(CRLFar);
        tmp.writeBytes(key);
        tmp.writeBytes(CRLFar);
        tmp.writeByte(BULK_STRING_ID); // Add value
        tmp.writeBytes(intToBytes(nextByteArray.length));
        tmp.writeBytes(CRLFar);
        tmp.writeBytes(nextByteArray);
        tmp.writeBytes(CRLFar);
        size++;
    }

    response.writeBytes(intToBytes(size * 2));
    response.writeBytes(CRLFar);
    response.writeBytes(tmp);

    tmp.release();

    return response;
}

From source file:com.gemstone.gemfire.internal.redis.Coder.java

License:Apache License

public static final ByteBuf getScanResponse(ByteBufAllocator alloc, List<?> items) {
    ByteBuf response = alloc.buffer();
    response.writeByte(ARRAY_ID);//from w  ww.java  2s  .c  o m
    response.writeBytes(intToBytes(2));
    response.writeBytes(CRLFar);
    response.writeByte(BULK_STRING_ID);
    byte[] cursor = stringToBytes((String) items.get(0));
    response.writeBytes(intToBytes(cursor.length));
    response.writeBytes(CRLFar);
    response.writeBytes(cursor);
    response.writeBytes(CRLFar);
    items = items.subList(1, items.size());
    Iterator<?> it = items.iterator();
    response.writeByte(ARRAY_ID);
    response.writeBytes(intToBytes(items.size()));
    response.writeBytes(CRLFar);

    while (it.hasNext()) {
        Object nextObject = it.next();
        if (nextObject instanceof String) {
            String next = (String) nextObject;
            response.writeByte(BULK_STRING_ID);
            response.writeBytes(intToBytes(next.length()));
            response.writeBytes(CRLFar);
            response.writeBytes(stringToBytes(next));
            response.writeBytes(CRLFar);
        } else if (nextObject instanceof ByteArrayWrapper) {
            byte[] next = ((ByteArrayWrapper) nextObject).toBytes();
            response.writeByte(BULK_STRING_ID);
            response.writeBytes(intToBytes(next.length));
            response.writeBytes(CRLFar);
            response.writeBytes(next);
            response.writeBytes(CRLFar);
        }
    }
    return response;
}

From source file:com.gemstone.gemfire.internal.redis.Coder.java

License:Apache License

public static final ByteBuf getSimpleStringResponse(ByteBufAllocator alloc, String string) {
    byte[] simpAr = stringToBytes(string);

    ByteBuf response = alloc.buffer(simpAr.length + 20);
    response.writeByte(SIMPLE_STRING_ID);
    response.writeBytes(simpAr);
    response.writeBytes(CRLFar);//from w ww  . ja v  a  2 s.  c o  m
    return response;
}

From source file:com.gemstone.gemfire.internal.redis.Coder.java

License:Apache License

public static final ByteBuf getErrorResponse(ByteBufAllocator alloc, String error) {
    byte[] errorAr = stringToBytes(error);
    ByteBuf response = alloc.buffer(errorAr.length + 25);
    response.writeByte(ERROR_ID);/* w  w w  .  j av  a2  s.  co m*/
    response.writeBytes(err);
    response.writeBytes(errorAr);
    response.writeBytes(CRLFar);
    return response;
}

From source file:com.gemstone.gemfire.internal.redis.Coder.java

License:Apache License

public static final ByteBuf getNoAuthResponse(ByteBufAllocator alloc, String error) {
    byte[] errorAr = stringToBytes(error);
    ByteBuf response = alloc.buffer(errorAr.length + 25);
    response.writeByte(ERROR_ID);//  w  ww  .  ja v  a2  s .com
    response.writeBytes(noAuth);
    response.writeBytes(errorAr);
    response.writeBytes(CRLFar);
    return response;
}

From source file:com.gemstone.gemfire.internal.redis.Coder.java

License:Apache License

public static final ByteBuf getWrongTypeResponse(ByteBufAllocator alloc, String error) {
    byte[] errorAr = stringToBytes(error);
    ByteBuf response = alloc.buffer(errorAr.length + 31);
    response.writeByte(ERROR_ID);//w ww. j ava  2  s.  c o m
    response.writeBytes(wrongType);
    response.writeBytes(errorAr);
    response.writeBytes(CRLFar);
    return response;
}

From source file:com.gemstone.gemfire.internal.redis.Coder.java

License:Apache License

public static final ByteBuf getIntegerResponse(ByteBufAllocator alloc, int integer) {
    ByteBuf response = alloc.buffer(15);
    response.writeByte(INTEGER_ID);/*w  w w.ja  v  a  2 s  .  co  m*/
    response.writeBytes(intToBytes(integer));
    response.writeBytes(CRLFar);
    return response;
}

From source file:com.gemstone.gemfire.internal.redis.Coder.java

License:Apache License

public static final ByteBuf getIntegerResponse(ByteBufAllocator alloc, long l) {
    ByteBuf response = alloc.buffer(25);
    response.writeByte(INTEGER_ID);/*from  w w w.  ja v  a  2s  .c  o m*/
    response.writeBytes(longToBytes(l));
    response.writeBytes(CRLFar);
    return response;
}