Example usage for io.netty.buffer ByteBuf readBytes

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

Introduction

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

Prototype

public abstract ByteBuf readBytes(ByteBuffer dst);

Source Link

Document

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

Usage

From source file:com.forgetutorials.lib.network.SubPacketTileEntityCustom.java

License:LGPL

@Override
public void readData(ByteBuf data) throws IOException {

    int lenght = data.readInt();
    this.custom = data.readBytes(lenght);
}

From source file:com.friz.game.network.codec.LoginDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
    int type = buf.readUnsignedByte();
    int size = buf.readUnsignedShort();

    int major = buf.readInt();
    int minor = buf.readInt();
    boolean dropped = buf.readBoolean();

    int rsaSize = buf.readUnsignedShort();
    byte[] rsa = new byte[rsaSize];
    buf.readBytes(rsa);

    ByteBuf rsaBuf = Unpooled.wrappedBuffer(
            new BigInteger(rsa).modPow(Constants.LOGIN_EXPONENT, Constants.LOGIN_MODULUS).toByteArray());
    int rsaMagic = rsaBuf.readUnsignedByte();

    int[] key = new int[4];
    for (int i = 0; i < key.length; i++)
        key[i] = rsaBuf.readInt();// w ww.j a  v a 2  s .c  o m

    int block = rsaBuf.readUnsignedByte();

    if (block == 1 || block == 3) {
        int code = rsaBuf.readUnsignedMedium();
        rsaBuf.readerIndex(rsaBuf.readerIndex() + 1);
    } else if (block == 0) {
        int trusted = rsaBuf.readInt();
    } else if (block == 2) {
        rsaBuf.readerIndex(rsaBuf.readerIndex() + 4);
    }

    String password = BufferUtils.getString(rsaBuf);

    long serverKey = rsaBuf.readLong();
    long clientKey = rsaBuf.readLong();

    byte[] xtea = new byte[buf.readableBytes()];
    buf.readBytes(xtea);
    ByteBuf xteaBuf = Unpooled.wrappedBuffer(new XTEA(xtea).decrypt(key).toByteArray());

    String username = "";
    boolean asString = xteaBuf.readBoolean();
    if (asString)
        username = BufferUtils.getString(xteaBuf);
    else
        username = BufferUtils.getBase37(xteaBuf);

    int display = xteaBuf.readUnsignedByte();
    int width = xteaBuf.readUnsignedShort();
    int height = xteaBuf.readUnsignedShort();

    int multisample = xteaBuf.readByte();

    byte[] uid = new byte[24];
    for (int i = 0; i < uid.length; i++)
        uid[i] = xteaBuf.readByte();

    String token = BufferUtils.getString(xteaBuf);

    int prefSize = xteaBuf.readUnsignedByte();
    int prefVersion = xteaBuf.readUnsignedByte();
    int aPref = xteaBuf.readUnsignedByte();
    int antiAliasing = xteaBuf.readUnsignedByte();
    int aPref1 = xteaBuf.readUnsignedByte();
    int bloom = xteaBuf.readUnsignedByte();
    int brightness = xteaBuf.readUnsignedByte();
    int buildArea = xteaBuf.readUnsignedByte();
    int aPref2 = xteaBuf.readUnsignedByte();
    int flickeringEffects = xteaBuf.readUnsignedByte();
    int fog = xteaBuf.readUnsignedByte();
    int groundBlending = xteaBuf.readUnsignedByte();
    int groundDecoration = xteaBuf.readUnsignedByte();
    int idleAnimations = xteaBuf.readUnsignedByte();
    int lighting = xteaBuf.readUnsignedByte();
    int sceneryShadows = xteaBuf.readUnsignedByte();
    int aPref3 = xteaBuf.readUnsignedByte();
    int nullPref = xteaBuf.readUnsignedByte();
    int orthoMode = xteaBuf.readUnsignedByte();
    int particles = xteaBuf.readUnsignedByte();
    int removeRoofs = xteaBuf.readUnsignedByte();
    int maxScreenSize = xteaBuf.readUnsignedByte();
    int skyboxes = xteaBuf.readUnsignedByte();
    int mobShadows = xteaBuf.readUnsignedByte();
    int textures = xteaBuf.readUnsignedByte();
    int desiredToolkit = xteaBuf.readUnsignedByte();
    int nullPref1 = xteaBuf.readUnsignedByte();
    int water = xteaBuf.readUnsignedByte();
    int screenSize = xteaBuf.readUnsignedByte();
    int customCursors = xteaBuf.readUnsignedByte();
    int graphics = xteaBuf.readUnsignedByte();
    int cpu = xteaBuf.readUnsignedByte();
    int aPref4 = xteaBuf.readUnsignedByte();
    int safeMode = xteaBuf.readUnsignedByte();
    int aPref5 = xteaBuf.readUnsignedByte();
    int aPref6 = xteaBuf.readUnsignedByte();
    int aPref7 = xteaBuf.readUnsignedByte();
    int soundEffectsVolume = xteaBuf.readUnsignedByte();
    int areaSoundsVolume = xteaBuf.readUnsignedByte();
    int voiceOverVolume = xteaBuf.readUnsignedByte();
    int musicVolume = xteaBuf.readUnsignedByte();
    int themeMusicVolume = xteaBuf.readUnsignedByte();
    int steroSound = xteaBuf.readUnsignedByte();

    int infoVersion = xteaBuf.readUnsignedByte();
    int osType = xteaBuf.readUnsignedByte();
    boolean arch64 = xteaBuf.readBoolean();
    int versionType = xteaBuf.readUnsignedByte();
    int vendorType = xteaBuf.readUnsignedByte();
    int jMajor = xteaBuf.readUnsignedByte();
    int jMinor = xteaBuf.readUnsignedByte();
    int jPatch = xteaBuf.readUnsignedByte();
    boolean falseBool = xteaBuf.readBoolean();
    int heapSize = xteaBuf.readUnsignedShort();
    int pocessorCount = xteaBuf.readUnsignedByte();
    int cpuPhyscialMemory = xteaBuf.readUnsignedMedium();
    int cpuClock = xteaBuf.readUnsignedShort();
    String gpuName = BufferUtils.getJagString(xteaBuf);
    String aString = BufferUtils.getJagString(xteaBuf);
    String dxVersion = BufferUtils.getJagString(xteaBuf);
    String aString1 = BufferUtils.getJagString(xteaBuf);
    int gpuDriverMonth = xteaBuf.readUnsignedByte();
    int gpuDriverYear = xteaBuf.readUnsignedShort();
    String cpuType = BufferUtils.getJagString(xteaBuf);
    String cpuName = BufferUtils.getJagString(xteaBuf);
    int cpuThreads = xteaBuf.readUnsignedByte();
    int anInt = xteaBuf.readUnsignedByte();
    int anInt1 = xteaBuf.readInt();
    int anInt2 = xteaBuf.readInt();
    int anInt3 = xteaBuf.readInt();
    int anInt4 = xteaBuf.readInt();
    String aString2 = BufferUtils.getString(xteaBuf);

    int anInt5 = xteaBuf.readInt();
    int anInt6 = xteaBuf.readInt();
    int anInt7 = xteaBuf.readInt();
    String aString3 = BufferUtils.getString(xteaBuf);

    boolean hasAdditional = xteaBuf.readBoolean();
    String additionalInfo = "";
    if (hasAdditional)
        additionalInfo = BufferUtils.getString(xteaBuf);

    int anInt8 = xteaBuf.readUnsignedByte();
    int anInt9 = xteaBuf.readUnsignedByte();
    int anInt10 = xteaBuf.readUnsignedByte();
    int anInt11 = xteaBuf.readInt();
    String aString4 = BufferUtils.getString(xteaBuf);

    boolean newWorld = xteaBuf.readBoolean();
    int lobbyId = xteaBuf.readUnsignedShort();

    int[] checksums = new int[(xteaBuf.readableBytes() / 4) + 1];
    for (int i = 0; i < checksums.length; i++) {
        if (i == 32)
            checksums[i] = -1;
        else
            checksums[i] = xteaBuf.readInt();
    }
}

