📄 tokenizer.java
字号:
if (sb.length() == 0 && type != QUOTED_STRING) { checkUnbalancedParens(); return current.set(EOF, null); } return current.set(type, sb);}/** * Gets the next token from a tokenizer, ignoring whitespace and comments. * @return The next token in the stream. * @throws TextParseException The input was invalid. * @throws IOException An I/O error occurred. */public Tokenget() throws IOException { return get(false, false);}/** * Returns a token to the stream, so that it will be returned by the next call * to get(). * @throws IllegalStateException There are already ungotten tokens. */public voidunget() { if (ungottenToken) throw new IllegalStateException ("Cannot unget multiple tokens"); if (current.type == EOL) line--; ungottenToken = true;}/** * Gets the next token from a tokenizer and converts it to a string. * @return The next token in the stream, as a string. * @throws TextParseException The input was invalid or not a string. * @throws IOException An I/O error occurred. */public StringgetString() throws IOException { Token next = get(); if (!next.isString()) { throw exception("expected a string"); } return next.value;}/** * Gets the next token from a tokenizer, ensures it is an unquoted string, * and converts it to a string. * @return The next token in the stream, as a string. * @throws TextParseException The input was invalid or not an unquoted string. * @throws IOException An I/O error occurred. */public StringgetIdentifier() throws IOException { Token next = get(); if (next.type != IDENTIFIER) { throw exception("expected an identifier"); } return next.value;}/** * Gets the next token from a tokenizer and converts it to a long. * @return The next token in the stream, as a long. * @throws TextParseException The input was invalid or not a long. * @throws IOException An I/O error occurred. */public longgetLong() throws IOException { String next = getIdentifier(); if (!Character.isDigit(next.charAt(0))) throw exception("expecting an integer"); try { return Long.parseLong(next); } catch (NumberFormatException e) { throw exception("expecting an integer"); }}/** * Gets the next token from a tokenizer and converts it to an unsigned 32 bit * integer. * @return The next token in the stream, as an unsigned 32 bit integer. * @throws TextParseException The input was invalid or not an unsigned 32 * bit integer. * @throws IOException An I/O error occurred. */public longgetUInt32() throws IOException { long l = getLong(); if (l < 0 || l > 0xFFFFFFFFL) throw exception("expecting an 32 bit unsigned integer"); return l;}/** * Gets the next token from a tokenizer and converts it to an unsigned 16 bit * integer. * @return The next token in the stream, as an unsigned 16 bit integer. * @throws TextParseException The input was invalid or not an unsigned 16 * bit integer. * @throws IOException An I/O error occurred. */public intgetUInt16() throws IOException { long l = getLong(); if (l < 0 || l > 0xFFFFL) throw exception("expecting an 16 bit unsigned integer"); return (int) l;}/** * Gets the next token from a tokenizer and converts it to an unsigned 8 bit * integer. * @return The next token in the stream, as an unsigned 8 bit integer. * @throws TextParseException The input was invalid or not an unsigned 8 * bit integer. * @throws IOException An I/O error occurred. */public intgetUInt8() throws IOException { long l = getLong(); if (l < 0 || l > 0xFFL) throw exception("expecting an 8 bit unsigned integer"); return (int) l;}/** * Gets the next token from a tokenizer and parses it as a TTL. * @return The next token in the stream, as an unsigned 32 bit integer. * @throws TextParseException The input was not valid. * @throws IOException An I/O error occurred. * @see TTL */public longgetTTL() throws IOException { String next = getIdentifier(); try { return TTL.parseTTL(next); } catch (NumberFormatException e) { throw exception("expecting a valid TTL value"); }}/** * Gets the next token from a tokenizer and parses it as if it were a TTL. * @return The next token in the stream, as an unsigned 32 bit integer. * @throws TextParseException The input was not valid. * @throws IOException An I/O error occurred. * @see TTL */public longgetTTLLike() throws IOException { String next = getIdentifier(); try { return TTL.parse(next, false); } catch (NumberFormatException e) { throw exception("expecting a valid TTL-like value"); }}/** * Gets the next token from a tokenizer and converts it to a name. * @param origin The origin to append to relative names. * @return The next token in the stream, as a name. * @throws TextParseException The input was invalid or not a valid name. * @throws IOException An I/O error occurred. * @throws RelativeNameException The parsed name was relative, even with the * origin. * @see Name */public NamegetName(Name origin) throws IOException { try { Name name = Name.fromString(getIdentifier(), origin); if (!name.isAbsolute()) throw new RelativeNameException(name); return name; } catch (TextParseException e) { throw exception(e.getMessage()); }}/** * Gets the next token from a tokenizer, which must be an EOL or EOF. * @throws TextParseException The input was invalid or not an EOL or EOF token. * @throws IOException An I/O error occurred. */public voidgetEOL() throws IOException { Token next = get(); if (next.type != EOL && next.type != EOF) { throw exception("expecting EOL or EOF"); }}/** * Returns a concatenation of the remaining strings from a Tokenizer. */private StringremainingStrings() throws IOException { StringBuffer sb = null; while (true) { Tokenizer.Token t = get(); if (!t.isString()) break; if (sb == null) sb = new StringBuffer(); sb.append(t.value); } unget(); if (sb == null) return null; return sb.toString();}/** * Gets the remaining string tokens until an EOL/EOF is seen, concatenates * them together, and converts the base64 encoded data to a byte array. * @param required If true, an exception will be thrown if no strings remain; * otherwise null be be returned. * @return The byte array containing the decoded strings, or null if there * were no strings to decode. * @throws TextParseException The input was invalid. * @throws IOException An I/O error occurred. */public byte []getBase64(boolean required) throws IOException { String s = remainingStrings(); if (s == null) { if (required) throw exception("expected base64 encoded string"); else return null; } byte [] array = base64.fromString(s); if (array == null) throw exception("invalid base64 encoding"); return array;}/** * Gets the remaining string tokens until an EOL/EOF is seen, concatenates * them together, and converts the base64 encoded data to a byte array. * @return The byte array containing the decoded strings, or null if there * were no strings to decode. * @throws TextParseException The input was invalid. * @throws IOException An I/O error occurred. */public byte []getBase64() throws IOException { return getBase64(false);}/** * Gets the remaining string tokens until an EOL/EOF is seen, concatenates * them together, and converts the hex encoded data to a byte array. * @param required If true, an exception will be thrown if no strings remain; * otherwise null be be returned. * @return The byte array containing the decoded strings, or null if there * were no strings to decode. * @throws TextParseException The input was invalid. * @throws IOException An I/O error occurred. */public byte []getHex(boolean required) throws IOException { String s = remainingStrings(); if (s == null) { if (required) throw exception("expected hex encoded string"); else return null; } byte [] array = base16.fromString(s); if (array == null) throw exception("invalid hex encoding"); return array;}/** * Gets the remaining string tokens until an EOL/EOF is seen, concatenates * them together, and converts the hex encoded data to a byte array. * @return The byte array containing the decoded strings, or null if there * were no strings to decode. * @throws TextParseException The input was invalid. * @throws IOException An I/O error occurred. */public byte []getHex() throws IOException { return getHex(false);}/** * Creates an exception which includes the current state in the error message * @param s The error message to include. * @return The exception to be thrown */public TextParseExceptionexception(String s) { return new TokenizerException(filename, line, s);}/** * Closes any files opened by this tokenizer. */public voidclose() { if (wantClose) { try { is.close(); } catch (IOException e) { } }}protected voidfinalize() { close();}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -