/** * Consumes the next character in the request, checking that it matches the * expected one. This method should be used when the */ protected void consumeChar(ImapRequestLineReader request, char expected) throws ProtocolException { char consumed = request.consume(); if (consumed != expected) { throw new ProtocolException("Expected:'" + expected + "' found:'" + consumed + '\''); } }
public long consumeLong(ImapRequestLineReader request) throws ProtocolException { StringBuilder atom = new StringBuilder(); char next = request.nextWordChar(); while (Character.isDigit(next)) { atom.append(next); request.consume(); next = request.nextChar(); } return Long.parseLong(atom.toString()); }
private char nextNonSpaceChar(ImapRequestLineReader request) throws ProtocolException { char next = request.nextChar(); while (next == ' ') { request.consume(); next = request.nextChar(); } return next; }
StoreDirective storeDirective(ImapRequestLineReader request) throws ProtocolException { int sign = 0; boolean silent = false; char next = request.nextWordChar(); if (next == '+') { sign = 1; request.consume(); } else if (next == '-') { sign = -1; request.consume(); } else { sign = 0; } String directive = consumeWord(request, new NoopCharValidator()); if ("FLAGS".equalsIgnoreCase(directive)) { silent = false; } else if ("FLAGS.SILENT".equalsIgnoreCase(directive)) { silent = true; } else { throw new ProtocolException("Invalid Store Directive: '" + directive + '\''); } return new StoreDirective(sign, silent); } }
/** * Reads the next "word from the request, comprising all characters up to the next SPACE. * Characters are tested by the supplied CharacterValidator, and an exception is thrown * if invalid characters are encountered. */ protected String consumeWord(ImapRequestLineReader request, CharacterValidator validator) throws ProtocolException { StringBuilder atom = new StringBuilder(); char next = request.nextWordChar(); while (!isWhitespace(next)) { if (validator.isValid(next)) { atom.append(next); request.consume(); } else { throw new ProtocolException("Invalid character: '" + next + '\''); } next = request.nextChar(); } return atom.toString(); }
/** * Reads a quoted string value from the request. */ protected String consumeQuoted(ImapRequestLineReader request) throws ProtocolException { // The 1st character must be '"' consumeChar(request, '"'); StringBuilder quoted = new StringBuilder(); char next = request.nextChar(); while (next != '"') { if (next == '\\') { request.consume(); next = request.nextChar(); if (!isQuotedSpecial(next)) { throw new ProtocolException("Invalid escaped character in quote: '" + next + '\''); } } quoted.append(next); request.consume(); next = request.nextChar(); } consumeChar(request, '"'); return quoted.toString(); }
/** * Moves the request line reader to end of the line, checking that no non-space * character are found. * * @throws ProtocolException If more non-space tokens are found in this line, * or the end-of-file is reached. */ public void eol() throws ProtocolException { char next = nextChar(); // Ignore trailing spaces. while (next == ' ') { consume(); next = nextChar(); } // handle DOS and unix end-of-lines if (next == '\r') { consume(); next = nextChar(); } // Check if we found extra characters. if (next != '\n') { throw new ProtocolException("Expected end-of-line, found more character(s): "+next); } dumpLine(); }
/** * Reads the next regular, non-space character in the current line. Spaces are skipped * over, but end-of-line characters will cause a {@link ProtocolException} to be thrown. * This method will continue to return * the same character until the {@link #consume()} method is called. * * @return The next non-space character. * @throws ProtocolException If the end-of-line or end-of-stream is reached. */ public char nextWordChar() throws ProtocolException { char next = nextChar(); while (next == ' ') { consume(); next = nextChar(); } if (next == '\r' || next == '\n') { throw new ProtocolException("Missing argument."); } return next; }
protected byte[] consumeLiteralAsBytes(ImapRequestLineReader request) throws ProtocolException { // The 1st character must be '{' consumeChar(request, '{'); StringBuilder digits = new StringBuilder(); char next = request.nextChar(); while (next != '}' && next != '+') { digits.append(next); request.consume(); next = request.nextChar(); } // If the number is *not* suffixed with a '+', we *are* using a synchronized literal, // and we need to send command continuation request before reading data. boolean synchronizedLiteral = true; // '+' indicates a non-synchronized literal (no command continuation request) if (next == '+') { synchronizedLiteral = false; consumeChar(request, '+'); } // Consume the '}' and the newline consumeChar(request, '}'); consumeCRLF(request); if (synchronizedLiteral) { request.commandContinuationRequest(); } int size = Integer.parseInt(digits.toString()); byte[] buffer = new byte[size]; request.read(buffer); return buffer; }
sb.append(next); request.consume(); next = request.nextChar(); if (next == '\"') { request.consume(); sb.append(next); next = request.nextChar(); request.consume(); // \n next = request.nextChar(); final Integer capacity = Integer.valueOf(sb.substring(1, sb.length() - 1)); ByteBuffer bb = ByteBuffer.allocate(capacity); while (next != CHR_CR) { request.consume(); // \n sb.append(next); next = request.nextChar();
request.consume(); if (next == ')') { break;