From source file:com.friz.lobby.network.codec.LobbyInitDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
    if (!buf.isReadable())
        return;//ww w. j  a  v a 2 s. c o m

    out.add(new LobbyInitRequestEvent(buf.readUnsignedByte()));

    if (buf.isReadable())
        out.add(new RecycleEvent(buf.readBytes(buf.readableBytes())));
}

From source file:com.friz.lobby.network.codec.LoginDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
    if (!buf.isReadable())
        return;/*from  w  w  w .  j  ava2 s  .  c  om*/

    int type = buf.readUnsignedByte();
    int size = buf.readUnsignedShort();

    if (!buf.isReadable(size))
        return;

    int major = buf.readInt();
    int minor = buf.readInt();

    int rsaSize = buf.readUnsignedShort();
    byte[] rsa = new byte[rsaSize];
    buf.readBytes(rsa);

    ByteBuf rsaBuf = Unpooled.wrappedBuffer(
            new BigInteger(rsa).modPow(Constants.LOGIN_EXPONENT, Constants.LOGIN_MODULUS).toByteArray());
    int rsaMagic = rsaBuf.readUnsignedByte();

    int[] key = new int[4];
    for (int i = 0; i < key.length; i++)
        key[i] = rsaBuf.readInt();

    int block = rsaBuf.readUnsignedByte();

    if (block == 1 || block == 3) {
        int code = rsaBuf.readUnsignedMedium();
        rsaBuf.readerIndex(rsaBuf.readerIndex() + 1);
        System.out.println(new GoogleAuthenticator().authorize("OE2ZSYF6T7N2R5CG", code));
    } else if (block == 0) {
        int trusted = rsaBuf.readInt();
    } else if (block == 2) {
        rsaBuf.readerIndex(rsaBuf.readerIndex() + 4);
    }

    String password = BufferUtils.getString(rsaBuf);

    long serverKey = rsaBuf.readLong();
    long clientKey = rsaBuf.readLong();

    byte[] xtea = new byte[buf.readableBytes()];
    buf.readBytes(xtea);
    ByteBuf xteaBuf = Unpooled.wrappedBuffer(new XTEA(xtea).decrypt(key).toByteArray());

    String username = "";
    boolean asString = xteaBuf.readBoolean();
    if (asString)
        username = BufferUtils.getString(xteaBuf);
    else
        username = BufferUtils.getBase37(xteaBuf);

    int game = xteaBuf.readUnsignedByte();
    int lang = xteaBuf.readUnsignedByte();
    int display = xteaBuf.readUnsignedByte();
    int width = xteaBuf.readUnsignedShort();
    int height = xteaBuf.readUnsignedShort();

    int multisample = xteaBuf.readByte();

    byte[] uid = new byte[24];
    for (int i = 0; i < uid.length; i++)
        uid[i] = xteaBuf.readByte();

    String token = BufferUtils.getString(xteaBuf);

    int prefSize = xteaBuf.readUnsignedByte();
    int prefVersion = xteaBuf.readUnsignedByte();
    int aPref = xteaBuf.readUnsignedByte();
    int antiAliasing = xteaBuf.readUnsignedByte();
    int aPref1 = xteaBuf.readUnsignedByte();
    int bloom = xteaBuf.readUnsignedByte();
    int brightness = xteaBuf.readUnsignedByte();
    int buildArea = xteaBuf.readUnsignedByte();
    int aPref2 = xteaBuf.readUnsignedByte();
    int flickeringEffects = xteaBuf.readUnsignedByte();
    int fog = xteaBuf.readUnsignedByte();
    int groundBlending = xteaBuf.readUnsignedByte();
    int groundDecoration = xteaBuf.readUnsignedByte();
    int idleAnimations = xteaBuf.readUnsignedByte();
    int lighting = xteaBuf.readUnsignedByte();
    int sceneryShadows = xteaBuf.readUnsignedByte();
    int aPref3 = xteaBuf.readUnsignedByte();
    int nullPref = xteaBuf.readUnsignedByte();
    int orthoMode = xteaBuf.readUnsignedByte();
    int particles = xteaBuf.readUnsignedByte();
    int removeRoofs = xteaBuf.readUnsignedByte();
    int maxScreenSize = xteaBuf.readUnsignedByte();
    int skyboxes = xteaBuf.readUnsignedByte();
    int mobShadows = xteaBuf.readUnsignedByte();
    int textures = xteaBuf.readUnsignedByte();
    int desiredToolkit = xteaBuf.readUnsignedByte();
    int nullPref1 = xteaBuf.readUnsignedByte();
    int water = xteaBuf.readUnsignedByte();
    int screenSize = xteaBuf.readUnsignedByte();
    int customCursors = xteaBuf.readUnsignedByte();
    int graphics = xteaBuf.readUnsignedByte();
    int cpu = xteaBuf.readUnsignedByte();
    int aPref4 = xteaBuf.readUnsignedByte();
    int safeMode = xteaBuf.readUnsignedByte();
    int aPref5 = xteaBuf.readUnsignedByte();
    int aPref6 = xteaBuf.readUnsignedByte();
    int aPref7 = xteaBuf.readUnsignedByte();
    int soundEffectsVolume = xteaBuf.readUnsignedByte();
    int areaSoundsVolume = xteaBuf.readUnsignedByte();
    int voiceOverVolume = xteaBuf.readUnsignedByte();
    int musicVolume = xteaBuf.readUnsignedByte();
    int themeMusicVolume = xteaBuf.readUnsignedByte();
    int steroSound = xteaBuf.readUnsignedByte();

    int infoVersion = xteaBuf.readUnsignedByte();
    int osType = xteaBuf.readUnsignedByte();
    boolean arch64 = xteaBuf.readBoolean();
    int versionType = xteaBuf.readUnsignedByte();
    int vendorType = xteaBuf.readUnsignedByte();
    int jMajor = xteaBuf.readUnsignedByte();
    int jMinor = xteaBuf.readUnsignedByte();
    int jPatch = xteaBuf.readUnsignedByte();
    boolean falseBool = xteaBuf.readBoolean();
    int heapSize = xteaBuf.readUnsignedShort();
    int pocessorCount = xteaBuf.readUnsignedByte();
    int cpuPhyscialMemory = xteaBuf.readUnsignedMedium();
    int cpuClock = xteaBuf.readUnsignedShort();
    String gpuName = BufferUtils.getJagString(xteaBuf);
    String aString = BufferUtils.getJagString(xteaBuf);
    String dxVersion = BufferUtils.getJagString(xteaBuf);
    String aString1 = BufferUtils.getJagString(xteaBuf);
    int gpuDriverMonth = xteaBuf.readUnsignedByte();
    int gpuDriverYear = xteaBuf.readUnsignedShort();
    String cpuType = BufferUtils.getJagString(xteaBuf);
    String cpuName = BufferUtils.getJagString(xteaBuf);
    int cpuThreads = xteaBuf.readUnsignedByte();
    int anInt = xteaBuf.readUnsignedByte();
    int anInt1 = xteaBuf.readInt();
    int anInt2 = xteaBuf.readInt();
    int anInt3 = xteaBuf.readInt();
    int anInt4 = xteaBuf.readInt();
    String aString2 = BufferUtils.getJagString(xteaBuf);

    int anInt5 = xteaBuf.readInt();
    String aString3 = BufferUtils.getString(xteaBuf);
    int affiliate = xteaBuf.readInt();
    int anInt6 = xteaBuf.readInt();
    String aString4 = BufferUtils.getString(xteaBuf);
    int anInt7 = xteaBuf.readUnsignedByte();

    int[] checksums = new int[(xteaBuf.readableBytes() / 4) + 1];
    for (int i = 0; i < checksums.length; i++) {
        if (i == 32)
            checksums[i] = -1;
        else
            checksums[i] = xteaBuf.readInt();
    }

    final List<Module> modules = new ArrayList<>();
    modules.add(new ClientVersionModule(major, minor));
    modules.add(new ClientTypeModule(game, lang, display, width, height));
    out.add(new LoginRequestEvent(modules));
}

