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:io.reactiverse.pgclient.impl.codec.DataTypeCodec.java

License:Apache License

private static Box[] textDecodeBoxArray(IntFunction<Box[]> supplier, int index, int len, ByteBuf buff) {
    // Box Array representation: {box1;box2;...boxN}
    List<Box> boxes = new ArrayList<>();
    int start = index + 1;
    int end = index + len - 1;
    while (start < end) {
        int idxOfBoxSeparator = buff.indexOf(start, end + 1, (byte) ';');
        if (idxOfBoxSeparator == -1) {
            // the last box
            Box box = textDecodeBox(start, end - start, buff);
            boxes.add(box);/*from   w  ww  .  j a v a  2  s .  com*/
            break;
        }
        int lenOfBox = idxOfBoxSeparator - start;
        Box box = textDecodeBox(start, lenOfBox, buff);
        boxes.add(box);
        start = idxOfBoxSeparator + 1;
    }
    return boxes.toArray(supplier.apply(boxes.size()));
}

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

License:Apache License

private static List<Point> textDecodeMultiplePoints(int index, int len, ByteBuf buff) {
    // representation: p1,p2,p3...pn
    List<Point> points = new ArrayList<>();
    int start = index;
    int end = index + len - 1;
    while (start < end) {
        int rightParenthesis = buff.indexOf(start, end + 1, (byte) ')');
        int idxOfPointSeparator = rightParenthesis + 1;
        int lenOfPoint = idxOfPointSeparator - start;
        Point point = textDecodePOINT(start, lenOfPoint, buff);
        points.add(point);/*from w  w  w  .  j av a  2 s . co  m*/
        start = idxOfPointSeparator + 1;
    }
    return points;
}

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

License:Apache License

private static Circle textDecodeCircle(int index, int len, ByteBuf buff) {
    // Circle representation: <p,r>
    int idxOfLastComma = buff.indexOf(index + len - 1, index, (byte) ',');
    int lenOfPoint = idxOfLastComma - index - 1;
    Point center = textDecodePOINT(index + 1, lenOfPoint, buff);
    int lenOfRadius = len - lenOfPoint - 3;
    double radius = textDecodeFLOAT8(idxOfLastComma + 1, lenOfRadius, buff);
    return new Circle(center, radius);
}

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

License:Apache License

private static <T> T[] textDecodeArray(IntFunction<T[]> supplier, DataType type, int index, int len,
        ByteBuf buff) {
    List<T> list = new ArrayList<>();
    int from = index + 1; // Set index after '{'
    int to = index + len - 1; // Set index before '}'
    while (from < to) {
        // Escaped content ?
        boolean escaped = buff.getByte(from) == '"';
        int idx;/* w w w.j  a v  a  2  s  . c  o m*/
        if (escaped) {
            idx = buff.forEachByte(from, to - from, new UTF8StringEndDetector());
            idx = buff.indexOf(idx, to, (byte) ','); // SEE iF WE CAN GET RID oF IT
        } else {
            idx = buff.indexOf(from, to, (byte) ',');
        }
        if (idx == -1) {
            idx = to;
        }
        T elt = textDecodeArrayElement(type, from, idx - from, buff);
        list.add(elt);
        from = idx + 1;
    }
    return list.toArray(supplier.apply(list.size()));
}

From source file:net.openhft.fix.transport.codec.NettyFrameDecoder.java

License:Apache License

/**
 * TODO: loop for more messages in the ByteBuf
*///from  w ww .java  2 s . c  om
void doDecode(ByteBuf in, List<Object> out) {
    if (m_msgLength == -1) {
        if (in.readableBytes() >= NettyFrameHelper.MSG_MIN_BYTES) {
            //int rindex = in.readerIndex();

            int bsi = in.indexOf(0, 12, NettyFrameHelper.BYTE_SOH);
            int bli = in.indexOf(12, 20, NettyFrameHelper.BYTE_SOH);

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

                int bodyLength = 0;
                for (int i = bsi + 3; i < bli; i++) {
                    bodyLength *= 10;
                    bodyLength += ((int) in.getByte(i) - (int) '0');
                }

                m_msgLength = 1 + bodyLength + bli + NettyFrameHelper.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);
            in.discardReadBytes();

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

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

From source file:org.dcache.xrootd.protocol.messages.LoginRequest.java

License:Open Source License

public LoginRequest(ByteBuf buffer) {
    super(buffer, kXR_login);

    int pos = buffer.indexOf(8, 16, (byte) 0); // User name is padded with '\0'
    if (pos > -1) {
        username = buffer.toString(8, pos - 8, US_ASCII);
    } else {//from   ww w  .ja v  a  2s.c  om
        username = buffer.toString(8, 8, US_ASCII);
    }

    pid = buffer.getInt(4);
    capver = buffer.getUnsignedByte(18);
    role = buffer.getUnsignedByte(19);

    int tlen = buffer.getInt(20);
    token = buffer.toString(24, tlen, US_ASCII);
}

From source file:org.dcache.xrootd.protocol.messages.MvRequest.java

License:Open Source License

public MvRequest(ByteBuf buffer) {
    super(buffer, kXR_mv);

    int dlen = buffer.getInt(20);
    int end = 24 + dlen;

    int psep = buffer.indexOf(24, end, (byte) 0x20);

    if (psep == -1) {
        throw new IllegalArgumentException("kXR_mv needs two paths!");
    }//from   ww  w.jav a2s .  com

    String source = buffer.toString(24, psep - 24, US_ASCII);
    String target = buffer.toString(psep + 1, end - (psep + 1), US_ASCII);

    int osep = source.indexOf("?");

    if (osep > -1) {
        sourcePath = source.substring(0, osep);
        sourceOpaque = source.substring(osep + 1);
    } else {
        sourcePath = source;
    }

    osep = target.indexOf("?");

    if (osep > -1) {
        targetPath = target.substring(0, osep);
        targetOpaque = target.substring(osep + 1);
    } else {
        targetPath = target;
    }
}

From source file:org.dcache.xrootd.protocol.messages.PathRequest.java

License:Open Source License

private void setPathAndOpaque(ByteBuf buffer, int begin, int length) {
    int end = begin + length;
    int pos = buffer.indexOf(begin, end, XrootdProtocol.OPAQUE_DELIMITER);
    if (pos > -1) {
        setPath(buffer.toString(begin, pos - begin, US_ASCII));
        setOpaque(buffer.toString(pos + 1, end - (pos + 1), US_ASCII));
    } else {/*  www .j av  a 2 s. c  o  m*/
        setPath(buffer.toString(begin, end - begin, US_ASCII));
        setOpaque("");
    }
}

From source file:org.openremote.agent.protocol.velbus.VelbusPacketEncoderDecoder.java

License:Open Source License

public static void decode(ByteBuf buf, List<VelbusPacket> messages) {
    int startIndex = buf.indexOf(0, buf.capacity() - 1, VelbusPacket.STX);

    if (startIndex < 0) {
        return;/*from w w w.  j  ava  2s  .  co  m*/
    }

    if (startIndex > 0) {
        buf.readerIndex(startIndex);
        buf.discardReadBytes();
    }

    if (buf.readableBytes() < 4) {
        return;
    }

    int dataSize = buf.getByte(3);

    if (buf.readableBytes() < 6 + dataSize) {
        return;
    }

    // Find end of packet
    int endIndex = buf.indexOf(4 + dataSize, MAX_PACKET_SIZE, VelbusPacket.ETX);

    if (endIndex < 0) {
        if (buf.readableBytes() > MAX_PACKET_SIZE) {
            buf.readerIndex(MAX_PACKET_SIZE);
            buf.discardReadBytes();
        }
        return;
    }

    byte[] packetBytes = new byte[endIndex + 1];
    buf.readBytes(packetBytes);
    buf.discardReadBytes();
    VelbusPacket packet = new VelbusPacket(packetBytes);

    if (packet.isValid()) {
        messages.add(packet);
    }
}

From source file:org.skfiy.typhon.net.Netty4EndpointHandler.java

License:Apache License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    Netty4Session session = ctx.attr(SESSION_KEY).get();

    // ?/*from  w w  w.java 2  s. c  om*/
    session.updateLastAccessedTime();

    // read namespace
    int len = buf.indexOf(0, 32, AbstractSession.NS_SEPARTOR);
    if (len <= 0) {
        return;
    }

    byte[] nsBytes = new byte[len];
    buf.readBytes(nsBytes, 0, nsBytes.length);
    buf.skipBytes(1);
    byte[] dataBytes = new byte[buf.readableBytes()];
    buf.readBytes(dataBytes, 0, dataBytes.length);

    protocolHandler.handle(session, nsBytes, dataBytes);
}