io.netty.util.internal.AppendableCharSequence Java Examples

The following examples show how to use io.netty.util.internal.AppendableCharSequence. 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: MqttOverWebsocketProtocol.java    From joyqueue with Apache License 2.0 6 votes vote down vote up
private static String[] splitInitialLine(AppendableCharSequence sb) {
    int aStart;
    int aEnd;
    int bStart;
    int bEnd;
    int cStart;
    int cEnd;

    aStart = findNonWhitespace(sb, 0);
    aEnd = findWhitespace(sb, aStart);

    bStart = findNonWhitespace(sb, aEnd);
    bEnd = findWhitespace(sb, bStart);

    cStart = findNonWhitespace(sb, bEnd);
    cEnd = findEndOfString(sb);

    return new String[] {
            sb.subStringUnsafe(aStart, aEnd),
            sb.subStringUnsafe(bStart, bEnd),
            cStart < cEnd? sb.subStringUnsafe(cStart, cEnd) : "" };
}
 
Example #2
Source File: LiteralStringSizeParser.java    From NioImapClient with Apache License 2.0 6 votes vote down vote up
@Override
public Integer parse(ByteBuf in) {
  AppendableCharSequence seq = sequenceRef.get();

  seq.reset();
  boolean foundStart = false;

  for (;;) {
    char c = ((char) in.readUnsignedByte());

    if (c == '{') {
      foundStart = true;
    } else if (c == '}') {
      return Integer.parseInt(seq.toString());
    } else if (foundStart) {
      seq.append(c);
    }
  }
}
 
Example #3
Source File: HttpObjectDecoder.java    From netty4.0.27Learn with Apache License 2.0 6 votes vote down vote up
private static String[] splitInitialLine(AppendableCharSequence sb) {
    int aStart;
    int aEnd;
    int bStart;
    int bEnd;
    int cStart;
    int cEnd;

    aStart = findNonWhitespace(sb, 0);
    aEnd = findWhitespace(sb, aStart);

    bStart = findNonWhitespace(sb, aEnd);
    bEnd = findWhitespace(sb, bStart);

    cStart = findNonWhitespace(sb, bEnd);
    cEnd = findEndOfString(sb);

    return new String[] {
            sb.substring(aStart, aEnd),
            sb.substring(bStart, bEnd),
            cStart < cEnd? sb.substring(cStart, cEnd) : "" };
}
 
Example #4
Source File: StompSubframeDecoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private String readLine(ByteBuf buffer, int initialBufferSize) {
    AppendableCharSequence buf = new AppendableCharSequence(initialBufferSize);
    int lineLength = 0;
    for (;;) {
        byte nextByte = buffer.readByte();
        if (nextByte == StompConstants.CR) {
            //do nothing
        } else if (nextByte == StompConstants.LF) {
            return buf.toString();
        } else {
            if (lineLength >= maxLineLength) {
                invalidLineLength();
            }
            lineLength ++;
            buf.append((char) nextByte);
        }
    }
}
 
Example #5
Source File: HttpObjectDecoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
protected HttpObjectDecoder(
        int maxInitialLineLength, int maxHeaderSize, int maxChunkSize,
        boolean chunkedSupported, boolean validateHeaders, int initialBufferSize) {
    if (maxInitialLineLength <= 0) {
        throw new IllegalArgumentException(
                "maxInitialLineLength must be a positive integer: " +
                 maxInitialLineLength);
    }
    if (maxHeaderSize <= 0) {
        throw new IllegalArgumentException(
                "maxHeaderSize must be a positive integer: " +
                maxHeaderSize);
    }
    if (maxChunkSize <= 0) {
        throw new IllegalArgumentException(
                "maxChunkSize must be a positive integer: " +
                maxChunkSize);
    }
    AppendableCharSequence seq = new AppendableCharSequence(initialBufferSize);
    lineParser = new LineParser(seq, maxInitialLineLength);
    headerParser = new HeaderParser(seq, maxHeaderSize);
    this.maxChunkSize = maxChunkSize;
    this.chunkedSupported = chunkedSupported;
    this.validateHeaders = validateHeaders;
}
 
Example #6
Source File: HttpObjectDecoder.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private static String[] splitInitialLine(AppendableCharSequence sb) {
    int aStart;
    int aEnd;
    int bStart;
    int bEnd;
    int cStart;
    int cEnd;

    aStart = findNonWhitespace(sb, 0);
    aEnd = findWhitespace(sb, aStart);

    bStart = findNonWhitespace(sb, aEnd);
    bEnd = findWhitespace(sb, bStart);

    cStart = findNonWhitespace(sb, bEnd);
    cEnd = findEndOfString(sb);

    return new String[] {
            sb.subStringUnsafe(aStart, aEnd),
            sb.subStringUnsafe(bStart, bEnd),
            cStart < cEnd? sb.subStringUnsafe(cStart, cEnd) : "" };
}
 