From source file:com.friz.lobby.network.codec.SocialDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buf, List<Object> out) throws Exception {
    if (!buf.isReadable())
        return;//from w w  w .  j a v a  2  s.c o m

    int type = buf.readUnsignedByte();
    int size = buf.readUnsignedShort();

    if (!buf.isReadable(size))
        return;

    byte[] xtea = new byte[size];
    buf.readBytes(xtea);
    ByteBuf xteaBuf = Unpooled.wrappedBuffer(new XTEA(xtea).decrypt(key).toByteArray());

    int game = xteaBuf.readUnsignedByte();
    int lang = xteaBuf.readUnsignedByte();
    int display = xteaBuf.readUnsignedByte();
    int width = xteaBuf.readUnsignedShort();
    int height = xteaBuf.readUnsignedShort();

    int multisample = xteaBuf.readByte();

    byte[] uid = new byte[24];
    for (int i = 0; i < uid.length; i++)
        uid[i] = xteaBuf.readByte();

    String gameToken = BufferUtils.getString(xteaBuf);

    int prefSize = xteaBuf.readUnsignedByte();
    int prefVersion = xteaBuf.readUnsignedByte();
    int aPref = xteaBuf.readUnsignedByte();
    int antiAliasing = xteaBuf.readUnsignedByte();
    int aPref1 = xteaBuf.readUnsignedByte();
    int bloom = xteaBuf.readUnsignedByte();
    int brightness = xteaBuf.readUnsignedByte();
    int buildArea = xteaBuf.readUnsignedByte();
    int aPref2 = xteaBuf.readUnsignedByte();
    int flickeringEffects = xteaBuf.readUnsignedByte();
    int fog = xteaBuf.readUnsignedByte();
    int groundBlending = xteaBuf.readUnsignedByte();
    int groundDecoration = xteaBuf.readUnsignedByte();
    int idleAnimations = xteaBuf.readUnsignedByte();
    int lighting = xteaBuf.readUnsignedByte();
    int sceneryShadows = xteaBuf.readUnsignedByte();
    int aPref3 = xteaBuf.readUnsignedByte();
    int nullPref = xteaBuf.readUnsignedByte();
    int orthoMode = xteaBuf.readUnsignedByte();
    int particles = xteaBuf.readUnsignedByte();
    int removeRoofs = xteaBuf.readUnsignedByte();
    int maxScreenSize = xteaBuf.readUnsignedByte();
    int skyboxes = xteaBuf.readUnsignedByte();
    int mobShadows = xteaBuf.readUnsignedByte();
    int textures = xteaBuf.readUnsignedByte();
    int desiredToolkit = xteaBuf.readUnsignedByte();
    int nullPref1 = xteaBuf.readUnsignedByte();
    int water = xteaBuf.readUnsignedByte();
    int screenSize = xteaBuf.readUnsignedByte();
    int customCursors = xteaBuf.readUnsignedByte();
    int graphics = xteaBuf.readUnsignedByte();
    int cpu = xteaBuf.readUnsignedByte();
    int aPref4 = xteaBuf.readUnsignedByte();
    int safeMode = xteaBuf.readUnsignedByte();
    int aPref5 = xteaBuf.readUnsignedByte();
    int aPref6 = xteaBuf.readUnsignedByte();
    int aPref7 = xteaBuf.readUnsignedByte();
    int soundEffectsVolume = xteaBuf.readUnsignedByte();
    int areaSoundsVolume = xteaBuf.readUnsignedByte();
    int voiceOverVolume = xteaBuf.readUnsignedByte();
    int musicVolume = xteaBuf.readUnsignedByte();
    int themeMusicVolume = xteaBuf.readUnsignedByte();
    int steroSound = xteaBuf.readUnsignedByte();
    xteaBuf.readMedium();
    xteaBuf.readMedium();

    int infoVersion = xteaBuf.readUnsignedByte();
    int osType = xteaBuf.readUnsignedByte();
    boolean arch64 = xteaBuf.readBoolean();
    int versionType = xteaBuf.readUnsignedByte();
    int vendorType = xteaBuf.readUnsignedByte();
    int jMajor = xteaBuf.readUnsignedByte();
    int jMinor = xteaBuf.readUnsignedByte();
    int jPatch = xteaBuf.readUnsignedByte();
    boolean falseBool = xteaBuf.readBoolean();
    int heapSize = xteaBuf.readUnsignedShort();
    int pocessorCount = xteaBuf.readUnsignedByte();
    int cpuPhyscialMemory = xteaBuf.readUnsignedMedium();
    int cpuClock = xteaBuf.readUnsignedShort();
    String gpuName = BufferUtils.getJagString(xteaBuf);
    String aString = BufferUtils.getJagString(xteaBuf);
    String dxVersion = BufferUtils.getJagString(xteaBuf);
    String aString1 = BufferUtils.getJagString(xteaBuf);
    int gpuDriverMonth = xteaBuf.readUnsignedByte();
    int gpuDriverYear = xteaBuf.readUnsignedShort();
    String cpuType = BufferUtils.getJagString(xteaBuf);
    String cpuName = BufferUtils.getJagString(xteaBuf);
    int cpuThreads = xteaBuf.readUnsignedByte();
    int anInt = xteaBuf.readUnsignedByte();
    int anInt1 = xteaBuf.readInt();
    int anInt2 = xteaBuf.readInt();
    int anInt3 = xteaBuf.readInt();
    int anInt4 = xteaBuf.readInt();
    String aString2 = BufferUtils.getJagString(xteaBuf);

    int anInt5 = xteaBuf.readInt();
    String seed = BufferUtils.getString(xteaBuf);
    int affiliate = xteaBuf.readInt();
    int anInt6 = xteaBuf.readInt();
    String updateToken = BufferUtils.getString(xteaBuf);
    int anInt7 = xteaBuf.readUnsignedByte();

    int[] checksums = new int[(xteaBuf.readableBytes() / 4) + 1];
    for (int i = 0; i < checksums.length; i++) {
        if (i == 32)
            checksums[i] = -1;
        else
            checksums[i] = xteaBuf.readInt();
    }

    System.out.println(seed);
}

