Example usage for io.netty.buffer ByteBuf indexOf

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

Introduction

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

Prototype

public abstract int indexOf(int fromIndex, int toIndex, byte value);

Source Link

Document

Locates the first occurrence of the specified value in this buffer.

Usage

From source file:com.github.lburgazzoli.quickfixj.transport.netty.codec.NettyMessageDecoder.java

License:Apache License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    if (m_msgLength == -1) {
        if (in.readableBytes() >= FIXCodecHelper.MSG_MIN_BYTES) {
            int ridx = in.readerIndex();
            int bssohidx = in.indexOf(ridx, ridx + 12, FIXCodecHelper.BYTE_SOH);
            int blsohidx = in.indexOf(ridx + 12, ridx + 20, FIXCodecHelper.BYTE_SOH);

            // check the existence of:
            // - BeginString 8=
            // - BodyLength  9=
            if (in.getByte(ridx) == FIXCodecHelper.BYTE_BEGIN_STRING
                    && in.getByte(ridx + 1) == FIXCodecHelper.BYTE_EQUALS
                    && in.getByte(bssohidx + 1) == FIXCodecHelper.BYTE_BODY_LENGTH
                    && in.getByte(bssohidx + 2) == FIXCodecHelper.BYTE_EQUALS) {

                int bodyLength = 0;
                for (int i = bssohidx + 3; i < blsohidx; i++) {
                    bodyLength *= 10;/*from ww w.  j av  a2s.com*/
                    bodyLength += (in.getByte(i) - '0');
                }

                m_msgLength = 1 + bodyLength + (blsohidx - ridx) + FIXCodecHelper.MSG_CSUM_LEN;
            } else {
                throw new Error("Unexpected state (header)");
            }
        }
    }

    if (m_msgLength != -1 && in.readableBytes() >= m_msgLength) {
        if (in.readableBytes() >= m_msgLength) {
            byte[] rv = new byte[m_msgLength];
            in.readBytes(rv);

            //TODO: validate checksum
            out.add(rv);

            m_msgLength = -1;
        } else {
            throw new Error("Unexpected state (body)");
        }
    }
}

From source file:com.whizzosoftware.hobson.radiora.api.codec.RadioRaFrameDecoder.java

License:Open Source License

@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf buffer) throws Exception {
    logger.trace("decode: {}", buffer.toString(CharsetUtil.UTF_8));

    ByteBuf frame = (ByteBuf) super.decode(ctx, buffer);

    if (frame != null) {
        // if we receive a single byte frame, it should be a '!'
        if (frame.readableBytes() == 1) {
            byte b = frame.readByte();
            if (b == '!') {
                return null;
            } else {
                throw new CorruptedFrameException("Unexpected single byte frame");
            }//from w  w  w  .  j  av  a 2  s  . c  o  m
            // otherwise, we assume it's a command frame
        } else {
            int ix = frame.indexOf(frame.readerIndex(), frame.writerIndex(), (byte) ',');
            if (ix > 0) {
                String cmdName = frame.slice(0, ix).toString(CharsetUtil.UTF_8);

                switch (cmdName) {
                case ZoneMap.TYPE:
                    if (frame.readableBytes() >= ix + 33) {
                        return new ZoneMap(frame.slice(ix + 1, 32).toString(CharsetUtil.UTF_8));
                    } else {
                        throw new CorruptedFrameException("Received invalid zone map size");
                    }

                case LocalZoneChange.TYPE:
                    if (frame.readableBytes() >= ix + 3) {
                        String sZoneNum = frame.slice(ix + 1, 2).toString(CharsetUtil.UTF_8);
                        if (frame.readableBytes() >= ix + 7) {
                            String state = frame.slice(ix + 4, 3).toString(CharsetUtil.UTF_8).trim();
                            try {
                                return new LocalZoneChange(Integer.parseInt(sZoneNum),
                                        LocalZoneChange.State.valueOf(state));
                            } catch (IllegalArgumentException iae) {
                                throw new CorruptedFrameException("Invalid LZC state string");
                            }
                        } else {
                            throw new CorruptedFrameException("Invalid LZC size (state)");
                        }
                    } else {
                        throw new CorruptedFrameException("Invalid LZC size (zoneNum)");
                    }

                case LEDMap.TYPE:
                    if (frame.readableBytes() >= ix + 16) {
                        return new LEDMap(frame.slice(ix + 1, 15).toString(CharsetUtil.UTF_8));
                    } else {
                        throw new CorruptedFrameException("Invalid LED map size");
                    }

                default:
                    throw new DecoderException("Unrecognized command: " + cmdName);
                }
            } else {
                throw new CorruptedFrameException("Invalid frame format (no comma)");
            }
        }
    } else {
        return null;
    }
}