Example #7
Source File: MqttOverWebsocketProtocol.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
private static int findWhitespace(AppendableCharSequence sb, int offset) {
    for (int result = offset; result < sb.length(); ++result) {
        if (Character.isWhitespace(sb.charAtUnsafe(result))) {
            return result;
        }
    }
    return sb.length();
}
 
Example #8
Source File: AtomOrStringParser.java    From NioImapClient with Apache License 2.0 5 votes vote down vote up
private void append(AppendableCharSequence seq, ByteBuf buffer, int length) {
  if (size + length >= maxStringLength) {
    throw new TooLongFrameException("String is larger than " + maxStringLength + " bytes.");
  }

  size += length;
  seq.append(buffer.readCharSequence(length - 1, StandardCharsets.UTF_8));
}
 
Example #9
Source File: AtomOrStringParser.java    From NioImapClient with Apache License 2.0 5 votes vote down vote up
private void append(AppendableCharSequence seq, char c) {
  if (size >= maxStringLength) {
    throw new TooLongFrameException("String is larger than " + maxStringLength + " bytes.");
  }

  size++;
  seq.append(c);
}
 
Example #10
Source File: AtomOrStringParser.java    From NioImapClient with Apache License 2.0 5 votes vote down vote up
/**
 * See https://tools.ietf.org/html/rfc3501.html#section-4.3
 *
 * String literals have the form
 *
 * {10}
 * abcdefghij
 *
 * Where {10} represents the length of the string literal. The literal
 * begins after any CLRF characters following the '}' character.
 */
private String parseLiteral(ByteBuf buffer, AppendableCharSequence seq) {
  int length = 0;

  char c = (char) buffer.readUnsignedByte();
  while (c != '}') {
    if (Character.isDigit(c)) {
      int digit = Character.digit(c, 10);
      length = (length * 10) + digit;
      c = (char) buffer.readUnsignedByte();
    } else {
      throw new DecoderException(
          String.format("Found non-digit character %c where a digit was expected", c)
      );
    }
  }

  if (length > 0) {
    //ignore crlf characters after '}'
    c = (char) buffer.readUnsignedByte();
    while (isCRLFCharacter(c)) {
      c = (char) buffer.readUnsignedByte();
    }

    append(seq, c);
    append(seq, buffer, length);

    return seq.toString();
  } else {
    return "";
  }
}
 
Example #11
Source File: FetchResponseTypeParser.java    From NioImapClient with Apache License 2.0 5 votes vote down vote up
@Override
public String parse(ByteBuf in) {
  AppendableCharSequence seq = sequenceRef.get();

  seq.reset();
  size = 0;
  for (;;) {
    char nextByte = (char) in.readUnsignedByte();
    if (Character.isWhitespace(nextByte)) {
      if (size > 0) {
        break;
      }
    } else if (!Character.isLetterOrDigit(nextByte) && nextByte != '.' && nextByte != '-') {
      in.readerIndex(in.readerIndex() - 1);
      break;
    } else {
      if (size >= maxWordLength) {
        throw new TooLongFrameException(
            "Word is larger than " + maxWordLength +
                " bytes.");
      }
      size++;
      seq.append(nextByte);
    }
  }

  return seq.toString();
}
 
Example #12
Source File: AllBytesParser.java    From NioImapClient with Apache License 2.0 5 votes vote down vote up
@Override
public String parse(ByteBuf buffer) {
  AppendableCharSequence sequence = sequenceReference.get();

  int readerIndex = buffer.readerIndex();
  try {
    super.parse(buffer);
  } catch (Signal e) {
    e.expect(REPLAYING_SIGNAL);
    buffer.readerIndex(readerIndex + size);
    return sequence.toString();
  }

  return sequence.toString();
}
 
Example #13
Source File: LiteralStringParser.java    From NioImapClient with Apache License 2.0 5 votes vote down vote up
@Override
public String parse(ByteBuf in) {
  AppendableCharSequence seq = sequenceRef.get();

  seq.reset();
  size = 0;
  expectedSize = -1;
  for (;;) {
    char c = ((char) in.readUnsignedByte());
    if (c == '{' && expectedSize < 0) {
      in.readerIndex(in.readerIndex() - 1);
      expectedSize = sizeParser.parse(in);

      in.readerIndex(in.readerIndex() + 2); // Skip CRLF
    } else if (expectedSize >= 0) {
      seq.reset();
      seq.append(c);
      size++;

      while (size < expectedSize) {
        c = ((char) in.readUnsignedByte());
        seq.append(c);
        size++;
      }

      return seq.toString();
    } else if (Character.isWhitespace(c)) {
      continue;
    } else {
      in.readerIndex(in.readerIndex() - 1);
      return stringParser.parse(in);
    }
  }
}
 