From source file:com.friz.owari.network.codec.ExchangeDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (!in.isReadable())
        return;//from  w w  w .  j  a  va  2  s. c  o  m

    int len = in.readInt();
    byte[] enc = new byte[len];
    in.readBytes(enc);

    out.add(new ExchangeRecieveEvent(enc));
}

From source file:com.friz.owari.network.codec.PatchDecoder.java

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (!in.isReadable())
        return;/*from  w w w . j a va  2 s  .  c om*/

    int length = in.readUnsignedShort();
    byte[] path = new byte[length];
    in.readBytes(path);

    int size = in.readInt();
    byte[] bytes = new byte[size];
    in.readBytes(bytes);

    out.add(new PatchEvent(new String(path), bytes));
}

From source file:com.friz.update.network.codec.UpdateEncoder.java

License:Open Source License

@Override
protected void encode(ChannelHandlerContext ctx, FileResponseEvent msg, ByteBuf buffer) throws Exception {
    ByteBuf container = msg.getContainer();
    int type = msg.getType();
    int file = msg.getFile();

    if (!msg.isPriority())
        file |= 0x80000000;/*from www  .  j  a  v a2 s .  c o m*/

    buffer.writeByte(type);
    buffer.writeInt(file);

    int bytes = container.readableBytes();
    if (bytes > BYTES_AFTER_HEADER)
        bytes = BYTES_AFTER_HEADER;

    buffer.writeBytes(container.readBytes(bytes));

    for (;;) {
        bytes = container.readableBytes();
        if (bytes == 0)
            break;
        else if (bytes > BYTES_AFTER_HEADER)
            bytes = BYTES_AFTER_HEADER;

        buffer.writeByte(type);
        buffer.writeInt(file);
        buffer.writeBytes(container.readBytes(bytes));
    }
}

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

