Java Code Examples for io.netty.buffer.ByteBuf#indexOf()

The following examples show how to use io.netty.buffer.ByteBuf#indexOf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JT808MessageDecoder.java    From jt808-server with Apache License 2.0 6 votes vote down vote up
/**
 * 反转义
 */
@Override
public ByteBuf unEscape(ByteBuf source) {
    int low = source.readerIndex();
    int high = source.writerIndex();

    int mark = source.indexOf(low, high, (byte) 0x7d);
    if (mark == -1)
        return source;

    List<ByteBuf> bufList = new ArrayList<>(3);

    int len;
    do {

        len = mark + 2 - low;
        bufList.add(slice(source, low, len));
        low += len;

        mark = source.indexOf(low, high, (byte) 0x7d);
    } while (mark > 0);

    bufList.add(source.slice(low, high - low));

    return new CompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, false, bufList.size(), bufList);
}
 
Example 2
Source File: HttpProxyMatcher.java    From cute-proxy with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public int match(ByteBuf buf) {
    if (buf.readableBytes() < 8) {
        return PENDING;
    }

    int index = buf.indexOf(0, 8, (byte) ' ');
    if (index < 0) {
        return MISMATCH;
    }

    int firstURIIndex = index + 1;
    if (buf.readableBytes() < firstURIIndex + 1) {
        return PENDING;
    }

    String method = buf.toString(0, index, US_ASCII);
    char firstURI = (char) (buf.getByte(firstURIIndex + buf.readerIndex()) & 0xff);
    if (!methods.contains(method) || firstURI == '/') {
        return MISMATCH;
    }


    return MATCH;
}
 
Example 3
Source File: HttpMatcher.java    From cute-proxy with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public int match(ByteBuf buf) {
    if (buf.readableBytes() < 8) {
        return PENDING;
    }

    int index = buf.indexOf(0, 8, (byte) ' ');
    if (index < 0) {
        return MISMATCH;
    }

    int firstURIIndex = index + 1;
    if (buf.readableBytes() < firstURIIndex + 1) {
        return PENDING;
    }

    String method = buf.toString(0, index, US_ASCII);
    char firstURI = (char) (buf.getByte(firstURIIndex + buf.readerIndex()) & 0xff);
    if (!methods.contains(method) || firstURI != '/') {
        return MISMATCH;
    }

    return MATCH;
}
 
Example 4
Source File: DataTypeCodec.java    From vertx-sql-client with Apache License 2.0 6 votes vote down vote up
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;
    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()));
}
 
Example 5
Source File: SetCodec.java    From r2dbc-mysql with Apache License 2.0 5 votes vote down vote up
@Override
public String[] decode(ByteBuf value, FieldInformation info, Class<?> target, boolean binary, CodecContext context) {
    if (!value.isReadable()) {
        return EMPTY_STRINGS;
    }

    int firstComma = value.indexOf(value.readerIndex(), value.writerIndex(), (byte) ',');
    Charset charset = CharCollation.fromId(info.getCollationId(), context.getServerVersion()).getCharset();

    if (firstComma < 0) {
        return new String[] { value.toString(charset) };
    }

    return value.toString(charset).split(",");
}
 
Example 6
Source File: WebSocket00FrameDecoder.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
private WebSocketFrame decodeTextFrame(ChannelHandlerContext ctx, ByteBuf buffer) {
    int ridx = buffer.readerIndex();
    int rbytes = actualReadableBytes();
    int delimPos = buffer.indexOf(ridx, ridx + rbytes, (byte) 0xFF);
    if (delimPos == -1) {
        // Frame delimiter (0xFF) not found
        if (rbytes > maxFrameSize) {
            // Frame length exceeded the maximum
            throw new TooLongFrameException();
        } else {
            // Wait until more data is received
            return null;
        }
    }

    int frameSize = delimPos - ridx;
    if (frameSize > maxFrameSize) {
        throw new TooLongFrameException();
    }

    ByteBuf binaryData = ctx.alloc().buffer(frameSize);
    buffer.readBytes(binaryData);
    buffer.skipBytes(1);

    int ffDelimPos = binaryData.indexOf(binaryData.readerIndex(), binaryData.writerIndex(), (byte) 0xFF);
    if (ffDelimPos >= 0) {
        throw new IllegalArgumentException("a text frame should not contain 0xFF.");
    }

    return new TextWebSocketFrame(binaryData);
}
 
