Java Code Examples for io.netty.util.internal.AppendableCharSequence#toString()

The following examples show how to use io.netty.util.internal.AppendableCharSequence#toString() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
Source File: AtomOrStringParser.java    From NioImapClient with Apache License 2.0 4 votes vote down vote up
public String parse(ByteBuf buffer) {
  AppendableCharSequence seq = sequenceRef.get();

  seq.reset();
  size = 0;

  boolean isQuoted = false;
  char previousChar = ' ';
  for (;;) {
    char c;
    try {
      c = ((char) buffer.readUnsignedByte());
    } catch (IndexOutOfBoundsException e) {
      return seq.toString();
    }

    if (Character.isWhitespace(c)) {
      if (isQuoted) {
        append(seq, c);
      } else if (size > 0) {
        break;
      }
    } else if (c == QUOTE && previousChar != BACKSLASH) {
      if (size == 0 && !isQuoted) {    // Start Quote
        isQuoted = true;
      } else {            // End Quote
        break;
      }
    } else if (!isQuoted && (c == ')' || c == '(')) {
      buffer.readerIndex(buffer.readerIndex() - 1);
      break;
    } else if (!isQuoted && (c == '{')) {
      return parseLiteral(buffer, seq);
    } else {
      append(seq, c);
    }

    // Always ignore any characters after a backslash
    if (previousChar != BACKSLASH) {
      previousChar = c;
    } else {
      previousChar = ' ';
    }
  }

  return seq.toString();
}