Example usage for io.netty.buffer ByteBufAllocator buffer

List of usage examples for io.netty.buffer ByteBufAllocator buffer

Introduction

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

Prototype

ByteBuf buffer();

Source Link

Document

Allocate a ByteBuf .

Usage

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

License:Apache License

public static final ByteBuf getBulkStringResponse(ByteBufAllocator alloc, double value) {
    ByteBuf response = alloc.buffer();
    byte[] doub = doubleToBytes(value);
    response.writeByte(BULK_STRING_ID);//from w  w w .java2s  .c  o  m
    response.writeBytes(intToBytes(doub.length));
    response.writeBytes(CRLFar);
    response.writeBytes(doub);
    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, List<String> items) {
    Iterator<String> it = items.iterator();
    ByteBuf response = alloc.buffer();
    response.writeByte(ARRAY_ID);//from   w  w w.j ava  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);//from w ww . java 2s .c o m
    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();
    response.writeByte(ARRAY_ID);//  w w w  . ja v a  2s .c  om

    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);// w  w w. jav a  2 s.  c  om
    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 getEmptyArrayResponse(ByteBufAllocator alloc) {
    ByteBuf buf = alloc.buffer().writeBytes(bEMPTY_ARRAY);
    return buf;//from  w  w w.ja  v a2s .c  om
}

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

License:Apache License

public static final ByteBuf getNilResponse(ByteBufAllocator alloc) {
    ByteBuf buf = alloc.buffer().writeBytes(bNIL);
    return buf;//from   w ww  . j  a v  a 2 s  .com
}

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

License:Apache License

public static ByteBuf getBulkStringArrayResponseOfValues(ByteBufAllocator alloc, Collection<?> items) {
    Iterator<?> it = items.iterator();
    ByteBuf response = alloc.buffer();
    response.writeByte(Coder.ARRAY_ID);/*from w w w .  j a  v  a2  s  .  c  om*/
    ByteBuf tmp = alloc.buffer();
    int size = 0;
    while (it.hasNext()) {
        Object next = it.next();
        ByteArrayWrapper nextWrapper = null;
        if (next instanceof Entry) {
            try {
                nextWrapper = (ByteArrayWrapper) ((Entry<?, ?>) next).getValue();
            } catch (EntryDestroyedException e) {
                continue;
            }
        } else if (next instanceof Struct) {
            nextWrapper = (ByteArrayWrapper) ((Struct) next).getFieldValues()[1];
        }
        if (nextWrapper != null) {
            tmp.writeByte(Coder.BULK_STRING_ID);
            tmp.writeBytes(intToBytes(nextWrapper.length()));
            tmp.writeBytes(Coder.CRLFar);
            tmp.writeBytes(nextWrapper.toBytes());
            tmp.writeBytes(Coder.CRLFar);
        } else {
            tmp.writeBytes(Coder.bNIL);
        }
        size++;
    }

    response.writeBytes(intToBytes(size));
    response.writeBytes(Coder.CRLFar);
    response.writeBytes(tmp);

    tmp.release();

    return response;
}

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

License:Apache License

public static ByteBuf zRangeResponse(ByteBufAllocator alloc, Collection<?> list, boolean withScores) {
    if (list.isEmpty())
        return Coder.getEmptyArrayResponse(alloc);

    ByteBuf buffer = alloc.buffer();
    buffer.writeByte(Coder.ARRAY_ID);/*  ww  w  .ja v  a 2 s  .co m*/
    ByteBuf tmp = alloc.buffer();
    int size = 0;

    for (Object entry : list) {
        ByteArrayWrapper key;
        DoubleWrapper score;
        if (entry instanceof Entry) {
            try {
                key = (ByteArrayWrapper) ((Entry<?, ?>) entry).getKey();
                score = (DoubleWrapper) ((Entry<?, ?>) entry).getValue();
            } catch (EntryDestroyedException e) {
                continue;
            }
        } else {
            Object[] fieldVals = ((Struct) entry).getFieldValues();
            key = (ByteArrayWrapper) fieldVals[0];
            score = (DoubleWrapper) fieldVals[1];
        }
        byte[] byteAr = key.toBytes();
        tmp.writeByte(Coder.BULK_STRING_ID);
        tmp.writeBytes(intToBytes(byteAr.length));
        tmp.writeBytes(Coder.CRLFar);
        tmp.writeBytes(byteAr);
        tmp.writeBytes(Coder.CRLFar);
        size++;
        if (withScores) {
            String scoreString = score.toString();
            byte[] scoreAr = stringToBytes(scoreString);
            tmp.writeByte(Coder.BULK_STRING_ID);
            tmp.writeBytes(intToBytes(scoreString.length()));
            tmp.writeBytes(Coder.CRLFar);
            tmp.writeBytes(scoreAr);
            tmp.writeBytes(Coder.CRLFar);
            size++;
        }
    }

    buffer.writeBytes(intToBytes(size));
    buffer.writeBytes(Coder.CRLFar);
    buffer.writeBytes(tmp);

    tmp.release();

    return buffer;
}

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

License:Apache License

public static ByteBuf getArrayOfNils(ByteBufAllocator alloc, int length) {
    ByteBuf response = alloc.buffer();
    response.writeByte(Coder.ARRAY_ID);/*from ww w . j  a  v  a2s .  c om*/
    response.writeBytes(intToBytes(length));
    response.writeBytes(Coder.CRLFar);

    for (int i = 0; i < length; i++)
        response.writeBytes(bNIL);

    return response;
}