目录
简介
各种字段和匹配的正则表达式
13个构造函数,方法toCharset,3个makeReadable,toDecoder
方法saveState,2个revertState,2个cacheResult,clearCaches,getCachedResult,useTypeCache
方法readInput,makeSpace,translateSavedIndexes,throwFor
方法hasTokenInBuffer,getCompleteTokenInBuffer,findPatternInBuffer,matchPatternInBuffer,ensureOpen
方法close,ioException,delimiter,2个useDelimiter,locale,useLocale,radix,useRadix,setRadix,match,toString
方法3个hasNext,3个next,remove,hasNextLine,nextLine,2个findInLine,2个findWithinHorizon,2个skip
方法hasNextXXX,nextXXX(boolean,byte,short,int,long,float,double,biginteger,bigdecimal),reset
简介
package java.util;
import java.nio.file.Path;
import java.nio.file.Files;
import java.util.regex.*;
import java.io.*;
import java.math.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.text.*;
import java.util.Locale;
import sun.misc.LRUCache;
/**
* 一个简单的文本扫描器,可以解析使用正则表达式的基本类型和字符串。
*
* <p>扫描程序使用限制符模式将其输入分解为标记,默认情况下匹配空格。
* 然后,可以使用各种next方法将结果标记转换为不同类型的值。
*
* <p>例如,这段代码允许用户从System.in中读取一个数字。
* <blockquote><pre>{@code
* Scanner sc = new Scanner(System.in);
* int i = sc.nextInt();
* }</pre></blockquote>
*
* <p>作为另一个例子,这段代码允许从文件myNumbers中得到long类型:
* <blockquote><pre>{@code
* Scanner sc = new Scanner(new File("myNumbers"));
* while (sc.hasNextLong()) {
* long aLong = sc.nextLong();
* }
* }</pre></blockquote>
*
* <p>扫描程序还可以使用除空白之外的分隔符。
* 这个例子从一个字符串中读入几个条目:
* <blockquote><pre>{@code
* String input = "1 fish 2 fish red fish blue fish";
* Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
* System.out.println(s.nextInt());
* System.out.println(s.nextInt());
* System.out.println(s.next());
* System.out.println(s.next());
* s.close();
* }</pre></blockquote>
* <p>
* 输出如下:
* <blockquote><pre>{@code
* 1
* 2
* red
* blue
* }</pre></blockquote>
*
* <p>这段代码可以生成相同的输出,它使用一个正则表达式来一次解析所有四个令牌:
* <blockquote><pre>{@code
* String input = "1 fish 2 fish red fish blue fish";
* Scanner s = new Scanner(input);
* s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
* MatchResult result = s.match();
* for (int i=1; i<=result.groupCount(); i++)
* System.out.println(result.group(i));
* s.close();
* }</pre></blockquote>
*
* <p>扫描器使用的默认空格分隔符是java.lang. characters.iswhitespace()识别的。
* reset方法将把扫描程序定界符的值重置为默认的空白定界符,而不管它以前是否被更改过。
*
* <p>扫描操作可能阻塞等待输入。
*
* <p>next和hasNext方法及其原语类型的伴生方法(如nextInt和hasNextInt),
* 首先跳过任何匹配分割符的输入,然后尝试返回下一个标记。
* hasnext和next方法都可能阻塞,等待进一步的输入。
* 一个hasNext方法的阻塞与它关联的next方法是否阻塞,没有关联。
*
* <p> findInLine、findWithinHorizon和skip方法的操作独立于分隔符模式。
* 这些方法将尝试匹配指定的模式,而不考虑输入中的分隔符,因此可用于与分隔符不相关的特殊情况。
* 这些方法可能阻塞,等待更多的输入。
*
* <p>当扫描程序抛出InputMismatchException异常时,
* 扫描程序将不会传递引起该异常的令牌,因此可以通过其他方法重新获取或跳过它。
*
* <p>根据分隔模式的类型,可能会返回空标记。
* 例如,模式“\\s+”将返回不为空的标记,因为它匹配分隔符的多个实例。
* 分隔模式“\\s”可以返回空标记,因为它一次只传递一个空间。
*
* <p> 扫描器可以从任何实现java.lang.Readable的对象读取文本。可读的接口。
* 如果调用依赖的readable的read方法抛出IOException,那扫描器假定已经到达了输入的末端。
* 底层可读抛出的最近的IOException可以通过IOException方法检索。
*
* <p>When a <code>Scanner</code> is closed, it will close its input source
* if the source implements the {@link java.io.Closeable} interface.
*
* 当扫描器关闭时,如果源实现了Closeable,它将关闭它的输入源。
*
* <p>扫描器在没有外部同步的情况下对多线程使用是不安全的。
*
* <p>除非另有说明,将null参数传递给扫描器的任何方法都会导致抛出NullPointerException。
*
* <p>扫描器默认将数字解释为十进制,除非使用useRadix方法设置了不同的基数。
* reset方法将把扫描器的基数值重置为10,而不管它之前是否被更改过。
*
* <h3> <a name="localized-numbers">Localized numbers</a> </h3>
*
* <p> 该类的实例能够以标准格式和扫描器区域设置的格式扫描数字。
* 扫描仪的初始区域设置是由locale.getDefault方法返回的值;
* 它可以通过useLocale方法进行更改。
* reset方法将把扫描程序的区域设置的值重置为初始的区域设置,而不管它之前是否被更改过。
*
* <p>本地化格式由以下参数定义,对于特定的语言环境,
* 这些参数取自该语言环境的DecimalFormat对象df及其和DecimalFormatSymbols对象dfs。
*
* <blockquote><dl>
* <dt><i>LocalGroupSeparator </i>
* <dd>The character used to separate thousands groups,
* <i>i.e.,</i> <tt>dfs.</tt>{@link
* java.text.DecimalFormatSymbols#getGroupingSeparator
* getGroupingSeparator()}
* <dt><i>LocalDecimalSeparator </i>
* <dd>The character used for the decimal point,
* <i>i.e.,</i> <tt>dfs.</tt>{@link
* java.text.DecimalFormatSymbols#getDecimalSeparator
* getDecimalSeparator()}
* <dt><i>LocalPositivePrefix </i>
* <dd>The string that appears before a positive number (may
* be empty), <i>i.e.,</i> <tt>df.</tt>{@link
* java.text.DecimalFormat#getPositivePrefix
* getPositivePrefix()}
* <dt><i>LocalPositiveSuffix </i>
* <dd>The string that appears after a positive number (may be
* empty), <i>i.e.,</i> <tt>df.</tt>{@link
* java.text.DecimalFormat#getPositiveSuffix
* getPositiveSuffix()}
* <dt><i>LocalNegativePrefix </i>
* <dd>The string that appears before a negative number (may
* be empty), <i>i.e.,</i> <tt>df.</tt>{@link
* java.text.DecimalFormat#getNegativePrefix
* getNegativePrefix()}
* <dt><i>LocalNegativeSuffix </i>
* <dd>The string that appears after a negative number (may be
* empty), <i>i.e.,</i> <tt>df.</tt>{@link
* java.text.DecimalFormat#getNegativeSuffix
* getNegativeSuffix()}
* <dt><i>LocalNaN </i>
* <dd>The string that represents not-a-number for
* floating-point values,
* <i>i.e.,</i> <tt>dfs.</tt>{@link
* java.text.DecimalFormatSymbols#getNaN
* getNaN()}
* <dt><i>LocalInfinity </i>
* <dd>The string that represents infinity for floating-point
* values, <i>i.e.,</i> <tt>dfs.</tt>{@link
* java.text.DecimalFormatSymbols#getInfinity
* getInfinity()}
* </dl></blockquote>
*
* <h4> <a name="number-syntax">Number syntax</a> </h4>
*
* <p> 这个类的实例可以解析为数字的字符串是按照下面的正则表达式语法指定的,
* 其中Rmax是所使用的基数中的最高数字(例如,Rmax以10为基数是9)。
*
* <dl>
* <dt><i>NonAsciiDigit</i>:
* <dd>A non-ASCII character c for which
* {@link java.lang.Character#isDigit Character.isDigit}<tt>(c)</tt>
* returns true
*
* <dt><i>Non0Digit</i>:
* <dd><tt>[1-</tt><i>Rmax</i><tt>] | </tt><i>NonASCIIDigit</i>
*
* <dt><i>Digit</i>:
* <dd><tt>[0-</tt><i>Rmax</i><tt>] | </tt><i>NonASCIIDigit</i>
*
* <dt><i>GroupedNumeral</i>:
* <dd><tt>( </tt><i>Non0Digit</i>
* <i>Digit</i><tt>?
* </tt><i>Digit</i><tt>?</tt>
* <dd> <tt>( </tt><i>LocalGroupSeparator</i>
* <i>Digit</i>
* <i>Digit</i>
* <i>Digit</i><tt> )+ )</tt>
*
* <dt><i>Numeral</i>:
* <dd><tt>( ( </tt><i>Digit</i><tt>+ )
* | </tt><i>GroupedNumeral</i><tt> )</tt>
*
* <dt><a name="Integer-regex"><i>Integer</i>:</a>
* <dd><tt>( [-+]? ( </tt><i>Numeral</i><tt>
* ) )</tt>
* <dd><tt>| </tt><i>LocalPositivePrefix</i> <i>Numeral</i>
* <i>LocalPositiveSuffix</i>
* <dd><tt>| </tt><i>LocalNegativePrefix</i> <i>Numeral</i>
* <i>LocalNegativeSuffix</i>
*
* <dt><i>DecimalNumeral</i>:
* <dd><i>Numeral</i>
* <dd><tt>| </tt><i>Numeral</i>
* <i>LocalDecimalSeparator</i>
* <i>Digit</i><tt>*</tt>
* <dd><tt>| </tt><i>LocalDecimalSeparator</i>
* <i>Digit</i><tt>+</tt>
*
* <dt><i>Exponent</i>:
* <dd><tt>( [eE] [+-]? </tt><i>Digit</i><tt>+ )</tt>
*
* <dt><a name="Decimal-regex"><i>Decimal</i>:</a>
* <dd><tt>( [-+]? </tt><i>DecimalNumeral</i>
* <i>Exponent</i><tt>? )</tt>
* <dd><tt>| </tt><i>LocalPositivePrefix</i>
* <i>DecimalNumeral</i>
* <i>LocalPositiveSuffix</i>
* <i>Exponent</i><tt>?</tt>
* <dd><tt>| </tt><i>LocalNegativePrefix</i>
* <i>DecimalNumeral</i>
* <i>LocalNegativeSuffix</i>
* <i>Exponent</i><tt>?</tt>
*
* <dt><i>HexFloat</i>:
* <dd><tt>[-+]? 0[xX][0-9a-fA-F]*\.[0-9a-fA-F]+
* ([pP][-+]?[0-9]+)?</tt>
*
* <dt><i>NonNumber</i>:
* <dd><tt>NaN
* | </tt><i>LocalNan</i><tt>
* | Infinity
* | </tt><i>LocalInfinity</i>
*
* <dt><i>SignedNonNumber</i>:
* <dd><tt>( [-+]? </tt><i>NonNumber</i><tt> )</tt>
* <dd><tt>| </tt><i>LocalPositivePrefix</i>
* <i>NonNumber</i>
* <i>LocalPositiveSuffix</i>
* <dd><tt>| </tt><i>LocalNegativePrefix</i>
* <i>NonNumber</i>
* <i>LocalNegativeSuffix</i>
*
* <dt><a name="Float-regex"><i>Float</i></a>:
* <dd><i>Decimal</i>
* <tt>| </tt><i>HexFloat</i>
* <tt>| </tt><i>SignedNonNumber</i>
*
* </dl>
* <p>在上面的正则表达式中,空格不重要。
*
* @since 1.5
*/
public final class Scanner implements Iterator<String>, Closeable
各种字段和匹配的正则表达式
// 用于保存输入的内部缓冲区
private CharBuffer buf;
// 内部字符缓冲区的大小
private static final int BUFFER_SIZE = 1024; // change to 1024;
// Scanner当前持有的缓冲区的索引
private int position;
// 用于查找分隔符的内部匹配器
private Matcher matcher;
// 用于分隔标记的模式
private Pattern delimPattern;
// 在最后一个 hasNext操作中找到的模式
private Pattern hasNextPattern;
// 最后一个 hasNext操作后的位置
private int hasNextPosition;
// 最后一个 hasNext操作后的结果
private String hasNextResult;
// 输入源
private Readable source;
// 如果source已经完成,则Boolean为true
private boolean sourceClosed = false;
// 表示需要更多输入的布尔值
private boolean needInput = false;
// 指示是否跳过delim操作的布尔值
private boolean skipped = false;
// 扫描器可以退回的存储位置
private int savedScannerPosition = -1;
// 所扫描的最后一个原语类型的缓存
private Object typeCache = null;
// 指示匹配结果是否可用的布尔值
private boolean matchValid = false;
// 指示此扫描器是否已关闭的布尔值
private boolean closed = false;
// 这个扫描器使用的当前基数
private int radix = 10;
// 这个扫描器的默认基数
private int defaultRadix = 10;
// 此扫描程序使用的区域设置
private Locale locale = null;
// 最近使用的几个模式的缓存
private LRUCache<String,Pattern> patternCache =
new LRUCache<String,Pattern>(7) {
protected Pattern create(String s) {
return Pattern.compile(s);
}
protected boolean hasName(Pattern p, String s) {
return p.pattern().equals(s);
}
};
// 最后一个遇到的IOException的持有者
private IOException lastException;
// 一种用于java空格的模式
private static Pattern WHITESPACE_PATTERN = Pattern.compile(
"\\p{javaWhitespace}+");
// 任何标记的模式
private static Pattern FIND_ANY_PATTERN = Pattern.compile("(?s).*");
// 非ascii数字的模式
private static Pattern NON_ASCII_DIGIT = Pattern.compile(
"[\\p{javaDigit}&&[^0-9]]");
// 字段和方法来支持扫描基元类型
/**
* 用于扫描数字的区域设置相关值
*/
private String groupSeparator = "\\,";
private String decimalSeparator = "\\.";
private String nanString = "NaN";
private String infinityString = "Infinity";
private String positivePrefix = "";
private String negativePrefix = "\\-";
private String positiveSuffix = "";
private String negativeSuffix = "";
/**
* 字段和匹配布尔值的访问器方法
*/
private static volatile Pattern boolPattern;
private static final String BOOLEAN_PATTERN = "true|false";
private static Pattern boolPattern() {
Pattern bp = boolPattern;
if (bp == null)
boolPattern = bp = Pattern.compile(BOOLEAN_PATTERN,
Pattern.CASE_INSENSITIVE);
return bp;
}
/**
* 匹配bytes、short、int和long类型的字段和方法
*/
private Pattern integerPattern;
private String digits = "0123456789abcdefghijklmnopqrstuvwxyz";
private String non0Digit = "[\\p{javaDigit}&&[^0]]";
private int SIMPLE_GROUP_INDEX = 5;
private String buildIntegerPatternString() {
String radixDigits = digits.substring(0, radix);
// \\p{javaDigit} is not guaranteed to be appropriate
// here but what can we do? The final authority will be
// whatever parse method is invoked, so ultimately the
// Scanner will do the right thing
String digit = "((?i)["+radixDigits+"]|\\p{javaDigit})";
String groupedNumeral = "("+non0Digit+digit+"?"+digit+"?("+
groupSeparator+digit+digit+digit+")+)";
// digit++ is the possessive form which is necessary for reducing
// backtracking that would otherwise cause unacceptable performance
String numeral = "(("+ digit+"++)|"+groupedNumeral+")";
String javaStyleInteger = "([-+]?(" + numeral + "))";
String negativeInteger = negativePrefix + numeral + negativeSuffix;
String positiveInteger = positivePrefix + numeral + positiveSuffix;
return "("+ javaStyleInteger + ")|(" +
positiveInteger + ")|(" +
negativeInteger + ")";
}
private Pattern integerPattern() {
if (integerPattern == null) {
integerPattern = patternCache.forName(buildIntegerPatternString());
}
return integerPattern;
}
/**
* 字段和匹配行分隔符的访问器方法
*/
private static volatile Pattern separatorPattern;
private static volatile Pattern linePattern;
private static final String LINE_SEPARATOR_PATTERN =
"\r\n|[\n\r\u2028\u2029\u0085]";
private static final String LINE_PATTERN = ".*("+LINE_SEPARATOR_PATTERN+")|.+$";
private static Pattern separatorPattern() {
Pattern sp = separatorPattern;
if (sp == null)
separatorPattern = sp = Pattern.compile(LINE_SEPARATOR_PATTERN);
return sp;
}
private static Pattern linePattern() {
Pattern lp = linePattern;
if (lp == null)
linePattern = lp = Pattern.compile(LINE_PATTERN);
return lp;
}
/**
* 匹配浮点数和双精度数的字段和方法
*/
private Pattern floatPattern;
private Pattern decimalPattern;
private void buildFloatAndDecimalPattern() {
// \\p{javaDigit} may not be perfect, see above
String digit = "([0-9]|(\\p{javaDigit}))";
String exponent = "([eE][+-]?"+digit+"+)?";
String groupedNumeral = "("+non0Digit+digit+"?"+digit+"?("+
groupSeparator+digit+digit+digit+")+)";
// Once again digit++ is used for performance, as above
String numeral = "(("+digit+"++)|"+groupedNumeral+")";
String decimalNumeral = "("+numeral+"|"+numeral +
decimalSeparator + digit + "*+|"+ decimalSeparator +
digit + "++)";
String nonNumber = "(NaN|"+nanString+"|Infinity|"+
infinityString+")";
String positiveFloat = "(" + positivePrefix + decimalNumeral +
positiveSuffix + exponent + ")";
String negativeFloat = "(" + negativePrefix + decimalNumeral +
negativeSuffix + exponent + ")";
String decimal = "(([-+]?" + decimalNumeral + exponent + ")|"+
positiveFloat + "|" + negativeFloat + ")";
String hexFloat =
"[-+]?0[xX][0-9a-fA-F]*\\.[0-9a-fA-F]+([pP][-+]?[0-9]+)?";
String positiveNonNumber = "(" + positivePrefix + nonNumber +
positiveSuffix + ")";
String negativeNonNumber = "(" + negativePrefix + nonNumber +
negativeSuffix + ")";
String signedNonNumber = "(([-+]?"+nonNumber+")|" +
positiveNonNumber + "|" +
negativeNonNumber + ")";
floatPattern = Pattern.compile(decimal + "|" + hexFloat + "|" +
signedNonNumber);
decimalPattern = Pattern.compile(decimal);
}
private Pattern floatPattern() {
if (floatPattern == null) {
buildFloatAndDecimalPattern();
}
return floatPattern;
}
private Pattern decimalPattern() {
if (decimalPattern == null) {
buildFloatAndDecimalPattern();
}
return decimalPattern;
}
13个构造函数,方法toCharset,3个makeReadable,toDecoder
// Constructors
/**
* 构造一个扫描器,该扫描器返回由指定模式分隔的指定源所扫描的值。
*
* @param source A character source implementing the Readable interface
* @param pattern A delimiting pattern
*/
private Scanner(Readable source, Pattern pattern) {
assert source != null : "source should not be null";
assert pattern != null : "pattern should not be null";
// 设置源和分隔符
this.source = source;
delimPattern = pattern;
// 配置缓存
buf = CharBuffer.allocate(BUFFER_SIZE);
buf.limit(0);
// 分隔符匹配缓存
matcher = delimPattern.matcher(buf);
matcher.useTransparentBounds(true);
matcher.useAnchoringBounds(false);
// 设置语言
useLocale(Locale.getDefault(Locale.Category.FORMAT));
}
/**
* 构造一个新的扫描程序,该程序生成从指定源扫描的值。
*
* @param source A character source implementing the {@link Readable}
* interface
*/
public Scanner(Readable source) {
// 用空格作为分隔符
this(Objects.requireNonNull(source, "source"), WHITESPACE_PATTERN);
}
/**
* 构造一个新的扫描程序,该扫描程序产生从指定输入流扫描的值。
* 流中的字节将使用底层平台的默认字符集转换为字符。
*
* @param source An input stream to be scanned
*/
public Scanner(InputStream source) {
this(new InputStreamReader(source), WHITESPACE_PATTERN);
}
/**
* 构造一个新的扫描程序,该扫描程序产生从指定输入流扫描的值。
* 流中的字节使用指定的字符集转换为字符。
*
* @param source An input stream to be scanned
* @param charsetName The encoding type used to convert bytes from the
* stream into characters to be scanned
* @throws IllegalArgumentException if the specified character set
* does not exist
*/
public Scanner(InputStream source, String charsetName) {
// return new InputStreamReader(source, charset);
this(makeReadable(Objects.requireNonNull(source, "source"), toCharset(charsetName)),
WHITESPACE_PATTERN);
}
/**
* 返回给定字符集名称的字符集对象。
* @throws NullPointerException is csn is null
* @throws IllegalArgumentException if the charset is not supported
*/
private static Charset toCharset(String csn) {
Objects.requireNonNull(csn, "charsetName");
try {
return Charset.forName(csn);
} catch (IllegalCharsetNameException|UnsupportedCharsetException e) {
// IllegalArgumentException should be thrown
throw new IllegalArgumentException(e);
}
}
private static Readable makeReadable(InputStream source, Charset charset) {
// inputstream 变成 reader
return new InputStreamReader(source, charset);
}
/**
* 构造一个新的扫描程序,该程序生成从指定文件扫描的值。
* 文件中的字节将使用底层平台的默认字符集转换为字符。
*
* @param source A file to be scanned
* @throws FileNotFoundException if source is not found
*/
public Scanner(File source) throws FileNotFoundException {
// file变成FileInputStream变成channel变成ReadableByteChannel
this((ReadableByteChannel)(new FileInputStream(source).getChannel()));
}
/**
* 构造一个新的扫描程序,该程序生成从指定文件扫描的值。
* 文件中的字节将使用指定的字符集转换为字符。
*
* @param source A file to be scanned
* @param charsetName The encoding type used to convert bytes from the file
* into characters to be scanned
* @throws FileNotFoundException if source is not found
* @throws IllegalArgumentException if the specified encoding is
* not found
*/
public Scanner(File source, String charsetName)
throws FileNotFoundException
{
this(Objects.requireNonNull(source), toDecoder(charsetName));
}
private Scanner(File source, CharsetDecoder dec)
throws FileNotFoundException
{
this(makeReadable((ReadableByteChannel)(new FileInputStream(source).getChannel()), dec));
}
private static CharsetDecoder toDecoder(String charsetName) {
Objects.requireNonNull(charsetName, "charsetName");
try {
return Charset.forName(charsetName).newDecoder();
} catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
throw new IllegalArgumentException(charsetName);
}
}
private static Readable makeReadable(ReadableByteChannel source,
CharsetDecoder dec) {
return Channels.newReader(source, dec, -1);
}
/**
* 构造一个新的扫描程序,该程序生成从指定文件扫描的值。
* 文件中的字节将使用底层平台的默认字符集转换为字符。
*
* @param source
* the path to the file to be scanned
* @throws IOException
* if an I/O error occurs opening source
*
* @since 1.7
*/
public Scanner(Path source)
throws IOException
{
this(Files.newInputStream(source));
}
/**
* 构造一个新的扫描程序,该程序生成从指定文件扫描的值。
* 文件中的字节将使用指定的字符集转换为字符。
*
* @param source
* the path to the file to be scanned
* @param charsetName
* The encoding type used to convert bytes from the file
* into characters to be scanned
* @throws IOException
* if an I/O error occurs opening source
* @throws IllegalArgumentException
* if the specified encoding is not found
* @since 1.7
*/
public Scanner(Path source, String charsetName) throws IOException {
this(Objects.requireNonNull(source), toCharset(charsetName));
}
private Scanner(Path source, Charset charset) throws IOException {
this(makeReadable(Files.newInputStream(source), charset));
}
/**
* 构造一个新的扫描程序,该程序生成从指定字符串扫描的值。
*
* @param source A string to scan
*/
public Scanner(String source) {
this(new StringReader(source), WHITESPACE_PATTERN);
}
/**
* 构造一个新的扫描程序,该程序生成从指定通道扫描的值。
* 源文件中的字节将使用底层平台的默认字符集转换为字符。
*
* @param source A channel to scan
*/
public Scanner(ReadableByteChannel source) {
this(makeReadable(Objects.requireNonNull(source, "source")),
WHITESPACE_PATTERN);
}
private static Readable makeReadable(ReadableByteChannel source) {
return makeReadable(source, Charset.defaultCharset().newDecoder());
}
/**
* 构造一个新的扫描程序,该程序生成从指定通道扫描的值。
* 来自源的字节使用指定的字符集转换为字符。
*
* @param source A channel to scan
* @param charsetName The encoding type used to convert bytes from the
* channel into characters to be scanned
* @throws IllegalArgumentException if the specified character set
* does not exist
*/
public Scanner(ReadableByteChannel source, String charsetName) {
this(makeReadable(Objects.requireNonNull(source, "source"), toDecoder(charsetName)),
WHITESPACE_PATTERN);
}
方法saveState,2个revertState,2个cacheResult,clearCaches,getCachedResult,useTypeCache
// 用于支持扫描的私有原语
private void saveState() {
// position赋值给savedScannerPosition
savedScannerPosition = position;
}
private void revertState() {
//savedScannerPosition反过来赋值给position
// 重置savedScannerPosition和skipped
this.position = savedScannerPosition;
savedScannerPosition = -1;
skipped = false;
}
private boolean revertState(boolean b) {
this.position = savedScannerPosition;
savedScannerPosition = -1;
skipped = false;
return b;
}
private void cacheResult() {
// 保存matcher的匹配结果
hasNextResult = matcher.group();
hasNextPosition = matcher.end();
hasNextPattern = matcher.pattern();
}
private void cacheResult(String result) {
hasNextResult = result;
hasNextPosition = matcher.end();
hasNextPattern = matcher.pattern();
}
// 清除常规缓存和类型缓存
private void clearCaches() {
hasNextPattern = null;
typeCache = null;
}
// 清除常规缓存和类型缓存
private String getCachedResult() {
// hasNextPosition赋值给position
position = hasNextPosition;
hasNextPattern = null;
typeCache = null;
return hasNextResult;
}
// 清除常规缓存和类型缓存
private void useTypeCache() {
if (closed)
throw new IllegalStateException("Scanner closed");
position = hasNextPosition;
hasNextPattern = null;
typeCache = null;
}
方法readInput,makeSpace,translateSavedIndexes,throwFor
// 尝试阅读更多的输入信息。可能会阻止。
private void readInput() {
// 先保证缓冲区有足够空间
if (buf.limit() == buf.capacity())
makeSpace();
// 准备接受信息
int p = buf.position();
buf.position(buf.limit());
buf.limit(buf.capacity());
int n = 0;
try {
// source数据读入buf
n = source.read(buf);
} catch (IOException ioe) {
lastException = ioe;
n = -1;
}
if (n == -1) {
sourceClosed = true;
needInput = false;
}
if (n > 0)
needInput = false;
// 恢复当前位置和限制,为了读取缓冲区
buf.limit(buf.position());
buf.position(p);
}
// 在这个方法被调用之后,要么会有一个异常,要么会在缓冲区中有空间
private boolean makeSpace() {
clearCaches();
int offset = savedScannerPosition == -1 ?
position : savedScannerPosition;
buf.position(offset);
// 通过压缩缓冲区,获得空间
if (offset > 0) {
// 压缩buf,重新设置scanner内部的position
buf.compact();
translateSavedIndexes(offset);
position -= offset;
buf.flip();
return true;
}
// 通过扩大缓冲区,获得空间
int newSize = buf.capacity() * 2;
CharBuffer newBuf = CharBuffer.allocate(newSize);
newBuf.put(buf);
newBuf.flip();
translateSavedIndexes(offset);
position -= offset;
buf = newBuf;
matcher.reset(buf);
return true;
}
// 当缓冲区压缩/重新分配发生时,必须适当地修改保存的索引
private void translateSavedIndexes(int offset) {
if (savedScannerPosition != -1)
savedScannerPosition -= offset;
}
// 如果我们在输入的末尾,那么NoSuchElement; 如果还有输入,那么输入不匹配
private void throwFor() {
skipped = false;
if ((sourceClosed) && (position == buf.limit()))
throw new NoSuchElementException();
else
throw new InputMismatchException();
}
方法hasTokenInBuffer,getCompleteTokenInBuffer,findPatternInBuffer,matchPatternInBuffer,ensureOpen
// 如果缓冲区中有完整标记或部分标记,则返回true。
// 不需要找到一个完整的令牌,因为部分令牌意味着会有另一个有或没有更多输入的令牌。
private boolean hasTokenInBuffer() {
matchValid = false;
matcher.usePattern(delimPattern);
matcher.region(position, buf.limit());
// 首先跳过分隔符
if (matcher.lookingAt())
position = matcher.end();
// 如果我们在缓冲区的末尾,那么缓冲区中就没有更多的标记
if (position == buf.limit())
return false;
return true;
}
/**
* 返回与指定模式匹配的“完整令牌”。
* 如果被边界包围,则令牌完整;
* 部分标记以delims为前缀,但不以它们为后缀。
*
* 位置被提升到完整标记的末尾。
*
* Pattern为null意味着接受所有的标记
*
* 返回一下3种
*
* 1.找到的合法字符串
* 2.返回null,needInput=false,没有找到
* 3.返回null,needInput=true意味着readInput后要再次尝试readInput
*/
private String getCompleteTokenInBuffer(Pattern pattern) {
matchValid = false;
// 首先跳过分隔符
matcher.usePattern(delimPattern);
if (!skipped) { // 只强制执行一个跳跃式的分隔符
matcher.region(position, buf.limit());
if (matcher.lookingAt()) {
// 如果更多的输入可以扩展分隔符,那么我们必须等待更多的输入
if (matcher.hitEnd() && !sourceClosed) {
needInput = true;
return null;
}
// 他们是完整的,匹配器应该跳过他们
skipped = true;
position = matcher.end();
}
}
// 如果我们在缓冲区的末尾,那么缓冲区中就没有更多的标记
if (position == buf.limit()) {
if (sourceClosed)
return null;
needInput = true;
return null;
}
// 必须寻找下一个目标。此时,简单地尝试匹配模式可能会找到一个匹配,
// 但它可能不是第一个最长的匹配,因为缺少输入,或者它可能匹配部分标记而不是整个标记。
// 然后找下一个分隔符
matcher.region(position, buf.limit());
boolean foundNextDelim = matcher.find();
if (foundNextDelim && (matcher.end() == position)) {
// 零长度分隔符匹配;
// 我们应该使用超过零长度匹配的自动前进来找到下一个;
// 否则我们只是找到了我们刚刚跳过的那个
foundNextDelim = matcher.find();
}
if (foundNextDelim) {
// 在很少的情况下,更多的输入可能导致匹配失败,并且有更多的输入,我们必须等待更多的输入。
// 注意,只要比赛不会结束,击中终点是可以的。这是我们想要确定的下一个谬误的开始,我们不关心它们是否可能进一步扩展。
if (matcher.requireEnd() && !sourceClosed) {
needInput = true;
return null;
}
int tokenEnd = matcher.start();
// 有一个完整的标记
if (pattern == null) {
// 必须继续使用match来提供有效的MatchResult
pattern = FIND_ANY_PATTERN;
}
// 尝试匹配所需的模式
matcher.usePattern(pattern);
matcher.region(position, tokenEnd);
if (matcher.matches()) {
String s = matcher.group();
position = matcher.end();
return s;
} else { // 完整的标记,但不匹配
return null;
}
}
// 如果我们找不到下一个delims,但是没有更多的输入,那么我们可以将余数作为一个整体标记处理
if (sourceClosed) {
if (pattern == null) {
// Must continue with match to provide valid MatchResult
pattern = FIND_ANY_PATTERN;
}
// 令牌;匹配这里的模式或抛出
matcher.usePattern(pattern);
matcher.region(position, buf.limit());
if (matcher.matches()) {
String s = matcher.group();
position = matcher.end();
return s;
}
// 最后一块不匹配
return null;
}
// 缓冲区中有部分标记;必须阅读更多来完成它吗
needInput = true;
return null;
}
// 在水平之前的缓冲区中查找指定的模式。返回指定输入模式的匹配项。
private String findPatternInBuffer(Pattern pattern, int horizon) {
matchValid = false;
matcher.usePattern(pattern);
int bufferLimit = buf.limit();
int horizonLimit = -1;
int searchLimit = bufferLimit;
if (horizon > 0) {
horizonLimit = position + horizon;
if (horizonLimit < bufferLimit)
searchLimit = horizonLimit;
}
matcher.region(position, searchLimit);
if (matcher.find()) {
if (matcher.hitEnd() && (!sourceClosed)) {
// 如果没有击中地平线或真正的终点,比赛可能会更长
if (searchLimit != horizonLimit) {
// 以人工方式击中终点;尽量延长比赛时间
needInput = true;
return null;
}
// 比赛可能会结束,这取决于接下来发生什么
if ((searchLimit == horizonLimit) && matcher.requireEnd()) {
// 罕见情况:我们碰到输入的末尾,它恰好在视界上,而输入的末尾是匹配所必需的。
needInput = true;
return null;
}
}
// 没击中终点,还是击中真实终点,还是击中地平线
position = matcher.end();
return matcher.group();
}
if (sourceClosed)
return null;
// 如果没有指定的视界,或者我们没有搜索
// 指定视界,获取更多的输入
if ((horizon == 0) || (searchLimit != horizonLimit))
needInput = true;
return null;
}
// 返回锚定在当前位置的指定输入模式的匹配
private String matchPatternInBuffer(Pattern pattern) {
matchValid = false;
matcher.usePattern(pattern);
matcher.region(position, buf.limit());
if (matcher.lookingAt()) {
if (matcher.hitEnd() && (!sourceClosed)) {
// 获得更多的输入,然后再试一次
needInput = true;
return null;
}
position = matcher.end();
return matcher.group();
}
if (sourceClosed)
return null;
// 阅读更多内容来寻找模式
needInput = true;
return null;
}
// 抛出扫描器是否关闭
private void ensureOpen() {
if (closed)
throw new IllegalStateException("Scanner closed");
}
方法close,ioException,delimiter,2个useDelimiter,locale,useLocale,radix,useRadix,setRadix,match,toString
// Public methods
/**
* 关闭这个扫描仪。
*
* <p> 如果这个扫描程序还没有关闭,那么如果它的底层可读也实现了java.io.Closeable,
* 然后readable的close方法将被调用。如果这个扫描程序已经关闭,那么调用这个方法将没有效果。
*
* <p>在扫描器关闭后尝试执行搜索操作将导致IllegalStateException。
*
*/
public void close() {
if (closed)
return;
if (source instanceof Closeable) {
try {
((Closeable)source).close();
} catch (IOException ioe) {
lastException = ioe;
}
}
sourceClosed = true;
source = null;
closed = true;
}
/**
* 返回该扫描器底层可读的最后一次抛出的IOException。如果不存在这样的异常,该方法将返回null。
*
* @return the last exception thrown by this scanner's readable
*/
public IOException ioException() {
return lastException;
}
/**
* 返回扫描器当前用于匹配分隔符的模式。
*
* @return this scanner's delimiting pattern.
*/
public Pattern delimiter() {
return delimPattern;
}
/**
* 将此扫描程序的分隔模式设置为指定的模式。
*
* @param pattern A delimiting pattern
* @return this scanner
*/
public Scanner useDelimiter(Pattern pattern) {
delimPattern = pattern;
return this;
}
/**
* 将扫描器的分隔模式设置为由指定字符串构造的模式。
*
* <p> 以useDelimiter(pattern)形式调用这个方法的行为与调用useDelimiter(pattern.compile(pattern))的行为完全相同。
*
* <p> 调用reset方法将扫描器的分隔符设置为默认值。
*
* @param pattern A string specifying a delimiting pattern
* @return this scanner
*/
public Scanner useDelimiter(String pattern) {
delimPattern = patternCache.forName(pattern);
return this;
}
/**
* 返回扫描仪的区域设置。
*
* <p>扫描程序的区域设置会影响默认基本匹配正则表达式的许多元素;参见上面的本地化数字。
*
* @return this scanner's locale
*/
public Locale locale() {
return this.locale;
}
/**
* 将扫描程序的区域设置为指定的区域设置。
*
* <p>扫描程序的区域设置会影响默认基本匹配正则表达式的许多元素;参见上面的本地化数字。
*
* <p>调用reset方法将把扫描器的区域设置为初始区域设置。
*
* @param locale A string specifying the locale to use
* @return this scanner
*/
public Scanner useLocale(Locale locale) {
if (locale.equals(this.locale))
return this;
this.locale = locale;
DecimalFormat df =
(DecimalFormat)NumberFormat.getNumberInstance(locale);
DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(locale);
// 必须对这些字符进行字面化,以避免与圆点或括号等正则表达式元字符发生冲突
groupSeparator = "\\" + dfs.getGroupingSeparator();
decimalSeparator = "\\" + dfs.getDecimalSeparator();
// 引用特定于区域设置的非零长度的内容,以避免与元字符的潜在冲突
nanString = "\\Q" + dfs.getNaN() + "\\E";
infinityString = "\\Q" + dfs.getInfinity() + "\\E";
positivePrefix = df.getPositivePrefix();
if (positivePrefix.length() > 0)
positivePrefix = "\\Q" + positivePrefix + "\\E";
negativePrefix = df.getNegativePrefix();
if (negativePrefix.length() > 0)
negativePrefix = "\\Q" + negativePrefix + "\\E";
positiveSuffix = df.getPositiveSuffix();
if (positiveSuffix.length() > 0)
positiveSuffix = "\\Q" + positiveSuffix + "\\E";
negativeSuffix = df.getNegativeSuffix();
if (negativeSuffix.length() > 0)
negativeSuffix = "\\Q" + negativeSuffix + "\\E";
// 强制重建和重新编译与区域相关的原始模式
integerPattern = null;
floatPattern = null;
return this;
}
/**
* 返回该扫描器的默认基数。
*
* <p>扫描器的基数会影响其默认数字匹配正则表达式的元素;参见上面的本地化数字。
*
* @return the default radix of this scanner
*/
public int radix() {
return this.defaultRadix;
}
/**
* 将扫描器的默认基数设置为指定的基数。
*
* <p>扫描器的基数会影响其默认数字匹配正则表达式的元素;参见上面的本地化数字。
*
* <p>如果基数小于字符MIN_RADIX,或者大于Character.MAX_RADIX,然后抛出一个IllegalArgumentException。
*
* <p>调用reset方法将把扫描器的基数设置为10。
*
* @param radix The radix to use when scanning numbers
* @return this scanner
* @throws IllegalArgumentException if radix is out of range
*/
public Scanner useRadix(int radix) {
if ((radix < Character.MIN_RADIX) || (radix > Character.MAX_RADIX))
throw new IllegalArgumentException("radix:"+radix);
if (this.defaultRadix == radix)
return this;
this.defaultRadix = radix;
// Force rebuilding and recompilation of radix dependent patterns
integerPattern = null;
return this;
}
// 下一个操作应该在指定的基数中进行,但默认值保持不变。
private void setRadix(int radix) {
if (this.radix != radix) {
// Force rebuilding and recompilation of radix dependent patterns
integerPattern = null;
this.radix = radix;
}
}
/**
* 返回此扫描器上次执行的扫描操作的匹配结果。
* 如果没有执行匹配,或者上次匹配不成功,此方法将抛出IllegalStateException。
*
* <p>如果扫描器的各种next方法完成而没有抛出异常,则匹配结果可用。
* 例如,在调用返回int的nextIntmethod之后,该方法返回一个匹配结果,用于搜索上面定义的整数正则表达式。
* 类似地,如果findInLine、findWithinHorizon和skip方法成功,它们将使匹配可用。
*
* @return a match result for the last match operation
* @throws IllegalStateException If no match result is available
*/
public MatchResult match() {
if (!matchValid)
throw new IllegalStateException("No match result available");
return matcher.toMatchResult();
}
/**
* <p>返回此扫描器的字符串表示形式。扫描器的字符串表示包含可能对调试有用的信息。具体的格式是未知的。
*
* @return The string representation of this scanner
*/
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("java.util.Scanner");
sb.append("[delimiters=" + delimPattern + "]");
sb.append("[position=" + position + "]");
sb.append("[match valid=" + matchValid + "]");
sb.append("[need input=" + needInput + "]");
sb.append("[source closed=" + sourceClosed + "]");
sb.append("[skipped=" + skipped + "]");
sb.append("[group separator=" + groupSeparator + "]");
sb.append("[decimal separator=" + decimalSeparator + "]");
sb.append("[positive prefix=" + positivePrefix + "]");
sb.append("[negative prefix=" + negativePrefix + "]");
sb.append("[positive suffix=" + positiveSuffix + "]");
sb.append("[negative suffix=" + negativeSuffix + "]");
sb.append("[NaN string=" + nanString + "]");
sb.append("[infinity string=" + infinityString + "]");
return sb.toString();
}
方法3个hasNext,3个next,remove,hasNextLine,nextLine,2个findInLine,2个findWithinHorizon,2个skip
/**
* 如果扫描器的输入中有另一个标记,则返回true。
* 此方法可能在等待扫描输入时阻塞。扫描器不会通过任何输入。
*
* @return true if and only if this scanner has another token
* @throws IllegalStateException if this scanner is closed
* @see java.util.Iterator
*/
public boolean hasNext() {
ensureOpen();
saveState();
while (!sourceClosed) {
if (hasTokenInBuffer())
return revertState(true);
readInput();
}
boolean result = hasTokenInBuffer();
return revertState(result);
}
/**
* 从此扫描程序查找并返回下一个完整的令牌。
* 一个完整的标记的前后都是匹配分隔符模式的输入。
* 这个方法在等待input扫描时可能会阻塞,即使先前对hasNext的调用返回true。
*
* @return the next token
* @throws NoSuchElementException if no more tokens are available
* @throws IllegalStateException if this scanner is closed
* @see java.util.Iterator
*/
public String next() {
ensureOpen();
clearCaches();
while (true) {
String token = getCompleteTokenInBuffer(null);
if (token != null) {
matchValid = true;
skipped = false;
return token;
}
if (needInput)
readInput();
else
throwFor();
}
}
/**
* 这种迭代器的实现不支持remove操作。
*
* @throws UnsupportedOperationException if this method is invoked.
* @see java.util.Iterator
*/
public void remove() {
throw new UnsupportedOperationException();
}
/**
* 如果下一个标记匹配由指定字符串构造的模式,则返回true。扫描器不会通过任何输入。
*
* <p> 以hasNext(pattern)形式调用此方法的行为与调用hasNext(pattern .compile(pattern))的行为完全相同。
*
* @param pattern a string specifying the pattern to scan
* @return true if and only if this scanner has another token matching
* the specified pattern
* @throws IllegalStateException if this scanner is closed
*/
public boolean hasNext(String pattern) {
return hasNext(patternCache.forName(pattern));
}
/**
* 如果匹配由指定字符串构造的模式,则返回下一个标记。
* 如果匹配成功,扫描程序将继续通过匹配模式的输入。
*
* <p>以next(pattern)形式调用此方法的行为与调用next(pattern .compile(pattern))的行为完全相同。
*
* @param pattern a string specifying the pattern to scan
* @return the next token
* @throws NoSuchElementException if no such tokens are available
* @throws IllegalStateException if this scanner is closed
*/
public String next(String pattern) {
return next(patternCache.forName(pattern));
}
/**
* 如果下一个完整标记匹配指定的模式,则返回true。
* 一个完整的标记由匹配分隔符模式的输入加上前缀和后缀。
* 此方法可能在等待输入时阻塞。扫描器不会通过任何输入。
*
* @param pattern the pattern to scan for
* @return true if and only if this scanner has another token matching
* the specified pattern
* @throws IllegalStateException if this scanner is closed
*/
public boolean hasNext(Pattern pattern) {
ensureOpen();
if (pattern == null)
throw new NullPointerException();
hasNextPattern = null;
saveState();
while (true) {
if (getCompleteTokenInBuffer(pattern) != null) {
matchValid = true;
cacheResult();
return revertState(true);
}
if (needInput)
readInput();
else
return revertState(false);
}
}
/**
* 如果匹配指定的模式,则返回下一个标记。
* 即使先前对hasNext(Pattern)的调用返回true,此方法也可能在等待扫描输入时阻塞。
* 如果匹配成功,则扫描程序向前通过匹配模式的输入。
*
* @param pattern the pattern to scan for
* @return the next token
* @throws NoSuchElementException if no more tokens are available
* @throws IllegalStateException if this scanner is closed
*/
public String next(Pattern pattern) {
ensureOpen();
if (pattern == null)
throw new NullPointerException();
// Did we already find this pattern?
if (hasNextPattern == pattern)
return getCachedResult();
clearCaches();
// Search for the pattern
while (true) {
String token = getCompleteTokenInBuffer(pattern);
if (token != null) {
matchValid = true;
skipped = false;
return token;
}
if (needInput)
readInput();
else
throwFor();
}
}
/**
* 如果该扫描器的输入中还有其他行,则返回true。
* 此方法可能在等待输入时阻塞。扫描器不会通过任何输入。
*
* @return true if and only if this scanner has another line of input
* @throws IllegalStateException if this scanner is closed
*/
public boolean hasNextLine() {
saveState();
String result = findWithinHorizon(linePattern(), 0);
if (result != null) {
MatchResult mr = this.match();
String lineSep = mr.group(1);
if (lineSep != null) {
result = result.substring(0, result.length() -
lineSep.length());
cacheResult(result);
} else {
cacheResult();
}
}
revertState();
return (result != null);
}
/**
* 使扫描器前进到当前行,并返回被跳过的输入。
* 此方法返回当前行的其余部分,不包括末尾的任何行分隔符。位置设置为下一行的开头。
*
* <p>由于该方法继续在输入中搜索行分隔符,因此如果没有行分隔符,它可能会缓冲所有用于跳过行的输入搜索。
*
* @return the line that was skipped
* @throws NoSuchElementException if no line was found
* @throws IllegalStateException if this scanner is closed
*/
public String nextLine() {
if (hasNextPattern == linePattern())
return getCachedResult();
clearCaches();
String result = findWithinHorizon(linePattern, 0);
if (result == null)
throw new NoSuchElementException("No line found");
MatchResult mr = this.match();
String lineSep = mr.group(1);
if (lineSep != null)
result = result.substring(0, result.length() - lineSep.length());
if (result == null)
throw new NoSuchElementException();
else
return result;
}
// 忽视分隔符的查找
/**
* 尝试查找由指定字符串构造的模式的下一个出现,忽略分隔符。
*
* <p>以findInLine(pattern)形式调用该方法的行为与findInLine(pattern .compile(pattern))调用的行为完全相同。
*
* @param pattern a string specifying the pattern to search for
* @return the text that matched the specified pattern
* @throws IllegalStateException if this scanner is closed
*/
public String findInLine(String pattern) {
return findInLine(patternCache.forName(pattern));
}
/**
* 尝试查找指定的模式忽略分隔符的下一个匹配项。
* 如果在下一个分隔符之前找到了模式,扫描器就会跳过匹配的输入并返回匹配该模式的字符串。
* 如果在下一个行分隔符之前的输入中没有检测到这样的模式,则返回null,扫描器的位置不变。
* 这个方法可能会阻塞对匹配模式的输入的等待。
*
* <p>由于该方法继续在输入中搜索指定的模式,因此如果没有行分隔符,它可能会缓冲所有用于搜索所需标记的输入。
*
* @param pattern the pattern to scan for
* @return the text that matched the specified pattern
* @throws IllegalStateException if this scanner is closed
*/
public String findInLine(Pattern pattern) {
ensureOpen();
if (pattern == null)
throw new NullPointerException();
clearCaches();
// Expand buffer to include the next newline or end of input
int endPosition = 0;
saveState();
while (true) {
String token = findPatternInBuffer(separatorPattern(), 0);
if (token != null) {
endPosition = matcher.start();
break; // up to next newline
}
if (needInput) {
readInput();
} else {
endPosition = buf.limit();
break; // up to end of input
}
}
revertState();
int horizonForLine = endPosition - position;
// 如果当前pos和下一个换行符之间没有任何东西,则简单地返回null,调用"horizon=0"的findWithinHorizon将扫描行边界之外。
if (horizonForLine == 0)
return null;
// 在horizonForLine选择pattern
return findWithinHorizon(pattern, horizonForLine);
}
/**
* 尝试查找由指定字符串构造的模式的下一个出现,忽略分隔符。
*
* <p>以findWithinHorizon(模式)形式调用此方法的行为与调用findWithinHorizon(pattern .compile(pattern, horizon))的行为完全相同。
*
* @param pattern a string specifying the pattern to search for
* @param horizon the search horizon
* @return the text that matched the specified pattern
* @throws IllegalStateException if this scanner is closed
* @throws IllegalArgumentException if horizon is negative
*/
public String findWithinHorizon(String pattern, int horizon) {
return findWithinHorizon(patternCache.forName(pattern), horizon);
}
/**
* 尝试查找指定模式的下一个匹配项。
*
* <p>此方法将搜索整个输入直到指定的搜索范围,忽略分隔符。
* 如果找到了模式,扫描器会向前推进并返回匹配模式的字符串。
* 如果没有检测到这样的模式,则返回完整的图像,扫描器的位置保持不变。
* 此方法可能阻塞等待与模式匹配的输入。
*
* <p>扫描器永远不会搜索超出horizon。
* 注意,一根火柴可能会被horizon切断;
* 也就是说,如果视野更大,任意的匹配结果可能会不同。
* 扫描器将horizon视为一个透明的、非锚定的边界(参见Matcher.useTransparentBounds和Matcher.useAnchoringBounds)。
*
* <p>如果horizon为0,则horizon被忽略,该方法将继续在输入中搜索指定的模式,而不受限制。
* 在这种情况下,它可以缓冲搜索模式的所有输入。
*
* <p>如果horizon为负,则抛出一个IllegalArgumentException。
*
* @param pattern the pattern to scan for
* @param horizon the search horizon
* @return the text that matched the specified pattern
* @throws IllegalStateException if this scanner is closed
* @throws IllegalArgumentException if horizon is negative
*/
public String findWithinHorizon(Pattern pattern, int horizon) {
ensureOpen();
if (pattern == null)
throw new NullPointerException();
if (horizon < 0)
throw new IllegalArgumentException("horizon < 0");
clearCaches();
// Search for the pattern
while (true) {
String token = findPatternInBuffer(pattern, horizon);
if (token != null) {
matchValid = true;
return token;
}
if (needInput)
readInput();
else
break; // up to end of input
}
return null;
}
/**
* 跳过匹配指定模式的输入,忽略分隔符。如果指定模式的锚定匹配成功,此方法将跳过输入。
*
* <p>如果在当前位置没有找到与指定模式的匹配,则不会跳过任何输入并抛出NoSuchElementException。
*
* <p>因为这个方法从扫描器的当前位置开始匹配指定的模式,所以可以匹配大量的输入(".*",例如)可能会导致扫描器缓冲大量的输入。
*
* <p>请注意,通过使用不匹配任何内容的模式,例如sc.skip("[\t]*"),可以跳过某些内容而不引发NoSuchElementException异常。
*
* @param pattern a string specifying the pattern to skip over
* @return this scanner
* @throws NoSuchElementException if the specified pattern is not found
* @throws IllegalStateException if this scanner is closed
*/
public Scanner skip(Pattern pattern) {
ensureOpen();
if (pattern == null)
throw new NullPointerException();
clearCaches();
// Search for the pattern
while (true) {
String token = matchPatternInBuffer(pattern);
if (token != null) {
matchValid = true;
position = matcher.end();
return this;
}
if (needInput)
readInput();
else
throw new NoSuchElementException();
}
}
/**
* 跳过匹配由指定字符串构造的模式的输入。
*
* <p> 以skip(pattern)形式调用此方法的行为与调用skip(pattern .compile(pattern))完全相同。
*
* @param pattern a string specifying the pattern to skip over
* @return this scanner
* @throws IllegalStateException if this scanner is closed
*/
public Scanner skip(String pattern) {
return skip(patternCache.forName(pattern));
}
方法hasNextXXX,nextXXX(boolean,byte,short,int,long,float,double,biginteger,bigdecimal),reset
// 扫描基本类型的方法
/**
* 如果扫描器输入的下一个标记可以被解释为一个布尔值,
* 使用从字符串"true|false"创建的不区分大小写的模式,则返回true。
* 扫描器不会向前推进匹配的输入。
*
* @return true if and only if this scanner's next token is a valid
* boolean value
* @throws IllegalStateException if this scanner is closed
*/
public boolean hasNextBoolean() {
return hasNext(boolPattern());
}
/**
* 将输入的下一个标记扫描为一个布尔值并返回该值。
* 这个方法将抛出InputMismatchException,如果下一个标记不能被转换成一个有效的布尔值。
* 如果匹配成功,扫描器向前推进匹配的输入。
*
* @return the boolean scanned from the input
* @throws InputMismatchException if the next token is not a valid boolean
* @throws NoSuchElementException if input is exhausted
* @throws IllegalStateException if this scanner is closed
*/
public boolean nextBoolean() {
clearCaches();
return Boolean.parseBoolean(next(boolPattern()));
}
/**
* Returns true if the next token in this scanner's input can be
* interpreted as a byte value in the default radix using the
* {@link #nextByte} method. The scanner does not advance past any input.
*
* @return true if and only if this scanner's next token is a valid
* byte value
* @throws IllegalStateException if this scanner is closed
*/
public boolean hasNextByte() {
return hasNextByte(defaultRadix);
}
/**
* Returns true if the next token in this scanner's input can be
* interpreted as a byte value in the specified radix using the
* {@link #nextByte} method. The scanner does not advance past any input.
*
* @param radix the radix used to interpret the token as a byte value
* @return true if and only if this scanner's next token is a valid
* byte value
* @throws IllegalStateException if this scanner is closed
*/
public boolean hasNextByte(int radix) {
setRadix(radix);
boolean result = hasNext(integerPattern());
if (result) { // Cache it
try {
String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
processIntegerToken(hasNextResult) :
hasNextResult;
typeCache = Byte.parseByte(s, radix);
} catch (NumberFormatException nfe) {
result = false;
}
}
return result;
}
/**
* Scans the next token of the input as a <tt>byte</tt>.
*
* <p> An invocation of this method of the form
* <tt>nextByte()</tt> behaves in exactly the same way as the
* invocation <tt>nextByte(radix)</tt>, where <code>radix</code>
* is the default radix of this scanner.
*
* @return the <tt>byte</tt> scanned from the input
* @throws InputMismatchException
* if the next token does not match the <i>Integer</i>
* regular expression, or is out of range
* @throws NoSuchElementException if input is exhausted
* @throws IllegalStateException if this scanner is closed
*/
public byte nextByte() {
return nextByte(defaultRadix);
}
/**
* Scans the next token of the input as a <tt>byte</tt>.
* This method will throw <code>InputMismatchException</code>
* if the next token cannot be translated into a valid byte value as
* described below. If the translation is successful, the scanner advances
* past the input that matched.
*
* <p> If the next token matches the <a
* href="#Integer-regex"><i>Integer</i></a> regular expression defined
* above then the token is converted into a <tt>byte</tt> value as if by
* removing all locale specific prefixes, group separators, and locale
* specific suffixes, then mapping non-ASCII digits into ASCII
* digits via {@link Character#digit Character.digit}, prepending a
* negative sign (-) if the locale specific negative prefixes and suffixes
* were present, and passing the resulting string to
* {@link Byte#parseByte(String, int) Byte.parseByte} with the
* specified radix.
*
* @param radix the radix used to interpret the token as a byte value
* @return the <tt>byte</tt> scanned from the input
* @throws InputMismatchException
* if the next token does not match the <i>Integer</i>
* regular expression, or is out of range
* @throws NoSuchElementException if input is exhausted
* @throws IllegalStateException if this scanner is closed
*/
public byte nextByte(int radix) {
// Check cached result
if ((typeCache != null) && (typeCache instanceof Byte)
&& this.radix == radix) {
byte val = ((Byte)typeCache).byteValue();
useTypeCache();
return val;
}
setRadix(radix);
clearCaches();
// Search for next byte
try {
String s = next(integerPattern());
if (matcher.group(SIMPLE_GROUP_INDEX) == null)
s = processIntegerToken(s);
return Byte.parseByte(s, radix);
} catch (NumberFormatException nfe) {
position = matcher.start(); // don't skip bad token
throw new InputMismatchException(nfe.getMessage());
}
}
/**
* Returns true if the next token in this scanner's input can be
* interpreted as a short value in the default radix using the
* {@link #nextShort} method. The scanner does not advance past any input.
*
* @return true if and only if this scanner's next token is a valid
* short value in the default radix
* @throws IllegalStateException if this scanner is closed
*/
public boolean hasNextShort() {
return hasNextShort(defaultRadix);
}
/**
* Returns true if the next token in this scanner's input can be
* interpreted as a short value in the specified radix using the
* {@link #nextShort} method. The scanner does not advance past any input.
*
* @param radix the radix used to interpret the token as a short value
* @return true if and only if this scanner's next token is a valid
* short value in the specified radix
* @throws IllegalStateException if this scanner is closed
*/
public boolean hasNextShort(int radix) {
setRadix(radix);
boolean result = hasNext(integerPattern());
if (result) { // Cache it
try {
String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
processIntegerToken(hasNextResult) :
hasNextResult;
typeCache = Short.parseShort(s, radix);
} catch (NumberFormatException nfe) {
result = false;
}
}
return result;
}
/**
* Scans the next token of the input as a <tt>short</tt>.
*
* <p> An invocation of this method of the form
* <tt>nextShort()</tt> behaves in exactly the same way as the
* invocation <tt>nextShort(radix)</tt>, where <code>radix</code>
* is the default radix of this scanner.
*
* @return the <tt>short</tt> scanned from the input
* @throws InputMismatchException
* if the next token does not match the <i>Integer</i>
* regular expression, or is out of range
* @throws NoSuchElementException if input is exhausted
* @throws IllegalStateException if this scanner is closed
*/
public short nextShort() {
return nextShort(defaultRadix);
}
/**
* Scans the next token of the input as a <tt>short</tt>.
* This method will throw <code>InputMismatchException</code>
* if the next token cannot be translated into a valid short value as
* described below. If the translation is successful, the scanner advances
* past the input that matched.
*
* <p> If the next token matches the <a
* href="#Integer-regex"><i>Integer</i></a> regular expression defined
* above then the token is converted into a <tt>short</tt> value as if by
* removing all locale specific prefixes, group separators, and locale
* specific suffixes, then mapping non-ASCII digits into ASCII
* digits via {@link Character#digit Character.digit}, prepending a
* negative sign (-) if the locale specific negative prefixes and suffixes
* were present, and passing the resulting string to
* {@link Short#parseShort(String, int) Short.parseShort} with the
* specified radix.
*
* @param radix the radix used to interpret the token as a short value
* @return the <tt>short</tt> scanned from the input
* @throws InputMismatchException
* if the next token does not match the <i>Integer</i>
* regular expression, or is out of range
* @throws NoSuchElementException if input is exhausted
* @throws IllegalStateException if this scanner is closed
*/
public short nextShort(int radix) {
// Check cached result
if ((typeCache != null) && (typeCache instanceof Short)
&& this.radix == radix) {
short val = ((Short)typeCache).shortValue();
useTypeCache();
return val;
}
setRadix(radix);
clearCaches();
// Search for next short
try {
String s = next(integerPattern());
if (matcher.group(SIMPLE_GROUP_INDEX) == null)
s = processIntegerToken(s);
return Short.parseShort(s, radix);
} catch (NumberFormatException nfe) {
position = matcher.start(); // don't skip bad token
throw new InputMismatchException(nfe.getMessage());
}
}
/**
* Returns true if the next token in this scanner's input can be
* interpreted as an int value in the default radix using the
* {@link #nextInt} method. The scanner does not advance past any input.
*
* @return true if and only if this scanner's next token is a valid
* int value
* @throws IllegalStateException if this scanner is closed
*/
public boolean hasNextInt() {
return hasNextInt(defaultRadix);
}
/**
* Returns true if the next token in this scanner's input can be
* interpreted as an int value in the specified radix using the
* {@link #nextInt} method. The scanner does not advance past any input.
*
* @param radix the radix used to interpret the token as an int value
* @return true if and only if this scanner's next token is a valid
* int value
* @throws IllegalStateException if this scanner is closed
*/
public boolean hasNextInt(int radix) {
setRadix(radix);
boolean result = hasNext(integerPattern());
if (result) { // Cache it
try {
String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
processIntegerToken(hasNextResult) :
hasNextResult;
typeCache = Integer.parseInt(s, radix);
} catch (NumberFormatException nfe) {
result = false;
}
}
return result;
}
/**
* The integer token must be stripped of prefixes, group separators,
* and suffixes, non ascii digits must be converted into ascii digits
* before parse will accept it.
*/
private String processIntegerToken(String token) {
String result = token.replaceAll(""+groupSeparator, "");
boolean isNegative = false;
int preLen = negativePrefix.length();
if ((preLen > 0) && result.startsWith(negativePrefix)) {
isNegative = true;
result = result.substring(preLen);
}
int sufLen = negativeSuffix.length();
if ((sufLen > 0) && result.endsWith(negativeSuffix)) {
isNegative = true;
result = result.substring(result.length() - sufLen,
result.length());
}
if (isNegative)
result = "-" + result;
return result;
}
/**
* Scans the next token of the input as an <tt>int</tt>.
*
* <p> An invocation of this method of the form
* <tt>nextInt()</tt> behaves in exactly the same way as the
* invocation <tt>nextInt(radix)</tt>, where <code>radix</code>
* is the default radix of this scanner.
*
* @return the <tt>int</tt> scanned from the input
* @throws InputMismatchException
* if the next token does not match the <i>Integer</i>
* regular expression, or is out of range
* @throws NoSuchElementException if input is exhausted
* @throws IllegalStateException if this scanner is closed
*/
public int nextInt() {
return nextInt(defaultRadix);
}
/**
* Scans the next token of the input as an <tt>int</tt>.
* This method will throw <code>InputMismatchException</code>
* if the next token cannot be translated into a valid int value as
* described below. If the translation is successful, the scanner advances
* past the input that matched.
*
* <p> If the next token matches the <a
* href="#Integer-regex"><i>Integer</i></a> regular expression defined
* above then the token is converted into an <tt>int</tt> value as if by
* removing all locale specific prefixes, group separators, and locale
* specific suffixes, then mapping non-ASCII digits into ASCII
* digits via {@link Character#digit Character.digit}, prepending a
* negative sign (-) if the locale specific negative prefixes and suffixes
* were present, and passing the resulting string to
* {@link Integer#parseInt(String, int) Integer.parseInt} with the
* specified radix.
*
* @param radix the radix used to interpret the token as an int value
* @return the <tt>int</tt> scanned from the input
* @throws InputMismatchException
* if the next token does not match the <i>Integer</i>
* regular expression, or is out of range
* @throws NoSuchElementException if input is exhausted
* @throws IllegalStateException if this scanner is closed
*/
public int nextInt(int radix) {
// Check cached result
if ((typeCache != null) && (typeCache instanceof Integer)
&& this.radix == radix) {
int val = ((Integer)typeCache).intValue();
useTypeCache();
return val;
}
setRadix(radix);
clearCaches();
// Search for next int
try {
String s = next(integerPattern());
if (matcher.group(SIMPLE_GROUP_INDEX) == null)
s = processIntegerToken(s);
return Integer.parseInt(s, radix);
} catch (NumberFormatException nfe) {
position = matcher.start(); // don't skip bad token
throw new InputMismatchException(nfe.getMessage());
}
}
/**
* Returns true if the next token in this scanner's input can be
* interpreted as a long value in the default radix using the
* {@link #nextLong} method. The scanner does not advance past any input.
*
* @return true if and only if this scanner's next token is a valid
* long value
* @throws IllegalStateException if this scanner is closed
*/
public boolean hasNextLong() {
return hasNextLong(defaultRadix);
}
/**
* Returns true if the next token in this scanner's input can be
* interpreted as a long value in the specified radix using the
* {@link #nextLong} method. The scanner does not advance past any input.
*
* @param radix the radix used to interpret the token as a long value
* @return true if and only if this scanner's next token is a valid
* long value
* @throws IllegalStateException if this scanner is closed
*/
public boolean hasNextLong(int radix) {
setRadix(radix);
boolean result = hasNext(integerPattern());
if (result) { // Cache it
try {
String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
processIntegerToken(hasNextResult) :
hasNextResult;
typeCache = Long.parseLong(s, radix);
} catch (NumberFormatException nfe) {
result = false;
}
}
return result;
}
/**
* Scans the next token of the input as a <tt>long</tt>.
*
* <p> An invocation of this method of the form
* <tt>nextLong()</tt> behaves in exactly the same way as the
* invocation <tt>nextLong(radix)</tt>, where <code>radix</code>
* is the default radix of this scanner.
*
* @return the <tt>long</tt> scanned from the input
* @throws InputMismatchException
* if the next token does not match the <i>Integer</i>
* regular expression, or is out of range
* @throws NoSuchElementException if input is exhausted
* @throws IllegalStateException if this scanner is closed
*/
public long nextLong() {
return nextLong(defaultRadix);
}
/**
* Scans the next token of the input as a <tt>long</tt>.
* This method will throw <code>InputMismatchException</code>
* if the next token cannot be translated into a valid long value as
* described below. If the translation is successful, the scanner advances
* past the input that matched.
*
* <p> If the next token matches the <a
* href="#Integer-regex"><i>Integer</i></a> regular expression defined
* above then the token is converted into a <tt>long</tt> value as if by
* removing all locale specific prefixes, group separators, and locale
* specific suffixes, then mapping non-ASCII digits into ASCII
* digits via {@link Character#digit Character.digit}, prepending a
* negative sign (-) if the locale specific negative prefixes and suffixes
* were present, and passing the resulting string to
* {@link Long#parseLong(String, int) Long.parseLong} with the
* specified radix.
*
* @param radix the radix used to interpret the token as an int value
* @return the <tt>long</tt> scanned from the input
* @throws InputMismatchException
* if the next token does not match the <i>Integer</i>
* regular expression, or is out of range
* @throws NoSuchElementException if input is exhausted
* @throws IllegalStateException if this scanner is closed
*/
public long nextLong(int radix) {
// Check cached result
if ((typeCache != null) && (typeCache instanceof Long)
&& this.radix == radix) {
long val = ((Long)typeCache).longValue();
useTypeCache();
return val;
}
setRadix(radix);
clearCaches();
try {
String s = next(integerPattern());
if (matcher.group(SIMPLE_GROUP_INDEX) == null)
s = processIntegerToken(s);
return Long.parseLong(s, radix);
} catch (NumberFormatException nfe) {
position = matcher.start(); // don't skip bad token
throw new InputMismatchException(nfe.getMessage());
}
}
/**
* The float token must be stripped of prefixes, group separators,
* and suffixes, non ascii digits must be converted into ascii digits
* before parseFloat will accept it.
*
* If there are non-ascii digits in the token these digits must
* be processed before the token is passed to parseFloat.
*/
private String processFloatToken(String token) {
String result = token.replaceAll(groupSeparator, "");
if (!decimalSeparator.equals("\\."))
result = result.replaceAll(decimalSeparator, ".");
boolean isNegative = false;
int preLen = negativePrefix.length();
if ((preLen > 0) && result.startsWith(negativePrefix)) {
isNegative = true;
result = result.substring(preLen);
}
int sufLen = negativeSuffix.length();
if ((sufLen > 0) && result.endsWith(negativeSuffix)) {
isNegative = true;
result = result.substring(result.length() - sufLen,
result.length());
}
if (result.equals(nanString))
result = "NaN";
if (result.equals(infinityString))
result = "Infinity";
if (isNegative)
result = "-" + result;
// Translate non-ASCII digits
Matcher m = NON_ASCII_DIGIT.matcher(result);
if (m.find()) {
StringBuilder inASCII = new StringBuilder();
for (int i=0; i<result.length(); i++) {
char nextChar = result.charAt(i);
if (Character.isDigit(nextChar)) {
int d = Character.digit(nextChar, 10);
if (d != -1)
inASCII.append(d);
else
inASCII.append(nextChar);
} else {
inASCII.append(nextChar);
}
}
result = inASCII.toString();
}
return result;
}
/**
* Returns true if the next token in this scanner's input can be
* interpreted as a float value using the {@link #nextFloat}
* method. The scanner does not advance past any input.
*
* @return true if and only if this scanner's next token is a valid
* float value
* @throws IllegalStateException if this scanner is closed
*/
public boolean hasNextFloat() {
setRadix(10);
boolean result = hasNext(floatPattern());
if (result) { // Cache it
try {
String s = processFloatToken(hasNextResult);
typeCache = Float.valueOf(Float.parseFloat(s));
} catch (NumberFormatException nfe) {
result = false;
}
}
return result;
}
/**
* Scans the next token of the input as a <tt>float</tt>.
* This method will throw <code>InputMismatchException</code>
* if the next token cannot be translated into a valid float value as
* described below. If the translation is successful, the scanner advances
* past the input that matched.
*
* <p> If the next token matches the <a
* href="#Float-regex"><i>Float</i></a> regular expression defined above
* then the token is converted into a <tt>float</tt> value as if by
* removing all locale specific prefixes, group separators, and locale
* specific suffixes, then mapping non-ASCII digits into ASCII
* digits via {@link Character#digit Character.digit}, prepending a
* negative sign (-) if the locale specific negative prefixes and suffixes
* were present, and passing the resulting string to
* {@link Float#parseFloat Float.parseFloat}. If the token matches
* the localized NaN or infinity strings, then either "Nan" or "Infinity"
* is passed to {@link Float#parseFloat(String) Float.parseFloat} as
* appropriate.
*
* @return the <tt>float</tt> scanned from the input
* @throws InputMismatchException
* if the next token does not match the <i>Float</i>
* regular expression, or is out of range
* @throws NoSuchElementException if input is exhausted
* @throws IllegalStateException if this scanner is closed
*/
public float nextFloat() {
// Check cached result
if ((typeCache != null) && (typeCache instanceof Float)) {
float val = ((Float)typeCache).floatValue();
useTypeCache();
return val;
}
setRadix(10);
clearCaches();
try {
return Float.parseFloat(processFloatToken(next(floatPattern())));
} catch (NumberFormatException nfe) {
position = matcher.start(); // don't skip bad token
throw new InputMismatchException(nfe.getMessage());
}
}
/**
* Returns true if the next token in this scanner's input can be
* interpreted as a double value using the {@link #nextDouble}
* method. The scanner does not advance past any input.
*
* @return true if and only if this scanner's next token is a valid
* double value
* @throws IllegalStateException if this scanner is closed
*/
public boolean hasNextDouble() {
setRadix(10);
boolean result = hasNext(floatPattern());
if (result) { // Cache it
try {
String s = processFloatToken(hasNextResult);
typeCache = Double.valueOf(Double.parseDouble(s));
} catch (NumberFormatException nfe) {
result = false;
}
}
return result;
}
/**
* Scans the next token of the input as a <tt>double</tt>.
* This method will throw <code>InputMismatchException</code>
* if the next token cannot be translated into a valid double value.
* If the translation is successful, the scanner advances past the input
* that matched.
*
* <p> If the next token matches the <a
* href="#Float-regex"><i>Float</i></a> regular expression defined above
* then the token is converted into a <tt>double</tt> value as if by
* removing all locale specific prefixes, group separators, and locale
* specific suffixes, then mapping non-ASCII digits into ASCII
* digits via {@link Character#digit Character.digit}, prepending a
* negative sign (-) if the locale specific negative prefixes and suffixes
* were present, and passing the resulting string to
* {@link Double#parseDouble Double.parseDouble}. If the token matches
* the localized NaN or infinity strings, then either "Nan" or "Infinity"
* is passed to {@link Double#parseDouble(String) Double.parseDouble} as
* appropriate.
*
* @return the <tt>double</tt> scanned from the input
* @throws InputMismatchException
* if the next token does not match the <i>Float</i>
* regular expression, or is out of range
* @throws NoSuchElementException if the input is exhausted
* @throws IllegalStateException if this scanner is closed
*/
public double nextDouble() {
// Check cached result
if ((typeCache != null) && (typeCache instanceof Double)) {
double val = ((Double)typeCache).doubleValue();
useTypeCache();
return val;
}
setRadix(10);
clearCaches();
// Search for next float
try {
return Double.parseDouble(processFloatToken(next(floatPattern())));
} catch (NumberFormatException nfe) {
position = matcher.start(); // don't skip bad token
throw new InputMismatchException(nfe.getMessage());
}
}
// Convenience methods for scanning multi precision numbers
/**
* Returns true if the next token in this scanner's input can be
* interpreted as a <code>BigInteger</code> in the default radix using the
* {@link #nextBigInteger} method. The scanner does not advance past any
* input.
*
* @return true if and only if this scanner's next token is a valid
* <code>BigInteger</code>
* @throws IllegalStateException if this scanner is closed
*/
public boolean hasNextBigInteger() {
return hasNextBigInteger(defaultRadix);
}
/**
* Returns true if the next token in this scanner's input can be
* interpreted as a <code>BigInteger</code> in the specified radix using
* the {@link #nextBigInteger} method. The scanner does not advance past
* any input.
*
* @param radix the radix used to interpret the token as an integer
* @return true if and only if this scanner's next token is a valid
* <code>BigInteger</code>
* @throws IllegalStateException if this scanner is closed
*/
public boolean hasNextBigInteger(int radix) {
setRadix(radix);
boolean result = hasNext(integerPattern());
if (result) { // Cache it
try {
String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
processIntegerToken(hasNextResult) :
hasNextResult;
typeCache = new BigInteger(s, radix);
} catch (NumberFormatException nfe) {
result = false;
}
}
return result;
}
/**
* Scans the next token of the input as a {@link java.math.BigInteger
* BigInteger}.
*
* <p> An invocation of this method of the form
* <tt>nextBigInteger()</tt> behaves in exactly the same way as the
* invocation <tt>nextBigInteger(radix)</tt>, where <code>radix</code>
* is the default radix of this scanner.
*
* @return the <tt>BigInteger</tt> scanned from the input
* @throws InputMismatchException
* if the next token does not match the <i>Integer</i>
* regular expression, or is out of range
* @throws NoSuchElementException if the input is exhausted
* @throws IllegalStateException if this scanner is closed
*/
public BigInteger nextBigInteger() {
return nextBigInteger(defaultRadix);
}
/**
* Scans the next token of the input as a {@link java.math.BigInteger
* BigInteger}.
*
* <p> If the next token matches the <a
* href="#Integer-regex"><i>Integer</i></a> regular expression defined
* above then the token is converted into a <tt>BigInteger</tt> value as if
* by removing all group separators, mapping non-ASCII digits into ASCII
* digits via the {@link Character#digit Character.digit}, and passing the
* resulting string to the {@link
* java.math.BigInteger#BigInteger(java.lang.String)
* BigInteger(String, int)} constructor with the specified radix.
*
* @param radix the radix used to interpret the token
* @return the <tt>BigInteger</tt> scanned from the input
* @throws InputMismatchException
* if the next token does not match the <i>Integer</i>
* regular expression, or is out of range
* @throws NoSuchElementException if the input is exhausted
* @throws IllegalStateException if this scanner is closed
*/
public BigInteger nextBigInteger(int radix) {
// Check cached result
if ((typeCache != null) && (typeCache instanceof BigInteger)
&& this.radix == radix) {
BigInteger val = (BigInteger)typeCache;
useTypeCache();
return val;
}
setRadix(radix);
clearCaches();
// Search for next int
try {
String s = next(integerPattern());
if (matcher.group(SIMPLE_GROUP_INDEX) == null)
s = processIntegerToken(s);
return new BigInteger(s, radix);
} catch (NumberFormatException nfe) {
position = matcher.start(); // don't skip bad token
throw new InputMismatchException(nfe.getMessage());
}
}
/**
* Returns true if the next token in this scanner's input can be
* interpreted as a <code>BigDecimal</code> using the
* {@link #nextBigDecimal} method. The scanner does not advance past any
* input.
*
* @return true if and only if this scanner's next token is a valid
* <code>BigDecimal</code>
* @throws IllegalStateException if this scanner is closed
*/
public boolean hasNextBigDecimal() {
setRadix(10);
boolean result = hasNext(decimalPattern());
if (result) { // Cache it
try {
String s = processFloatToken(hasNextResult);
typeCache = new BigDecimal(s);
} catch (NumberFormatException nfe) {
result = false;
}
}
return result;
}
/**
* Scans the next token of the input as a {@link java.math.BigDecimal
* BigDecimal}.
*
* <p> If the next token matches the <a
* href="#Decimal-regex"><i>Decimal</i></a> regular expression defined
* above then the token is converted into a <tt>BigDecimal</tt> value as if
* by removing all group separators, mapping non-ASCII digits into ASCII
* digits via the {@link Character#digit Character.digit}, and passing the
* resulting string to the {@link
* java.math.BigDecimal#BigDecimal(java.lang.String) BigDecimal(String)}
* constructor.
*
* @return the <tt>BigDecimal</tt> scanned from the input
* @throws InputMismatchException
* if the next token does not match the <i>Decimal</i>
* regular expression, or is out of range
* @throws NoSuchElementException if the input is exhausted
* @throws IllegalStateException if this scanner is closed
*/
public BigDecimal nextBigDecimal() {
// Check cached result
if ((typeCache != null) && (typeCache instanceof BigDecimal)) {
BigDecimal val = (BigDecimal)typeCache;
useTypeCache();
return val;
}
setRadix(10);
clearCaches();
// Search for next float
try {
String s = processFloatToken(next(decimalPattern()));
return new BigDecimal(s);
} catch (NumberFormatException nfe) {
position = matcher.start(); // don't skip bad token
throw new InputMismatchException(nfe.getMessage());
}
}
/**
* 重置这个扫描仪。
*
* <p> 重新设置扫描程序将丢弃它所有的显式状态信息,这些信息可能已经被useDelimiter、useLocale或useRadix调用所更改。
*
* <p> scanner.reset()的这个方法的调用与调用的行为完全相同
*
* <blockquote><pre>{@code
* scanner.useDelimiter("\\p{javaWhitespace}+")
* .useLocale(Locale.getDefault(Locale.Category.FORMAT))
* .useRadix(10);
* }</pre></blockquote>
*
* @return this scanner
*
* @since 1.6
*/
public Scanner reset() {
delimPattern = WHITESPACE_PATTERN;
useLocale(Locale.getDefault(Locale.Category.FORMAT));
useRadix(10);
clearCaches();
return this;
}