From source file:io.nodyn.http.HTTPParser.java

License:Apache License

protected boolean readRequestLine() {
    ByteBuf line = readLine();
    if (line == null) {
        return false;
    }/*from w w  w .j  a  v  a2 s .  co m*/

    int space = line.indexOf(line.readerIndex(), line.readerIndex() + line.readableBytes(), (byte) ' ');
    if (space < 0) {
        setError(Error.INVALID_METHOD);
        return false;
    }

    int len = space - line.readerIndex();

    ByteBuf methodBuf = line.readSlice(len);

    String methodName = methodBuf.toString(UTF8);
    for (int i = 0; i < METHODS.length; ++i) {
        if (METHODS[i].equals(methodName)) {
            this.method = i;
            break;
        }
    }

    if (this.method == null) {
        setError(Error.INVALID_METHOD);
        return false;
    }

    if ("CONNECT".equals(methodName)) {
        this.upgrade = true;
    }

    // skip the space
    line.readByte();

    space = line.indexOf(line.readerIndex(), line.readerIndex() + line.readableBytes(), (byte) ' ');

    ByteBuf urlBuf = null;
    ByteBuf versionBuf = null;
    if (space < 0) {
        // HTTP/1.0
        urlBuf = line.readSlice(line.readableBytes());
    } else {
        len = space - line.readerIndex();
        urlBuf = line.readSlice(len);
        versionBuf = line.readSlice(line.readableBytes());
    }

    this.url = urlBuf.toString(UTF8).trim();

    if (versionBuf != null) {
        if (!readVersion(versionBuf)) {
            setError(Error.INVALID_VERSION);
            return false;
        }
    } else {
        this.versionMajor = 1;
        this.versionMinor = 0;
    }
    return true;
}

From source file:io.nodyn.http.HTTPParser.java

License:Apache License

protected boolean readStatusLine() {
    ByteBuf line = readLine();

    if (line == null) {
        return false;
    }/*w  w  w . j ava2  s  .c  o  m*/

    int space = line.indexOf(line.readerIndex(), line.readerIndex() + line.readableBytes(), (byte) ' ');

    if (space < 0) {
        setError(Error.INVALID_VERSION);
        return false;
    }

    int len = space - line.readerIndex();

    ByteBuf versionBuf = line.readSlice(len);

    if (!readVersion(versionBuf)) {
        setError(Error.INVALID_VERSION);
        return false;
    }

    // skip space
    line.readByte();

    space = line.indexOf(line.readerIndex(), line.readerIndex() + line.readableBytes(), (byte) ' ');

    if (space < 0) {
        setError(Error.INVALID_STATUS);
        return false;
    }

    len = space - line.readerIndex();

    ByteBuf statusBuf = line.readSlice(len);

    int status = -1;

    try {
        status = Integer.parseInt(statusBuf.toString(UTF8));
    } catch (NumberFormatException e) {
        setError(Error.INVALID_STATUS);
        return false;
    }

    if (status > 999 || status < 100) {
        setError(Error.INVALID_STATUS);
        return false;
    }

    this.statusCode = status;

    // skip space
    line.readByte();

    ByteBuf messageBuf = line.readSlice(line.readableBytes());

    this.statusMessage = messageBuf.toString(UTF8).trim();

    return true;
}

From source file:io.nodyn.http.HTTPParser.java

License:Apache License

protected boolean readVersion(ByteBuf versionBuf) {
    int dotLoc = versionBuf.indexOf(versionBuf.readerIndex(),
            versionBuf.readerIndex() + versionBuf.readableBytes(), (byte) '.');
    if (dotLoc < 0) {
        return false;
    }//  w  w  w  . j a  v  a2 s. c  o m

    char majorChar = (char) versionBuf.getByte(dotLoc - 1);
    char minorChar = (char) versionBuf.getByte(dotLoc + 1);
    try {
        this.versionMajor = Integer.parseInt("" + majorChar);
        this.versionMinor = Integer.parseInt("" + minorChar);
    } catch (NumberFormatException e) {
        return false;
    }
    return true;
}

