Example usage for io.netty.buffer ByteBuf writeByte

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

Introduction

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

Prototype

public abstract ByteBuf writeByte(int value);

Source Link

Document

Sets the specified byte at the current writerIndex and increases the writerIndex by 1 in this buffer.

Usage

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);
    response.writeBytes(wrongType);//from w ww  .j ava 2s . c o m
    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);
    response.writeBytes(intToBytes(integer));
    response.writeBytes(CRLFar);//w  w  w  .j a 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 getIntegerResponse(ByteBufAllocator alloc, long l) {
    ByteBuf response = alloc.buffer(25);
    response.writeByte(INTEGER_ID);
    response.writeBytes(longToBytes(l));
    response.writeBytes(CRLFar);/*from   w w w  . jav  a 2s  .  c  om*/
    return response;
}

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);
    ByteBuf tmp = alloc.buffer();/* ww  w. j  a  v a  2s.  co m*/
    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);
    ByteBuf tmp = alloc.buffer();/*  w ww . j a  v a 2 s.  c o m*/
    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);
    response.writeBytes(intToBytes(length));
    response.writeBytes(Coder.CRLFar);/*from w  w w . j a v a 2 s .c  om*/

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

    return response;
}

From source file:com.gemstone.gemfire.internal.redis.executor.sortedset.ZRangeByLexExecutor.java

License:Apache License

private final ByteBuf getCustomBulkStringArrayResponse(Collection<ByteArrayWrapper> items,
        ExecutionHandlerContext context) {
    Iterator<ByteArrayWrapper> it = items.iterator();
    ByteBuf response = context.getByteBufAllocator().buffer();
    response.writeByte(Coder.ARRAY_ID);
    response.writeBytes(Coder.intToBytes(items.size()));
    response.writeBytes(Coder.CRLFar);/*from ww  w.  java 2 s  .c  om*/

    while (it.hasNext()) {
        ByteArrayWrapper next = it.next();
        byte[] byteAr = next.toBytes();
        response.writeByte(Coder.BULK_STRING_ID);
        response.writeBytes(Coder.intToBytes(byteAr.length));
        response.writeBytes(Coder.CRLFar);
        response.writeBytes(byteAr);
        response.writeBytes(Coder.CRLFar);
    }

    return response;
}

From source file:com.gemstone.gemfire.internal.redis.executor.TimeExecutor.java

License:Apache License

@Override
public void executeCommand(Command command, ExecutionHandlerContext context) {
    long timeStamp = System.currentTimeMillis();
    long seconds = timeStamp / 1000;
    long microSeconds = (timeStamp - (seconds * 1000)) * 1000;
    byte[] secAr = Coder.longToBytes(seconds);
    byte[] micAr = Coder.longToBytes(microSeconds);

    ByteBuf response = context.getByteBufAllocator().buffer(50);
    response.writeByte(Coder.ARRAY_ID);
    response.writeByte(50); // #2
    response.writeBytes(Coder.CRLFar);/* ww w. ja  va 2  s. com*/
    response.writeByte(Coder.BULK_STRING_ID);
    response.writeBytes(Coder.intToBytes(secAr.length));
    response.writeBytes(Coder.CRLFar);
    response.writeBytes(secAr);
    response.writeBytes(Coder.CRLFar);
    response.writeByte(Coder.BULK_STRING_ID);
    response.writeBytes(Coder.intToBytes(micAr.length));
    response.writeBytes(Coder.CRLFar);
    response.writeBytes(micAr);
    response.writeBytes(Coder.CRLFar);
    command.setResponse(response);
}

From source file:com.gemstone.gemfire.internal.redis.executor.transactions.ExecExecutor.java

License:Apache License

private ByteBuf constructResponseExec(ExecutionHandlerContext context) {
    Queue<Command> cQ = context.getTransactionQueue();
    ByteBuf response = context.getByteBufAllocator().buffer();
    response.writeByte(Coder.ARRAY_ID);
    response.writeBytes(Coder.intToBytes(cQ.size()));
    response.writeBytes(Coder.CRLFar);/*from  w  w  w .  ja  v a2  s  . c o m*/

    for (Command c : cQ) {
        ByteBuf r = c.getResponse();
        response.writeBytes(r);
    }
    return response;
}

From source file:com.github.milenkovicm.kafka.CrcTest.java

License:Apache License

private ByteBuf getByteBuf(int size) {
    Random rand = new Random();
    ByteBuf buf = freeLaterBuffer(size);
    for (int i = 0; i < size; i++) {
        buf.writeByte(rand.nextInt());
    }//from ww  w  . j  a v  a  2 s  .co  m
    //        byte[] b = new byte[size];
    //        buf.getBytes(0,b);
    //        System.out.println(Arrays.toString(b));
    return buf;
}