License:Apache License

/**
 * Helper method to parse a bulk string when one is seen
 * /*ww  w.ja va 2 s  .  c o m*/
 * @param buffer Buffer to read from
 * @return byte[] representation of the Bulk String read
 * @throws RedisCommandParserException Thrown when there is illegal syntax
 */
private byte[] parseBulkString(ByteBuf buffer) throws RedisCommandParserException {
    int bulkStringLength = parseCurrentNumber(buffer);
    if (bulkStringLength == Integer.MIN_VALUE)
        return null;
    if (bulkStringLength > MAX_BULK_STRING_LENGTH)
        throw new RedisCommandParserException(
                "invalid bulk length, cannot exceed max length of " + MAX_BULK_STRING_LENGTH);
    if (!parseRN(buffer))
        return null;

    if (!buffer.isReadable(bulkStringLength))
        return null;
    byte[] bulkString = new byte[bulkStringLength];
    buffer.readBytes(bulkString);

    if (!parseRN(buffer))
        return null;

    return bulkString;
}

From source file:com.ghrum.common.protocol.ProtocolReplayingDecoder.java

License:Apache License

/**
 * {@inheritDoc}/*from   w  ww .  j  a  va  2  s.  c o m*/
 */
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    switch (state) {
    case READ_ID:
        id = in.readUnsignedByte();
        checkpoint(DecoderState.READ_LENGTH);
        break;
    case READ_LENGTH:
        length = in.readUnsignedShort();
        checkpoint(DecoderState.READ_CONTENT);
        break;
    case READ_CONTENT:
        out.add(service.decode(id, in.readBytes(length)));
        checkpoint(DecoderState.READ_ID);
    }
}