From source file:io.nodyn.http.HTTPParser.java

License:Apache License

protected boolean readHeader(ByteBuf line, List<String> target, boolean analyze) {
    int colonLoc = line.indexOf(line.readerIndex(), line.readerIndex() + line.readableBytes(), (byte) ':');

    if (colonLoc < 0) {
        // maybe it's a continued header
        if (line.readableBytes() > 1) {
            char c = (char) line.getByte(0);
            if (c == ' ' || c == '\t') {
                // it IS a continued header value
                int lastIndex = this.headers.size() - 1;
                String val = this.headers.get(lastIndex);
                val = val + " " + line.toString(ASCII).trim();
                this.headers.set(lastIndex, val);
                return true;
            }/*  www .  j ava 2  s .com*/
        }
        return false;
    }

    int len = colonLoc - line.readerIndex();
    ByteBuf keyBuf = line.readSlice(len);

    // skip colon
    line.readByte();

    ByteBuf valueBuf = line.readSlice(line.readableBytes());

    String key = keyBuf.toString(UTF8).trim();
    String value = valueBuf.toString(UTF8).trim();

    target.add(key);
    target.add(value);

    if (analyze) {
        return analyzeHeader(key.toLowerCase(), value);
    }

    return true;
}

From source file:io.reactiverse.pgclient.impl.codec.DataTypeCodec.java

License:Apache License

private static Point textDecodePOINT(int index, int len, ByteBuf buff) {
    // Point representation: (x,y)
    int idx = ++index;
    int s = buff.indexOf(idx, idx + len, (byte) ',');
    int t = s - idx;
    double x = textDecodeFLOAT8(idx, t, buff);
    double y = textDecodeFLOAT8(s + 1, len - t - 3, buff);
    return new Point(x, y);
}

From source file:io.reactiverse.pgclient.impl.codec.DataTypeCodec.java

License:Apache License

private static Line textDecodeLine(int index, int len, ByteBuf buff) {
    // Line representation: {a,b,c}
    int idxOfFirstSeparator = buff.indexOf(index, index + len, (byte) ',');
    int idxOfLastSeparator = buff.indexOf(index + len, index, (byte) ',');

    int idx = index + 1;
    double a = textDecodeFLOAT8(idx, idxOfFirstSeparator - idx, buff);
    double b = textDecodeFLOAT8(idxOfFirstSeparator + 1, idxOfLastSeparator - idxOfFirstSeparator - 1, buff);
    double c = textDecodeFLOAT8(idxOfLastSeparator + 1, index + len - idxOfLastSeparator - 2, buff);
    return new Line(a, b, c);
}

From source file:io.reactiverse.pgclient.impl.codec.DataTypeCodec.java

License:Apache License

private static LineSegment textDecodeLseg(int index, int len, ByteBuf buff) {
    // Lseg representation: [p1,p2]
    int idxOfPointsSeparator = buff.indexOf(index, index + len, (byte) ')') + 1;
    int lenOfP1 = idxOfPointsSeparator - index - 1;
    Point p1 = textDecodePOINT(index + 1, lenOfP1, buff);
    Point p2 = textDecodePOINT(idxOfPointsSeparator + 1, len - lenOfP1 - 3, buff);
    return new LineSegment(p1, p2);
}

From source file:io.reactiverse.pgclient.impl.codec.DataTypeCodec.java

License:Apache License

private static Box textDecodeBox(int index, int len, ByteBuf buff) {
    // Box representation: p1,p2
    int idxOfPointsSeparator = buff.indexOf(index, index + len, (byte) ')') + 1;
    int lenOfUpperRightCornerPoint = idxOfPointsSeparator - index;
    Point upperRightCorner = textDecodePOINT(index, lenOfUpperRightCornerPoint, buff);
    Point lowerLeftCorner = textDecodePOINT(idxOfPointsSeparator + 1, len - lenOfUpperRightCornerPoint - 1,
            buff);/* w  w w  .j  a  va2 s . com*/
    return new Box(upperRightCorner, lowerLeftCorner);
}