Example 7
Source File: DataTypeCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 8
Source File: DataTypeCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
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);
    start = idxOfPointSeparator + 1;
  }
  return points;
}
 
Example 9
Source File: DataTypeCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
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);
  return new Box(upperRightCorner, lowerLeftCorner);
}
 
Example 10
Source File: DataTypeCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 11
Source File: DataTypeCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 12
Source File: DataTypeCodec.java    From vertx-sql-client with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 13
Source File: CodecUtils.java    From spring-boot-protocol with Apache License 2.0 5 votes vote down vote up
public static int findNullTermLen(ByteBuf buf) {
	int termIdx = buf.indexOf(buf.readerIndex(), buf.capacity(), (byte) 0);
	if (termIdx < 0) {
		return -1;
	}
	return termIdx - buf.readerIndex();
}
 
Example 14
Source File: SetCodec.java    From r2dbc-mysql with Apache License 2.0 5 votes vote down vote up
@Override
public String[] decode(ByteBuf value, FieldInformation info, Class<?> target, boolean binary, CodecContext context) {
    if (!value.isReadable()) {
        return EMPTY_STRINGS;
    }

    int firstComma = value.indexOf(value.readerIndex(), value.writerIndex(), (byte) ',');
    Charset charset = CharCollation.fromId(info.getCollationId(), context.getServerVersion()).getCharset();

    if (firstComma < 0) {
        return new String[] { value.toString(charset) };
    }

    return value.toString(charset).split(",");
}
 
Example 15
Source File: DefaultDecoder.java    From ratel with Apache License 2.0 5 votes vote down vote up
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
	int startIndex = -1;
	int endIndex = -1;
	if((startIndex = in.indexOf(in.readerIndex(), in.writerIndex(), TransferProtocolUtils.PROTOCOL_HAED)) != -1 && 
			(endIndex = in.indexOf(startIndex + 1, in.writerIndex(), TransferProtocolUtils.PROTOCOL_TAIL)) != -1) {
		endIndex ++;
		byte[] bytes = new byte[endIndex - startIndex];
		in.skipBytes(startIndex - in.readerIndex());
		in.readBytes(bytes, 0, bytes.length);
		out.add(bytes);
	}
}
 
Example 16
Source File: WebSocket00FrameDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private WebSocketFrame decodeTextFrame(ChannelHandlerContext ctx, ByteBuf buffer) {
    int ridx = buffer.readerIndex();
    int rbytes = actualReadableBytes();
    int delimPos = buffer.indexOf(ridx, ridx + rbytes, (byte) 0xFF);
    if (delimPos == -1) {
        // Frame delimiter (0xFF) not found
        if (rbytes > maxFrameSize) {
            // Frame length exceeded the maximum
            throw new TooLongFrameException();
        } else {
            // Wait until more data is received
            return null;
        }
    }

    int frameSize = delimPos - ridx;
    if (frameSize > maxFrameSize) {
        throw new TooLongFrameException();
    }

    ByteBuf binaryData = readBytes(ctx.alloc(), buffer, frameSize);
    buffer.skipBytes(1);

    int ffDelimPos = binaryData.indexOf(binaryData.readerIndex(), binaryData.writerIndex(), (byte) 0xFF);
    if (ffDelimPos >= 0) {
        binaryData.release();
        throw new IllegalArgumentException("a text frame should not contain 0xFF.");
    }

    return new TextWebSocketFrame(binaryData);
}
 
Example 17
Source File: NettyFrameDecoder.java    From TransFIX with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * TODO: loop for more messages in the ByteBuf
 */
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)");
        }
    }
}