Example #14
Source File: HttpObjectDecoder.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance with the specified parameters.
 */
protected HttpObjectDecoder(
        int maxInitialLineLength, int maxHeaderSize, int maxChunkSize,
        boolean chunkedSupported, boolean validateHeaders) {

    if (maxInitialLineLength <= 0) {
        throw new IllegalArgumentException(
                "maxInitialLineLength must be a positive integer: " +
                        maxInitialLineLength);
    }
    if (maxHeaderSize <= 0) {
        throw new IllegalArgumentException(
                "maxHeaderSize must be a positive integer: " +
                        maxHeaderSize);
    }
    if (maxChunkSize <= 0) {
        throw new IllegalArgumentException(
                "maxChunkSize must be a positive integer: " +
                        maxChunkSize);
    }
    this.maxChunkSize = maxChunkSize;
    this.chunkedSupported = chunkedSupported;
    this.validateHeaders = validateHeaders;
    AppendableCharSequence seq = new AppendableCharSequence(128);
    lineParser = new LineParser(seq, maxInitialLineLength);
    headerParser = new HeaderParser(seq, maxHeaderSize);
}
 
Example #15
Source File: SoftReferencedAppendableCharSequence.java    From NioImapClient with Apache License 2.0 5 votes vote down vote up
public AppendableCharSequence get() {
  AppendableCharSequence sequence = reference.get();
  if (sequence == null) {
    sequence = newCharSeq();
  }

  reference = new SoftReference<>(sequence);

  return sequence;
}
 
Example #16
Source File: MqttOverWebsocketProtocol.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
public AppendableCharSequence parse(ByteBuf buffer) {
    final int oldSize = size;
    seq.reset();
    int i = buffer.forEachByte(this);
    if (i == -1) {
        size = oldSize;
        return null;
    }
    buffer.readerIndex(i + 1);
    return seq;
}
 
Example #17
Source File: MqttOverWebsocketProtocol.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
private static int findEndOfString(AppendableCharSequence sb) {
    for (int result = sb.length() - 1; result > 0; --result) {
        if (!Character.isWhitespace(sb.charAtUnsafe(result))) {
            return result + 1;
        }
    }
    return 0;
}
 
Example #18
Source File: HttpObjectDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private void splitHeader(AppendableCharSequence sb) {
    final int length = sb.length();
    int nameStart;
    int nameEnd;
    int colonEnd;
    int valueStart;
    int valueEnd;

    nameStart = findNonWhitespace(sb, 0);
    for (nameEnd = nameStart; nameEnd < length; nameEnd ++) {
        char ch = sb.charAt(nameEnd);
        if (ch == ':' || Character.isWhitespace(ch)) {
            break;
        }
    }

    for (colonEnd = nameEnd; colonEnd < length; colonEnd ++) {
        if (sb.charAt(colonEnd) == ':') {
            colonEnd ++;
            break;
        }
    }

    name = sb.subStringUnsafe(nameStart, nameEnd);
    valueStart = findNonWhitespace(sb, colonEnd);
    if (valueStart == length) {
        value = EMPTY_VALUE;
    } else {
        valueEnd = findEndOfString(sb);
        value = sb.subStringUnsafe(valueStart, valueEnd);
    }
}
 
Example #19
Source File: MqttOverWebsocketProtocol.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
private static int findNonWhitespace(AppendableCharSequence sb, int offset) {
    for (int result = offset; result < sb.length(); ++result) {
        if (!Character.isWhitespace(sb.charAtUnsafe(result))) {
            return result;
        }
    }
    return sb.length();
}
 
Example #20
Source File: MqttOverWebsocketProtocol.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isSupport(ByteBuf buffer) {
    // from netty protocol implement

    if (isNativeMqtt(buffer)) {
        return false;
    }

    if (!skipControlCharacters(buffer)) {
        return false;
    }
    int rIdx = buffer.readerIndex();

    LineParser lineParser = new LineParser(new AppendableCharSequence(65536), 4096);
    AppendableCharSequence line = lineParser.parse(buffer);
    String[] initialLine = splitInitialLine(line);
    if (initialLine.length < 3) {
        return false;
    }

    Map<String, String> headers = readHeaders(buffer);
    buffer.readerIndex(rIdx);
    if (!headers.containsKey("Connection") && !headers.get("Connection").equals("Upgrade")) {
        return false;
    }
    if (!headers.containsKey("Upgrade") && !headers.get("Upgrade").equals("websocket")) {
        return false;
    }
    if (!headers.containsKey("Sec-WebSocket-Protocol") && !headers.get("Sec-WebSocket-Protocol").equals("mqtt")) {
        return false;
    }

    return true;
}
 
Example #21
Source File: StompSubframeDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private boolean readHeader(StompHeaders headers, AppendableCharSequence buf, ByteBuf buffer) {
    buf.reset();
    int lineLength = 0;
    String key = null;
    boolean valid = false;
    for (;;) {
        byte nextByte = buffer.readByte();

        if (nextByte == StompConstants.COLON && key == null) {
            key = buf.toString();
            valid = true;
            buf.reset();
        } else if (nextByte == StompConstants.CR) {
            //do nothing
        } else if (nextByte == StompConstants.LF) {
            if (key == null && lineLength == 0) {
                return false;
            } else if (valid) {
                headers.add(key, buf.toString());
            } else if (validateHeaders) {
                invalidHeader(key, buf.toString());
            }
            return true;
        } else {
            if (lineLength >= maxLineLength) {
                invalidLineLength();
            }
            if (nextByte == StompConstants.COLON && key != null) {
                valid = false;
            }
            lineLength ++;
            buf.append((char) nextByte);
        }
    }
}
 
Example #22
Source File: StompSubframeDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private State readHeaders(ByteBuf buffer, StompHeaders headers) {
    AppendableCharSequence buf = new AppendableCharSequence(128);
    for (;;) {
        boolean headerRead = readHeader(headers, buf, buffer);
        if (!headerRead) {
            if (headers.contains(StompHeaders.CONTENT_LENGTH)) {
                contentLength = getContentLength(headers, 0);
                if (contentLength == 0) {
                    return State.FINALIZE_FRAME_READ;
                }
            }
            return State.READ_CONTENT;
        }
    }
}
 
Example #23
Source File: HttpObjectDecoder.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
private void splitHeader(AppendableCharSequence sb) {
    final int length = sb.length();
    int nameStart;
    int nameEnd;
    int colonEnd;
    int valueStart;
    int valueEnd;

    nameStart = findNonWhitespace(sb, 0);
    for (nameEnd = nameStart; nameEnd < length; nameEnd ++) {
        char ch = sb.charAt(nameEnd);
        if (ch == ':' || Character.isWhitespace(ch)) {
            break;
        }
    }

    for (colonEnd = nameEnd; colonEnd < length; colonEnd ++) {
        if (sb.charAt(colonEnd) == ':') {
            colonEnd ++;
            break;
        }
    }

    name = sb.substring(nameStart, nameEnd);
    valueStart = findNonWhitespace(sb, colonEnd);
    if (valueStart == length) {
        value = EMPTY_VALUE;
    } else {
        valueEnd = findEndOfString(sb);
        value = sb.substring(valueStart, valueEnd);
    }
}
 
Example #24
Source File: HttpObjectDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
public AppendableCharSequence parse(ByteBuf buffer) {
    final int oldSize = size;
    seq.reset();
    int i = buffer.forEachByte(this);
    if (i == -1) {
        size = oldSize;
        return null;
    }
    buffer.readerIndex(i + 1);
    return seq;
}
 
Example #25
Source File: HttpObjectDecoder.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
public AppendableCharSequence parse(ByteBuf buffer) {
    final int oldSize = size;
    seq.reset();
    int i = buffer.forEachByte(this);
    if (i == -1) {
        size = oldSize;
        return null;
    }
    buffer.readerIndex(i + 1);
    return seq;
}
 
Example #26
Source File: HttpObjectDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static int findEndOfString(AppendableCharSequence sb) {
    for (int result = sb.length() - 1; result > 0; --result) {
        if (!Character.isWhitespace(sb.charAtUnsafe(result))) {
            return result + 1;
        }
    }
    return 0;
}
 
Example #27
Source File: HttpObjectDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static int findWhitespace(AppendableCharSequence sb, int offset) {
    for (int result = offset; result < sb.length(); ++result) {
        if (Character.isWhitespace(sb.charAtUnsafe(result))) {
            return result;
        }
    }
    return sb.length();
}
 
Example #28
Source File: HttpObjectDecoder.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private static int findNonWhitespace(AppendableCharSequence sb, int offset) {
    for (int result = offset; result < sb.length(); ++result) {
        if (!Character.isWhitespace(sb.charAtUnsafe(result))) {
            return result;
        }
    }
    return sb.length();
}
 
Example #29
Source File: HttpObjectDecoder.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
@Override
public AppendableCharSequence parse(ByteBuf buffer) {
    reset();
    return super.parse(buffer);
}
 
Example #30
Source File: HttpObjectDecoder.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
LineParser(AppendableCharSequence seq, int maxLength) {
    super(seq, maxLength);
}