1 /*
   2  * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.io.*;
  29 import java.math.*;
  30 import java.nio.*;
  31 import java.nio.channels.*;
  32 import java.nio.charset.*;
  33 import java.nio.file.Path;
  34 import java.nio.file.Files;
  35 import java.text.*;
  36 import java.util.function.Consumer;
  37 import java.util.regex.*;
  38 import java.util.stream.Stream;
  39 import java.util.stream.StreamSupport;
  40 
  41 /**
  42  * A simple text scanner which can parse primitive types and strings using
  43  * regular expressions.
  44  *
  45  * <p>A {@code Scanner} breaks its input into tokens using a
  46  * delimiter pattern, which by default matches whitespace. The resulting
  47  * tokens may then be converted into values of different types using the
  48  * various {@code next} methods.
  49  *
  50  * <p>For example, this code allows a user to read a number from
  51  * {@code System.in}:
  52  * <blockquote><pre>{@code
  53  *     Scanner sc = new Scanner(System.in);
  54  *     int i = sc.nextInt();
  55  * }</pre></blockquote>
  56  *
  57  * <p>As another example, this code allows {@code long} types to be
  58  * assigned from entries in a file {@code myNumbers}:
  59  * <blockquote><pre>{@code
  60  *      Scanner sc = new Scanner(new File("myNumbers"));
  61  *      while (sc.hasNextLong()) {
  62  *          long aLong = sc.nextLong();
  63  *      }
  64  * }</pre></blockquote>
  65  *
  66  * <p>The scanner can also use delimiters other than whitespace. This
  67  * example reads several items in from a string:
  68  * <blockquote><pre>{@code
  69  *     String input = "1 fish 2 fish red fish blue fish";
  70  *     Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
  71  *     System.out.println(s.nextInt());
  72  *     System.out.println(s.nextInt());
  73  *     System.out.println(s.next());
  74  *     System.out.println(s.next());
  75  *     s.close();
  76  * }</pre></blockquote>
  77  * <p>
  78  * prints the following output:
  79  * <blockquote><pre>{@code
  80  *     1
  81  *     2
  82  *     red
  83  *     blue
  84  * }</pre></blockquote>
  85  *
  86  * <p>The same output can be generated with this code, which uses a regular
  87  * expression to parse all four tokens at once:
  88  * <blockquote><pre>{@code
  89  *     String input = "1 fish 2 fish red fish blue fish";
  90  *     Scanner s = new Scanner(input);
  91  *     s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
  92  *     MatchResult result = s.match();
  93  *     for (int i=1; i<=result.groupCount(); i++)
  94  *         System.out.println(result.group(i));
  95  *     s.close();
  96  * }</pre></blockquote>
  97  *
  98  * <p>The <a name="default-delimiter">default whitespace delimiter</a> used
  99  * by a scanner is as recognized by {@link Character#isWhitespace(char)
 100  * Character.isWhitespace()}. The {@link #reset reset()}
 101  * method will reset the value of the scanner's delimiter to the default
 102  * whitespace delimiter regardless of whether it was previously changed.
 103  *
 104  * <p>A scanning operation may block waiting for input.
 105  *
 106  * <p>The {@link #next} and {@link #hasNext} methods and their
 107  * companion methods (such as {@link #nextInt} and
 108  * {@link #hasNextInt}) first skip any input that matches the delimiter
 109  * pattern, and then attempt to return the next token. Both {@code hasNext()}
 110  * and {@code next()} methods may block waiting for further input.  Whether a
 111  * {@code hasNext()} method blocks has no connection to whether or not its
 112  * associated {@code next()} method will block. The {@link #tokens} method
 113  * may also block waiting for input.
 114  *
 115  * <p>The {@link #findInLine findInLine()},
 116  * {@link #findWithinHorizon findWithinHorizon()},
 117  * {@link #skip skip()}, and {@link #findAll findAll()}
 118  * methods operate independently of the delimiter pattern. These methods will
 119  * attempt to match the specified pattern with no regard to delimiters in the
 120  * input and thus can be used in special circumstances where delimiters are
 121  * not relevant. These methods may block waiting for more input.
 122  *
 123  * <p>When a scanner throws an {@link InputMismatchException}, the scanner
 124  * will not pass the token that caused the exception, so that it may be
 125  * retrieved or skipped via some other method.
 126  *
 127  * <p>Depending upon the type of delimiting pattern, empty tokens may be
 128  * returned. For example, the pattern {@code "\\s+"} will return no empty
 129  * tokens since it matches multiple instances of the delimiter. The delimiting
 130  * pattern {@code "\\s"} could return empty tokens since it only passes one
 131  * space at a time.
 132  *
 133  * <p> A scanner can read text from any object which implements the {@link
 134  * java.lang.Readable} interface.  If an invocation of the underlying
 135  * readable's {@link java.lang.Readable#read read()} method throws an {@link
 136  * java.io.IOException} then the scanner assumes that the end of the input
 137  * has been reached.  The most recent {@code IOException} thrown by the
 138  * underlying readable can be retrieved via the {@link #ioException} method.
 139  *
 140  * <p>When a {@code Scanner} is closed, it will close its input source
 141  * if the source implements the {@link java.io.Closeable} interface.
 142  *
 143  * <p>A {@code Scanner} is not safe for multithreaded use without
 144  * external synchronization.
 145  *
 146  * <p>Unless otherwise mentioned, passing a {@code null} parameter into
 147  * any method of a {@code Scanner} will cause a
 148  * {@code NullPointerException} to be thrown.
 149  *
 150  * <p>A scanner will default to interpreting numbers as decimal unless a
 151  * different radix has been set by using the {@link #useRadix} method. The
 152  * {@link #reset} method will reset the value of the scanner's radix to
 153  * {@code 10} regardless of whether it was previously changed.
 154  *
 155  * <h3> <a name="localized-numbers">Localized numbers</a> </h3>
 156  *
 157  * <p> An instance of this class is capable of scanning numbers in the standard
 158  * formats as well as in the formats of the scanner's locale. A scanner's
 159  * <a name="initial-locale">initial locale </a>is the value returned by the {@link
 160  * java.util.Locale#getDefault(Locale.Category)
 161  * Locale.getDefault(Locale.Category.FORMAT)} method; it may be changed via the {@link
 162  * #useLocale useLocale()} method. The {@link #reset} method will reset the value of the
 163  * scanner's locale to the initial locale regardless of whether it was
 164  * previously changed.
 165  *
 166  * <p>The localized formats are defined in terms of the following parameters,
 167  * which for a particular locale are taken from that locale's {@link
 168  * java.text.DecimalFormat DecimalFormat} object, {@code df}, and its and
 169  * {@link java.text.DecimalFormatSymbols DecimalFormatSymbols} object,
 170  * {@code dfs}.
 171  *
 172  * <blockquote><dl>
 173  *     <dt><i>LocalGroupSeparator&nbsp;&nbsp;</i>
 174  *         <dd>The character used to separate thousands groups,
 175  *         <i>i.e.,</i>&nbsp;{@code dfs.}{@link
 176  *         java.text.DecimalFormatSymbols#getGroupingSeparator
 177  *         getGroupingSeparator()}
 178  *     <dt><i>LocalDecimalSeparator&nbsp;&nbsp;</i>
 179  *         <dd>The character used for the decimal point,
 180  *     <i>i.e.,</i>&nbsp;{@code dfs.}{@link
 181  *     java.text.DecimalFormatSymbols#getDecimalSeparator
 182  *     getDecimalSeparator()}
 183  *     <dt><i>LocalPositivePrefix&nbsp;&nbsp;</i>
 184  *         <dd>The string that appears before a positive number (may
 185  *         be empty), <i>i.e.,</i>&nbsp;{@code df.}{@link
 186  *         java.text.DecimalFormat#getPositivePrefix
 187  *         getPositivePrefix()}
 188  *     <dt><i>LocalPositiveSuffix&nbsp;&nbsp;</i>
 189  *         <dd>The string that appears after a positive number (may be
 190  *         empty), <i>i.e.,</i>&nbsp;{@code df.}{@link
 191  *         java.text.DecimalFormat#getPositiveSuffix
 192  *         getPositiveSuffix()}
 193  *     <dt><i>LocalNegativePrefix&nbsp;&nbsp;</i>
 194  *         <dd>The string that appears before a negative number (may
 195  *         be empty), <i>i.e.,</i>&nbsp;{@code df.}{@link
 196  *         java.text.DecimalFormat#getNegativePrefix
 197  *         getNegativePrefix()}
 198  *     <dt><i>LocalNegativeSuffix&nbsp;&nbsp;</i>
 199  *         <dd>The string that appears after a negative number (may be
 200  *         empty), <i>i.e.,</i>&nbsp;{@code df.}{@link
 201  *     java.text.DecimalFormat#getNegativeSuffix
 202  *     getNegativeSuffix()}
 203  *     <dt><i>LocalNaN&nbsp;&nbsp;</i>
 204  *         <dd>The string that represents not-a-number for
 205  *         floating-point values,
 206  *         <i>i.e.,</i>&nbsp;{@code dfs.}{@link
 207  *         java.text.DecimalFormatSymbols#getNaN
 208  *         getNaN()}
 209  *     <dt><i>LocalInfinity&nbsp;&nbsp;</i>
 210  *         <dd>The string that represents infinity for floating-point
 211  *         values, <i>i.e.,</i>&nbsp;{@code dfs.}{@link
 212  *         java.text.DecimalFormatSymbols#getInfinity
 213  *         getInfinity()}
 214  * </dl></blockquote>
 215  *
 216  * <h4> <a name="number-syntax">Number syntax</a> </h4>
 217  *
 218  * <p> The strings that can be parsed as numbers by an instance of this class
 219  * are specified in terms of the following regular-expression grammar, where
 220  * Rmax is the highest digit in the radix being used (for example, Rmax is 9 in base 10).
 221  *
 222  * <dl>
 223  *   <dt><i>NonAsciiDigit</i>:
 224  *       <dd>A non-ASCII character c for which
 225  *            {@link java.lang.Character#isDigit Character.isDigit}{@code (c)}
 226  *                        returns&nbsp;true
 227  *
 228  *   <dt><i>Non0Digit</i>:
 229  *       <dd>{@code [1-}<i>Rmax</i>{@code ] | }<i>NonASCIIDigit</i>
 230  *
 231  *   <dt><i>Digit</i>:
 232  *       <dd>{@code [0-}<i>Rmax</i>{@code ] | }<i>NonASCIIDigit</i>
 233  *
 234  *   <dt><i>GroupedNumeral</i>:
 235  *       <dd><code>(&nbsp;</code><i>Non0Digit</i>
 236  *                   <i>Digit</i>{@code ?
 237  *                   }<i>Digit</i>{@code ?}
 238  *       <dd>&nbsp;&nbsp;&nbsp;&nbsp;<code>(&nbsp;</code><i>LocalGroupSeparator</i>
 239  *                         <i>Digit</i>
 240  *                         <i>Digit</i>
 241  *                         <i>Digit</i>{@code  )+ )}
 242  *
 243  *   <dt><i>Numeral</i>:
 244  *       <dd>{@code ( ( }<i>Digit</i>{@code + )
 245  *               | }<i>GroupedNumeral</i>{@code  )}
 246  *
 247  *   <dt><a name="Integer-regex"><i>Integer</i>:</a>
 248  *       <dd>{@code ( [-+]? ( }<i>Numeral</i>{@code
 249  *                               ) )}
 250  *       <dd>{@code | }<i>LocalPositivePrefix</i> <i>Numeral</i>
 251  *                      <i>LocalPositiveSuffix</i>
 252  *       <dd>{@code | }<i>LocalNegativePrefix</i> <i>Numeral</i>
 253  *                 <i>LocalNegativeSuffix</i>
 254  *
 255  *   <dt><i>DecimalNumeral</i>:
 256  *       <dd><i>Numeral</i>
 257  *       <dd>{@code | }<i>Numeral</i>
 258  *                 <i>LocalDecimalSeparator</i>
 259  *                 <i>Digit</i>{@code *}
 260  *       <dd>{@code | }<i>LocalDecimalSeparator</i>
 261  *                 <i>Digit</i>{@code +}
 262  *
 263  *   <dt><i>Exponent</i>:
 264  *       <dd>{@code ( [eE] [+-]? }<i>Digit</i>{@code + )}
 265  *
 266  *   <dt><a name="Decimal-regex"><i>Decimal</i>:</a>
 267  *       <dd>{@code ( [-+]? }<i>DecimalNumeral</i>
 268  *                         <i>Exponent</i>{@code ? )}
 269  *       <dd>{@code | }<i>LocalPositivePrefix</i>
 270  *                 <i>DecimalNumeral</i>
 271  *                 <i>LocalPositiveSuffix</i>
 272  *                 <i>Exponent</i>{@code ?}
 273  *       <dd>{@code | }<i>LocalNegativePrefix</i>
 274  *                 <i>DecimalNumeral</i>
 275  *                 <i>LocalNegativeSuffix</i>
 276  *                 <i>Exponent</i>{@code ?}
 277  *
 278  *   <dt><i>HexFloat</i>:
 279  *       <dd>{@code [-+]? 0[xX][0-9a-fA-F]*\.[0-9a-fA-F]+
 280  *                 ([pP][-+]?[0-9]+)?}
 281  *
 282  *   <dt><i>NonNumber</i>:
 283  *       <dd>{@code NaN
 284  *                          | }<i>LocalNan</i>{@code
 285  *                          | Infinity
 286  *                          | }<i>LocalInfinity</i>
 287  *
 288  *   <dt><i>SignedNonNumber</i>:
 289  *       <dd>{@code ( [-+]? }<i>NonNumber</i>{@code  )}
 290  *       <dd>{@code | }<i>LocalPositivePrefix</i>
 291  *                 <i>NonNumber</i>
 292  *                 <i>LocalPositiveSuffix</i>
 293  *       <dd>{@code | }<i>LocalNegativePrefix</i>
 294  *                 <i>NonNumber</i>
 295  *                 <i>LocalNegativeSuffix</i>
 296  *
 297  *   <dt><a name="Float-regex"><i>Float</i></a>:
 298  *       <dd><i>Decimal</i>
 299  *           {@code | }<i>HexFloat</i>
 300  *           {@code | }<i>SignedNonNumber</i>
 301  *
 302  * </dl>
 303  * <p>Whitespace is not significant in the above regular expressions.
 304  *
 305  * @since   1.5
 306  */
 307 public final class Scanner implements Iterator<String>, Closeable {
 308 
 309     // Internal buffer used to hold input
 310     private CharBuffer buf;
 311 
 312     // Size of internal character buffer
 313     private static final int BUFFER_SIZE = 1024; // change to 1024;
 314 
 315     // The index into the buffer currently held by the Scanner
 316     private int position;
 317 
 318     // Internal matcher used for finding delimiters
 319     private Matcher matcher;
 320 
 321     // Pattern used to delimit tokens
 322     private Pattern delimPattern;
 323 
 324     // Pattern found in last hasNext operation
 325     private Pattern hasNextPattern;
 326 
 327     // Position after last hasNext operation
 328     private int hasNextPosition;
 329 
 330     // Result after last hasNext operation
 331     private String hasNextResult;
 332 
 333     // The input source
 334     private Readable source;
 335 
 336     // Boolean is true if source is done
 337     private boolean sourceClosed = false;
 338 
 339     // Boolean indicating more input is required
 340     private boolean needInput = false;
 341 
 342     // Boolean indicating if a delim has been skipped this operation
 343     private boolean skipped = false;
 344 
 345     // A store of a position that the scanner may fall back to
 346     private int savedScannerPosition = -1;
 347 
 348     // A cache of the last primitive type scanned
 349     private Object typeCache = null;
 350 
 351     // Boolean indicating if a match result is available
 352     private boolean matchValid = false;
 353 
 354     // Boolean indicating if this scanner has been closed
 355     private boolean closed = false;
 356 
 357     // The current radix used by this scanner
 358     private int radix = 10;
 359 
 360     // The default radix for this scanner
 361     private int defaultRadix = 10;
 362 
 363     // The locale used by this scanner
 364     private Locale locale = null;
 365 
 366     // A cache of the last few recently used Patterns
 367     private PatternLRUCache patternCache = new PatternLRUCache(7);
 368 
 369     // A holder of the last IOException encountered
 370     private IOException lastException;
 371 
 372     // Number of times this scanner's state has been modified.
 373     // Generally incremented on most public APIs and checked
 374     // within spliterator implementations.
 375     int modCount;
 376 
 377     // A pattern for java whitespace
 378     private static Pattern WHITESPACE_PATTERN = Pattern.compile(
 379                                                 "\\p{javaWhitespace}+");
 380 
 381     // A pattern for any token
 382     private static Pattern FIND_ANY_PATTERN = Pattern.compile("(?s).*");
 383 
 384     // A pattern for non-ASCII digits
 385     private static Pattern NON_ASCII_DIGIT = Pattern.compile(
 386         "[\\p{javaDigit}&&[^0-9]]");
 387 
 388     // Fields and methods to support scanning primitive types
 389 
 390     /**
 391      * Locale dependent values used to scan numbers
 392      */
 393     private String groupSeparator = "\\,";
 394     private String decimalSeparator = "\\.";
 395     private String nanString = "NaN";
 396     private String infinityString = "Infinity";
 397     private String positivePrefix = "";
 398     private String negativePrefix = "\\-";
 399     private String positiveSuffix = "";
 400     private String negativeSuffix = "";
 401 
 402     /**
 403      * Fields and an accessor method to match booleans
 404      */
 405     private static volatile Pattern boolPattern;
 406     private static final String BOOLEAN_PATTERN = "true|false";
 407     private static Pattern boolPattern() {
 408         Pattern bp = boolPattern;
 409         if (bp == null)
 410             boolPattern = bp = Pattern.compile(BOOLEAN_PATTERN,
 411                                           Pattern.CASE_INSENSITIVE);
 412         return bp;
 413     }
 414 
 415     /**
 416      * Fields and methods to match bytes, shorts, ints, and longs
 417      */
 418     private Pattern integerPattern;
 419     private String digits = "0123456789abcdefghijklmnopqrstuvwxyz";
 420     private String non0Digit = "[\\p{javaDigit}&&[^0]]";
 421     private int SIMPLE_GROUP_INDEX = 5;
 422     private String buildIntegerPatternString() {
 423         String radixDigits = digits.substring(0, radix);
 424         // \\p{javaDigit} is not guaranteed to be appropriate
 425         // here but what can we do? The final authority will be
 426         // whatever parse method is invoked, so ultimately the
 427         // Scanner will do the right thing
 428         String digit = "((?i)["+radixDigits+"]|\\p{javaDigit})";
 429         String groupedNumeral = "("+non0Digit+digit+"?"+digit+"?("+
 430                                 groupSeparator+digit+digit+digit+")+)";
 431         // digit++ is the possessive form which is necessary for reducing
 432         // backtracking that would otherwise cause unacceptable performance
 433         String numeral = "(("+ digit+"++)|"+groupedNumeral+")";
 434         String javaStyleInteger = "([-+]?(" + numeral + "))";
 435         String negativeInteger = negativePrefix + numeral + negativeSuffix;
 436         String positiveInteger = positivePrefix + numeral + positiveSuffix;
 437         return "("+ javaStyleInteger + ")|(" +
 438             positiveInteger + ")|(" +
 439             negativeInteger + ")";
 440     }
 441     private Pattern integerPattern() {
 442         if (integerPattern == null) {
 443             integerPattern = patternCache.forName(buildIntegerPatternString());
 444         }
 445         return integerPattern;
 446     }
 447 
 448     /**
 449      * Fields and an accessor method to match line separators
 450      */
 451     private static volatile Pattern separatorPattern;
 452     private static volatile Pattern linePattern;
 453     private static final String LINE_SEPARATOR_PATTERN =
 454                                            "\r\n|[\n\r\u2028\u2029\u0085]";
 455     private static final String LINE_PATTERN = ".*("+LINE_SEPARATOR_PATTERN+")|.+$";
 456 
 457     private static Pattern separatorPattern() {
 458         Pattern sp = separatorPattern;
 459         if (sp == null)
 460             separatorPattern = sp = Pattern.compile(LINE_SEPARATOR_PATTERN);
 461         return sp;
 462     }
 463 
 464     private static Pattern linePattern() {
 465         Pattern lp = linePattern;
 466         if (lp == null)
 467             linePattern = lp = Pattern.compile(LINE_PATTERN);
 468         return lp;
 469     }
 470 
 471     /**
 472      * Fields and methods to match floats and doubles
 473      */
 474     private Pattern floatPattern;
 475     private Pattern decimalPattern;
 476     private void buildFloatAndDecimalPattern() {
 477         // \\p{javaDigit} may not be perfect, see above
 478         String digit = "([0-9]|(\\p{javaDigit}))";
 479         String exponent = "([eE][+-]?"+digit+"+)?";
 480         String groupedNumeral = "("+non0Digit+digit+"?"+digit+"?("+
 481                                 groupSeparator+digit+digit+digit+")+)";
 482         // Once again digit++ is used for performance, as above
 483         String numeral = "(("+digit+"++)|"+groupedNumeral+")";
 484         String decimalNumeral = "("+numeral+"|"+numeral +
 485             decimalSeparator + digit + "*+|"+ decimalSeparator +
 486             digit + "++)";
 487         String nonNumber = "(NaN|"+nanString+"|Infinity|"+
 488                                infinityString+")";
 489         String positiveFloat = "(" + positivePrefix + decimalNumeral +
 490                             positiveSuffix + exponent + ")";
 491         String negativeFloat = "(" + negativePrefix + decimalNumeral +
 492                             negativeSuffix + exponent + ")";
 493         String decimal = "(([-+]?" + decimalNumeral + exponent + ")|"+
 494             positiveFloat + "|" + negativeFloat + ")";
 495         String hexFloat =
 496             "[-+]?0[xX][0-9a-fA-F]*\\.[0-9a-fA-F]+([pP][-+]?[0-9]+)?";
 497         String positiveNonNumber = "(" + positivePrefix + nonNumber +
 498                             positiveSuffix + ")";
 499         String negativeNonNumber = "(" + negativePrefix + nonNumber +
 500                             negativeSuffix + ")";
 501         String signedNonNumber = "(([-+]?"+nonNumber+")|" +
 502                                  positiveNonNumber + "|" +
 503                                  negativeNonNumber + ")";
 504         floatPattern = Pattern.compile(decimal + "|" + hexFloat + "|" +
 505                                        signedNonNumber);
 506         decimalPattern = Pattern.compile(decimal);
 507     }
 508     private Pattern floatPattern() {
 509         if (floatPattern == null) {
 510             buildFloatAndDecimalPattern();
 511         }
 512         return floatPattern;
 513     }
 514     private Pattern decimalPattern() {
 515         if (decimalPattern == null) {
 516             buildFloatAndDecimalPattern();
 517         }
 518         return decimalPattern;
 519     }
 520 
 521     // Constructors
 522 
 523     /**
 524      * Constructs a {@code Scanner} that returns values scanned
 525      * from the specified source delimited by the specified pattern.
 526      *
 527      * @param source A character source implementing the Readable interface
 528      * @param pattern A delimiting pattern
 529      */
 530     private Scanner(Readable source, Pattern pattern) {
 531         assert source != null : "source should not be null";
 532         assert pattern != null : "pattern should not be null";
 533         this.source = source;
 534         delimPattern = pattern;
 535         buf = CharBuffer.allocate(BUFFER_SIZE);
 536         buf.limit(0);
 537         matcher = delimPattern.matcher(buf);
 538         matcher.useTransparentBounds(true);
 539         matcher.useAnchoringBounds(false);
 540         useLocale(Locale.getDefault(Locale.Category.FORMAT));
 541     }
 542 
 543     /**
 544      * Constructs a new {@code Scanner} that produces values scanned
 545      * from the specified source.
 546      *
 547      * @param  source A character source implementing the {@link Readable}
 548      *         interface
 549      */
 550     public Scanner(Readable source) {
 551         this(Objects.requireNonNull(source, "source"), WHITESPACE_PATTERN);
 552     }
 553 
 554     /**
 555      * Constructs a new {@code Scanner} that produces values scanned
 556      * from the specified input stream. Bytes from the stream are converted
 557      * into characters using the underlying platform's
 558      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
 559      *
 560      * @param  source An input stream to be scanned
 561      */
 562     public Scanner(InputStream source) {
 563         this(new InputStreamReader(source), WHITESPACE_PATTERN);
 564     }
 565 
 566     /**
 567      * Constructs a new {@code Scanner} that produces values scanned
 568      * from the specified input stream. Bytes from the stream are converted
 569      * into characters using the specified charset.
 570      *
 571      * @param  source An input stream to be scanned
 572      * @param charsetName The encoding type used to convert bytes from the
 573      *        stream into characters to be scanned
 574      * @throws IllegalArgumentException if the specified character set
 575      *         does not exist
 576      */
 577     public Scanner(InputStream source, String charsetName) {
 578         this(makeReadable(Objects.requireNonNull(source, "source"), toCharset(charsetName)),
 579              WHITESPACE_PATTERN);
 580     }
 581 
 582     /**
 583      * Returns a charset object for the given charset name.
 584      * @throws NullPointerException          is csn is null
 585      * @throws IllegalArgumentException      if the charset is not supported
 586      */
 587     private static Charset toCharset(String csn) {
 588         Objects.requireNonNull(csn, "charsetName");
 589         try {
 590             return Charset.forName(csn);
 591         } catch (IllegalCharsetNameException|UnsupportedCharsetException e) {
 592             // IllegalArgumentException should be thrown
 593             throw new IllegalArgumentException(e);
 594         }
 595     }
 596 
 597     private static Readable makeReadable(InputStream source, Charset charset) {
 598         return new InputStreamReader(source, charset);
 599     }
 600 
 601     /**
 602      * Constructs a new {@code Scanner} that produces values scanned
 603      * from the specified file. Bytes from the file are converted into
 604      * characters using the underlying platform's
 605      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
 606      *
 607      * @param  source A file to be scanned
 608      * @throws FileNotFoundException if source is not found
 609      */
 610     public Scanner(File source) throws FileNotFoundException {
 611         this((ReadableByteChannel)(new FileInputStream(source).getChannel()));
 612     }
 613 
 614     /**
 615      * Constructs a new {@code Scanner} that produces values scanned
 616      * from the specified file. Bytes from the file are converted into
 617      * characters using the specified charset.
 618      *
 619      * @param  source A file to be scanned
 620      * @param charsetName The encoding type used to convert bytes from the file
 621      *        into characters to be scanned
 622      * @throws FileNotFoundException if source is not found
 623      * @throws IllegalArgumentException if the specified encoding is
 624      *         not found
 625      */
 626     public Scanner(File source, String charsetName)
 627         throws FileNotFoundException
 628     {
 629         this(Objects.requireNonNull(source), toDecoder(charsetName));
 630     }
 631 
 632     private Scanner(File source, CharsetDecoder dec)
 633         throws FileNotFoundException
 634     {
 635         this(makeReadable((ReadableByteChannel)(new FileInputStream(source).getChannel()), dec));
 636     }
 637 
 638     private static CharsetDecoder toDecoder(String charsetName) {
 639         Objects.requireNonNull(charsetName, "charsetName");
 640         try {
 641             return Charset.forName(charsetName).newDecoder();
 642         } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
 643             throw new IllegalArgumentException(charsetName);
 644         }
 645     }
 646 
 647     private static Readable makeReadable(ReadableByteChannel source,
 648                                          CharsetDecoder dec) {
 649         return Channels.newReader(source, dec, -1);
 650     }
 651 
 652     /**
 653      * Constructs a new {@code Scanner} that produces values scanned
 654      * from the specified file. Bytes from the file are converted into
 655      * characters using the underlying platform's
 656      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
 657      *
 658      * @param   source
 659      *          the path to the file to be scanned
 660      * @throws  IOException
 661      *          if an I/O error occurs opening source
 662      *
 663      * @since   1.7
 664      */
 665     public Scanner(Path source)
 666         throws IOException
 667     {
 668         this(Files.newInputStream(source));
 669     }
 670 
 671     /**
 672      * Constructs a new {@code Scanner} that produces values scanned
 673      * from the specified file. Bytes from the file are converted into
 674      * characters using the specified charset.
 675      *
 676      * @param   source
 677      *          the path to the file to be scanned
 678      * @param   charsetName
 679      *          The encoding type used to convert bytes from the file
 680      *          into characters to be scanned
 681      * @throws  IOException
 682      *          if an I/O error occurs opening source
 683      * @throws  IllegalArgumentException
 684      *          if the specified encoding is not found
 685      * @since   1.7
 686      */
 687     public Scanner(Path source, String charsetName) throws IOException {
 688         this(Objects.requireNonNull(source), toCharset(charsetName));
 689     }
 690 
 691     private Scanner(Path source, Charset charset)  throws IOException {
 692         this(makeReadable(Files.newInputStream(source), charset));
 693     }
 694 
 695     /**
 696      * Constructs a new {@code Scanner} that produces values scanned
 697      * from the specified string.
 698      *
 699      * @param  source A string to scan
 700      */
 701     public Scanner(String source) {
 702         this(new StringReader(source), WHITESPACE_PATTERN);
 703     }
 704 
 705     /**
 706      * Constructs a new {@code Scanner} that produces values scanned
 707      * from the specified channel. Bytes from the source are converted into
 708      * characters using the underlying platform's
 709      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
 710      *
 711      * @param  source A channel to scan
 712      */
 713     public Scanner(ReadableByteChannel source) {
 714         this(makeReadable(Objects.requireNonNull(source, "source")),
 715              WHITESPACE_PATTERN);
 716     }
 717 
 718     private static Readable makeReadable(ReadableByteChannel source) {
 719         return makeReadable(source, Charset.defaultCharset().newDecoder());
 720     }
 721 
 722     /**
 723      * Constructs a new {@code Scanner} that produces values scanned
 724      * from the specified channel. Bytes from the source are converted into
 725      * characters using the specified charset.
 726      *
 727      * @param  source A channel to scan
 728      * @param charsetName The encoding type used to convert bytes from the
 729      *        channel into characters to be scanned
 730      * @throws IllegalArgumentException if the specified character set
 731      *         does not exist
 732      */
 733     public Scanner(ReadableByteChannel source, String charsetName) {
 734         this(makeReadable(Objects.requireNonNull(source, "source"), toDecoder(charsetName)),
 735              WHITESPACE_PATTERN);
 736     }
 737 
 738     // Private primitives used to support scanning
 739 
 740     private void saveState() {
 741         savedScannerPosition = position;
 742     }
 743 
 744     private void revertState() {
 745         this.position = savedScannerPosition;
 746         savedScannerPosition = -1;
 747         skipped = false;
 748     }
 749 
 750     private boolean revertState(boolean b) {
 751         this.position = savedScannerPosition;
 752         savedScannerPosition = -1;
 753         skipped = false;
 754         return b;
 755     }
 756 
 757     private void cacheResult() {
 758         hasNextResult = matcher.group();
 759         hasNextPosition = matcher.end();
 760         hasNextPattern = matcher.pattern();
 761     }
 762 
 763     private void cacheResult(String result) {
 764         hasNextResult = result;
 765         hasNextPosition = matcher.end();
 766         hasNextPattern = matcher.pattern();
 767     }
 768 
 769     // Clears both regular cache and type cache
 770     private void clearCaches() {
 771         hasNextPattern = null;
 772         typeCache = null;
 773     }
 774 
 775     // Also clears both the regular cache and the type cache
 776     private String getCachedResult() {
 777         position = hasNextPosition;
 778         hasNextPattern = null;
 779         typeCache = null;
 780         return hasNextResult;
 781     }
 782 
 783     // Also clears both the regular cache and the type cache
 784     private void useTypeCache() {
 785         if (closed)
 786             throw new IllegalStateException("Scanner closed");
 787         position = hasNextPosition;
 788         hasNextPattern = null;
 789         typeCache = null;
 790     }
 791 
 792     // Tries to read more input. May block.
 793     private void readInput() {
 794         if (buf.limit() == buf.capacity())
 795             makeSpace();
 796         // Prepare to receive data
 797         int p = buf.position();
 798         buf.position(buf.limit());
 799         buf.limit(buf.capacity());
 800 
 801         int n = 0;
 802         try {
 803             n = source.read(buf);
 804         } catch (IOException ioe) {
 805             lastException = ioe;
 806             n = -1;
 807         }
 808         if (n == -1) {
 809             sourceClosed = true;
 810             needInput = false;
 811         }
 812         if (n > 0)
 813             needInput = false;
 814         // Restore current position and limit for reading
 815         buf.limit(buf.position());
 816         buf.position(p);
 817     }
 818 
 819     // After this method is called there will either be an exception
 820     // or else there will be space in the buffer
 821     private boolean makeSpace() {
 822         clearCaches();
 823         int offset = savedScannerPosition == -1 ?
 824             position : savedScannerPosition;
 825         buf.position(offset);
 826         // Gain space by compacting buffer
 827         if (offset > 0) {
 828             buf.compact();
 829             translateSavedIndexes(offset);
 830             position -= offset;
 831             buf.flip();
 832             return true;
 833         }
 834         // Gain space by growing buffer
 835         int newSize = buf.capacity() * 2;
 836         CharBuffer newBuf = CharBuffer.allocate(newSize);
 837         newBuf.put(buf);
 838         newBuf.flip();
 839         translateSavedIndexes(offset);
 840         position -= offset;
 841         buf = newBuf;
 842         matcher.reset(buf);
 843         return true;
 844     }
 845 
 846     // When a buffer compaction/reallocation occurs the saved indexes must
 847     // be modified appropriately
 848     private void translateSavedIndexes(int offset) {
 849         if (savedScannerPosition != -1)
 850             savedScannerPosition -= offset;
 851     }
 852 
 853     // If we are at the end of input then NoSuchElement;
 854     // If there is still input left then InputMismatch
 855     private void throwFor() {
 856         skipped = false;
 857         if ((sourceClosed) && (position == buf.limit()))
 858             throw new NoSuchElementException();
 859         else
 860             throw new InputMismatchException();
 861     }
 862 
 863     // Returns true if a complete token or partial token is in the buffer.
 864     // It is not necessary to find a complete token since a partial token
 865     // means that there will be another token with or without more input.
 866     private boolean hasTokenInBuffer() {
 867         matchValid = false;
 868         matcher.usePattern(delimPattern);
 869         matcher.region(position, buf.limit());
 870         // Skip delims first
 871         if (matcher.lookingAt()) {
 872             if (matcher.hitEnd() && !sourceClosed) {
 873                 // more input might change the match of delims, in which
 874                 // might change whether or not if there is token left in
 875                 // buffer (don't update the "position" in this case)
 876                 needInput = true;
 877                 return false;
 878             }
 879             position = matcher.end();
 880         }
 881         // If we are sitting at the end, no more tokens in buffer
 882         if (position == buf.limit())
 883             return false;
 884         return true;
 885     }
 886 
 887     /*
 888      * Returns a "complete token" that matches the specified pattern
 889      *
 890      * A token is complete if surrounded by delims; a partial token
 891      * is prefixed by delims but not postfixed by them
 892      *
 893      * The position is advanced to the end of that complete token
 894      *
 895      * Pattern == null means accept any token at all
 896      *
 897      * Triple return:
 898      * 1. valid string means it was found
 899      * 2. null with needInput=false means we won't ever find it
 900      * 3. null with needInput=true means try again after readInput
 901      */
 902     private String getCompleteTokenInBuffer(Pattern pattern) {
 903         matchValid = false;
 904         // Skip delims first
 905         matcher.usePattern(delimPattern);
 906         if (!skipped) { // Enforcing only one skip of leading delims
 907             matcher.region(position, buf.limit());
 908             if (matcher.lookingAt()) {
 909                 // If more input could extend the delimiters then we must wait
 910                 // for more input
 911                 if (matcher.hitEnd() && !sourceClosed) {
 912                     needInput = true;
 913                     return null;
 914                 }
 915                 // The delims were whole and the matcher should skip them
 916                 skipped = true;
 917                 position = matcher.end();
 918             }
 919         }
 920 
 921         // If we are sitting at the end, no more tokens in buffer
 922         if (position == buf.limit()) {
 923             if (sourceClosed)
 924                 return null;
 925             needInput = true;
 926             return null;
 927         }
 928         // Must look for next delims. Simply attempting to match the
 929         // pattern at this point may find a match but it might not be
 930         // the first longest match because of missing input, or it might
 931         // match a partial token instead of the whole thing.
 932 
 933         // Then look for next delims
 934         matcher.region(position, buf.limit());
 935         boolean foundNextDelim = matcher.find();
 936         if (foundNextDelim && (matcher.end() == position)) {
 937             // Zero length delimiter match; we should find the next one
 938             // using the automatic advance past a zero length match;
 939             // Otherwise we have just found the same one we just skipped
 940             foundNextDelim = matcher.find();
 941         }
 942         if (foundNextDelim) {
 943             // In the rare case that more input could cause the match
 944             // to be lost and there is more input coming we must wait
 945             // for more input. Note that hitting the end is okay as long
 946             // as the match cannot go away. It is the beginning of the
 947             // next delims we want to be sure about, we don't care if
 948             // they potentially extend further.
 949             if (matcher.requireEnd() && !sourceClosed) {
 950                 needInput = true;
 951                 return null;
 952             }
 953             int tokenEnd = matcher.start();
 954             // There is a complete token.
 955             if (pattern == null) {
 956                 // Must continue with match to provide valid MatchResult
 957                 pattern = FIND_ANY_PATTERN;
 958             }
 959             //  Attempt to match against the desired pattern
 960             matcher.usePattern(pattern);
 961             matcher.region(position, tokenEnd);
 962             if (matcher.matches()) {
 963                 String s = matcher.group();
 964                 position = matcher.end();
 965                 return s;
 966             } else { // Complete token but it does not match
 967                 return null;
 968             }
 969         }
 970 
 971         // If we can't find the next delims but no more input is coming,
 972         // then we can treat the remainder as a whole token
 973         if (sourceClosed) {
 974             if (pattern == null) {
 975                 // Must continue with match to provide valid MatchResult
 976                 pattern = FIND_ANY_PATTERN;
 977             }
 978             // Last token; Match the pattern here or throw
 979             matcher.usePattern(pattern);
 980             matcher.region(position, buf.limit());
 981             if (matcher.matches()) {
 982                 String s = matcher.group();
 983                 position = matcher.end();
 984                 return s;
 985             }
 986             // Last piece does not match
 987             return null;
 988         }
 989 
 990         // There is a partial token in the buffer; must read more
 991         // to complete it
 992         needInput = true;
 993         return null;
 994     }
 995 
 996     // Finds the specified pattern in the buffer up to horizon.
 997     // Returns true if the specified input pattern was matched,
 998     // and leaves the matcher field with the current match state.
 999     private boolean findPatternInBuffer(Pattern pattern, int horizon) {
1000         matchValid = false;
1001         matcher.usePattern(pattern);
1002         int bufferLimit = buf.limit();
1003         int horizonLimit = -1;
1004         int searchLimit = bufferLimit;
1005         if (horizon > 0) {
1006             horizonLimit = position + horizon;
1007             if (horizonLimit < bufferLimit)
1008                 searchLimit = horizonLimit;
1009         }
1010         matcher.region(position, searchLimit);
1011         if (matcher.find()) {
1012             if (matcher.hitEnd() && (!sourceClosed)) {
1013                 // The match may be longer if didn't hit horizon or real end
1014                 if (searchLimit != horizonLimit) {
1015                      // Hit an artificial end; try to extend the match
1016                     needInput = true;
1017                     return false;
1018                 }
1019                 // The match could go away depending on what is next
1020                 if ((searchLimit == horizonLimit) && matcher.requireEnd()) {
1021                     // Rare case: we hit the end of input and it happens
1022                     // that it is at the horizon and the end of input is
1023                     // required for the match.
1024                     needInput = true;
1025                     return false;
1026                 }
1027             }
1028             // Did not hit end, or hit real end, or hit horizon
1029             position = matcher.end();
1030             return true;
1031         }
1032 
1033         if (sourceClosed)
1034             return false;
1035 
1036         // If there is no specified horizon, or if we have not searched
1037         // to the specified horizon yet, get more input
1038         if ((horizon == 0) || (searchLimit != horizonLimit))
1039             needInput = true;
1040         return false;
1041     }
1042 
1043     // Attempts to match a pattern anchored at the current position.
1044     // Returns true if the specified input pattern was matched,
1045     // and leaves the matcher field with the current match state.
1046     private boolean matchPatternInBuffer(Pattern pattern) {
1047         matchValid = false;
1048         matcher.usePattern(pattern);
1049         matcher.region(position, buf.limit());
1050         if (matcher.lookingAt()) {
1051             if (matcher.hitEnd() && (!sourceClosed)) {
1052                 // Get more input and try again
1053                 needInput = true;
1054                 return false;
1055             }
1056             position = matcher.end();
1057             return true;
1058         }
1059 
1060         if (sourceClosed)
1061             return false;
1062 
1063         // Read more to find pattern
1064         needInput = true;
1065         return false;
1066     }
1067 
1068     // Throws if the scanner is closed
1069     private void ensureOpen() {
1070         if (closed)
1071             throw new IllegalStateException("Scanner closed");
1072     }
1073 
1074     // Public methods
1075 
1076     /**
1077      * Closes this scanner.
1078      *
1079      * <p> If this scanner has not yet been closed then if its underlying
1080      * {@linkplain java.lang.Readable readable} also implements the {@link
1081      * java.io.Closeable} interface then the readable's {@code close} method
1082      * will be invoked.  If this scanner is already closed then invoking this
1083      * method will have no effect.
1084      *
1085      * <p>Attempting to perform search operations after a scanner has
1086      * been closed will result in an {@link IllegalStateException}.
1087      *
1088      */
1089     public void close() {
1090         if (closed)
1091             return;
1092         if (source instanceof Closeable) {
1093             try {
1094                 ((Closeable)source).close();
1095             } catch (IOException ioe) {
1096                 lastException = ioe;
1097             }
1098         }
1099         sourceClosed = true;
1100         source = null;
1101         closed = true;
1102     }
1103 
1104     /**
1105      * Returns the {@code IOException} last thrown by this
1106      * {@code Scanner}'s underlying {@code Readable}. This method
1107      * returns {@code null} if no such exception exists.
1108      *
1109      * @return the last exception thrown by this scanner's readable
1110      */
1111     public IOException ioException() {
1112         return lastException;
1113     }
1114 
1115     /**
1116      * Returns the {@code Pattern} this {@code Scanner} is currently
1117      * using to match delimiters.
1118      *
1119      * @return this scanner's delimiting pattern.
1120      */
1121     public Pattern delimiter() {
1122         return delimPattern;
1123     }
1124 
1125     /**
1126      * Sets this scanner's delimiting pattern to the specified pattern.
1127      *
1128      * @param pattern A delimiting pattern
1129      * @return this scanner
1130      */
1131     public Scanner useDelimiter(Pattern pattern) {
1132         modCount++;
1133         delimPattern = pattern;
1134         return this;
1135     }
1136 
1137     /**
1138      * Sets this scanner's delimiting pattern to a pattern constructed from
1139      * the specified {@code String}.
1140      *
1141      * <p> An invocation of this method of the form
1142      * {@code useDelimiter(pattern)} behaves in exactly the same way as the
1143      * invocation {@code useDelimiter(Pattern.compile(pattern))}.
1144      *
1145      * <p> Invoking the {@link #reset} method will set the scanner's delimiter
1146      * to the <a href= "#default-delimiter">default</a>.
1147      *
1148      * @param pattern A string specifying a delimiting pattern
1149      * @return this scanner
1150      */
1151     public Scanner useDelimiter(String pattern) {
1152         modCount++;
1153         delimPattern = patternCache.forName(pattern);
1154         return this;
1155     }
1156 
1157     /**
1158      * Returns this scanner's locale.
1159      *
1160      * <p>A scanner's locale affects many elements of its default
1161      * primitive matching regular expressions; see
1162      * <a href= "#localized-numbers">localized numbers</a> above.
1163      *
1164      * @return this scanner's locale
1165      */
1166     public Locale locale() {
1167         return this.locale;
1168     }
1169 
1170     /**
1171      * Sets this scanner's locale to the specified locale.
1172      *
1173      * <p>A scanner's locale affects many elements of its default
1174      * primitive matching regular expressions; see
1175      * <a href= "#localized-numbers">localized numbers</a> above.
1176      *
1177      * <p>Invoking the {@link #reset} method will set the scanner's locale to
1178      * the <a href= "#initial-locale">initial locale</a>.
1179      *
1180      * @param locale A string specifying the locale to use
1181      * @return this scanner
1182      */
1183     public Scanner useLocale(Locale locale) {
1184         if (locale.equals(this.locale))
1185             return this;
1186 
1187         modCount++;
1188         this.locale = locale;
1189         DecimalFormat df =
1190             (DecimalFormat)NumberFormat.getNumberInstance(locale);
1191         DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(locale);
1192 
1193         // These must be literalized to avoid collision with regex
1194         // metacharacters such as dot or parenthesis
1195         groupSeparator =   "\\" + dfs.getGroupingSeparator();
1196         decimalSeparator = "\\" + dfs.getDecimalSeparator();
1197 
1198         // Quoting the nonzero length locale-specific things
1199         // to avoid potential conflict with metacharacters
1200         nanString = "\\Q" + dfs.getNaN() + "\\E";
1201         infinityString = "\\Q" + dfs.getInfinity() + "\\E";
1202         positivePrefix = df.getPositivePrefix();
1203         if (positivePrefix.length() > 0)
1204             positivePrefix = "\\Q" + positivePrefix + "\\E";
1205         negativePrefix = df.getNegativePrefix();
1206         if (negativePrefix.length() > 0)
1207             negativePrefix = "\\Q" + negativePrefix + "\\E";
1208         positiveSuffix = df.getPositiveSuffix();
1209         if (positiveSuffix.length() > 0)
1210             positiveSuffix = "\\Q" + positiveSuffix + "\\E";
1211         negativeSuffix = df.getNegativeSuffix();
1212         if (negativeSuffix.length() > 0)
1213             negativeSuffix = "\\Q" + negativeSuffix + "\\E";
1214 
1215         // Force rebuilding and recompilation of locale dependent
1216         // primitive patterns
1217         integerPattern = null;
1218         floatPattern = null;
1219 
1220         return this;
1221     }
1222 
1223     /**
1224      * Returns this scanner's default radix.
1225      *
1226      * <p>A scanner's radix affects elements of its default
1227      * number matching regular expressions; see
1228      * <a href= "#localized-numbers">localized numbers</a> above.
1229      *
1230      * @return the default radix of this scanner
1231      */
1232     public int radix() {
1233         return this.defaultRadix;
1234     }
1235 
1236     /**
1237      * Sets this scanner's default radix to the specified radix.
1238      *
1239      * <p>A scanner's radix affects elements of its default
1240      * number matching regular expressions; see
1241      * <a href= "#localized-numbers">localized numbers</a> above.
1242      *
1243      * <p>If the radix is less than {@link Character#MIN_RADIX Character.MIN_RADIX}
1244      * or greater than {@link Character#MAX_RADIX Character.MAX_RADIX}, then an
1245      * {@code IllegalArgumentException} is thrown.
1246      *
1247      * <p>Invoking the {@link #reset} method will set the scanner's radix to
1248      * {@code 10}.
1249      *
1250      * @param radix The radix to use when scanning numbers
1251      * @return this scanner
1252      * @throws IllegalArgumentException if radix is out of range
1253      */
1254     public Scanner useRadix(int radix) {
1255         if ((radix < Character.MIN_RADIX) || (radix > Character.MAX_RADIX))
1256             throw new IllegalArgumentException("radix:"+radix);
1257 
1258         if (this.defaultRadix == radix)
1259             return this;
1260         modCount++;
1261         this.defaultRadix = radix;
1262         // Force rebuilding and recompilation of radix dependent patterns
1263         integerPattern = null;
1264         return this;
1265     }
1266 
1267     // The next operation should occur in the specified radix but
1268     // the default is left untouched.
1269     private void setRadix(int radix) {
1270         if (this.radix != radix) {
1271             // Force rebuilding and recompilation of radix dependent patterns
1272             integerPattern = null;
1273             this.radix = radix;
1274         }
1275     }
1276 
1277     /**
1278      * Returns the match result of the last scanning operation performed
1279      * by this scanner. This method throws {@code IllegalStateException}
1280      * if no match has been performed, or if the last match was
1281      * not successful.
1282      *
1283      * <p>The various {@code next} methods of {@code Scanner}
1284      * make a match result available if they complete without throwing an
1285      * exception. For instance, after an invocation of the {@link #nextInt}
1286      * method that returned an int, this method returns a
1287      * {@code MatchResult} for the search of the
1288      * <a href="#Integer-regex"><i>Integer</i></a> regular expression
1289      * defined above. Similarly the {@link #findInLine findInLine()},
1290      * {@link #findWithinHorizon findWithinHorizon()}, and {@link #skip skip()}
1291      * methods will make a match available if they succeed.
1292      *
1293      * @return a match result for the last match operation
1294      * @throws IllegalStateException  If no match result is available
1295      */
1296     public MatchResult match() {
1297         if (!matchValid)
1298             throw new IllegalStateException("No match result available");
1299         return matcher.toMatchResult();
1300     }
1301 
1302     /**
1303      * <p>Returns the string representation of this {@code Scanner}. The
1304      * string representation of a {@code Scanner} contains information
1305      * that may be useful for debugging. The exact format is unspecified.
1306      *
1307      * @return  The string representation of this scanner
1308      */
1309     public String toString() {
1310         StringBuilder sb = new StringBuilder();
1311         sb.append("java.util.Scanner");
1312         sb.append("[delimiters=" + delimPattern + "]");
1313         sb.append("[position=" + position + "]");
1314         sb.append("[match valid=" + matchValid + "]");
1315         sb.append("[need input=" + needInput + "]");
1316         sb.append("[source closed=" + sourceClosed + "]");
1317         sb.append("[skipped=" + skipped + "]");
1318         sb.append("[group separator=" + groupSeparator + "]");
1319         sb.append("[decimal separator=" + decimalSeparator + "]");
1320         sb.append("[positive prefix=" + positivePrefix + "]");
1321         sb.append("[negative prefix=" + negativePrefix + "]");
1322         sb.append("[positive suffix=" + positiveSuffix + "]");
1323         sb.append("[negative suffix=" + negativeSuffix + "]");
1324         sb.append("[NaN string=" + nanString + "]");
1325         sb.append("[infinity string=" + infinityString + "]");
1326         return sb.toString();
1327     }
1328 
1329     /**
1330      * Returns true if this scanner has another token in its input.
1331      * This method may block while waiting for input to scan.
1332      * The scanner does not advance past any input.
1333      *
1334      * @return true if and only if this scanner has another token
1335      * @throws IllegalStateException if this scanner is closed
1336      * @see java.util.Iterator
1337      */
1338     public boolean hasNext() {
1339         ensureOpen();
1340         saveState();
1341         modCount++;
1342         while (!sourceClosed) {
1343             if (hasTokenInBuffer()) {
1344                 return revertState(true);
1345             }
1346             readInput();
1347         }
1348         boolean result = hasTokenInBuffer();
1349         return revertState(result);
1350     }
1351 
1352     /**
1353      * Finds and returns the next complete token from this scanner.
1354      * A complete token is preceded and followed by input that matches
1355      * the delimiter pattern. This method may block while waiting for input
1356      * to scan, even if a previous invocation of {@link #hasNext} returned
1357      * {@code true}.
1358      *
1359      * @return the next token
1360      * @throws NoSuchElementException if no more tokens are available
1361      * @throws IllegalStateException if this scanner is closed
1362      * @see java.util.Iterator
1363      */
1364     public String next() {
1365         ensureOpen();
1366         clearCaches();
1367         modCount++;
1368         while (true) {
1369             String token = getCompleteTokenInBuffer(null);
1370             if (token != null) {
1371                 matchValid = true;
1372                 skipped = false;
1373                 return token;
1374             }
1375             if (needInput)
1376                 readInput();
1377             else
1378                 throwFor();
1379         }
1380     }
1381 
1382     /**
1383      * The remove operation is not supported by this implementation of
1384      * {@code Iterator}.
1385      *
1386      * @throws UnsupportedOperationException if this method is invoked.
1387      * @see java.util.Iterator
1388      */
1389     public void remove() {
1390         throw new UnsupportedOperationException();
1391     }
1392 
1393     /**
1394      * Returns true if the next token matches the pattern constructed from the
1395      * specified string. The scanner does not advance past any input.
1396      *
1397      * <p> An invocation of this method of the form {@code hasNext(pattern)}
1398      * behaves in exactly the same way as the invocation
1399      * {@code hasNext(Pattern.compile(pattern))}.
1400      *
1401      * @param pattern a string specifying the pattern to scan
1402      * @return true if and only if this scanner has another token matching
1403      *         the specified pattern
1404      * @throws IllegalStateException if this scanner is closed
1405      */
1406     public boolean hasNext(String pattern)  {
1407         return hasNext(patternCache.forName(pattern));
1408     }
1409 
1410     /**
1411      * Returns the next token if it matches the pattern constructed from the
1412      * specified string.  If the match is successful, the scanner advances
1413      * past the input that matched the pattern.
1414      *
1415      * <p> An invocation of this method of the form {@code next(pattern)}
1416      * behaves in exactly the same way as the invocation
1417      * {@code next(Pattern.compile(pattern))}.
1418      *
1419      * @param pattern a string specifying the pattern to scan
1420      * @return the next token
1421      * @throws NoSuchElementException if no such tokens are available
1422      * @throws IllegalStateException if this scanner is closed
1423      */
1424     public String next(String pattern)  {
1425         return next(patternCache.forName(pattern));
1426     }
1427 
1428     /**
1429      * Returns true if the next complete token matches the specified pattern.
1430      * A complete token is prefixed and postfixed by input that matches
1431      * the delimiter pattern. This method may block while waiting for input.
1432      * The scanner does not advance past any input.
1433      *
1434      * @param pattern the pattern to scan for
1435      * @return true if and only if this scanner has another token matching
1436      *         the specified pattern
1437      * @throws IllegalStateException if this scanner is closed
1438      */
1439     public boolean hasNext(Pattern pattern) {
1440         ensureOpen();
1441         if (pattern == null)
1442             throw new NullPointerException();
1443         hasNextPattern = null;
1444         saveState();
1445         modCount++;
1446 
1447         while (true) {
1448             if (getCompleteTokenInBuffer(pattern) != null) {
1449                 matchValid = true;
1450                 cacheResult();
1451                 return revertState(true);
1452             }
1453             if (needInput)
1454                 readInput();
1455             else
1456                 return revertState(false);
1457         }
1458     }
1459 
1460     /**
1461      * Returns the next token if it matches the specified pattern. This
1462      * method may block while waiting for input to scan, even if a previous
1463      * invocation of {@link #hasNext(Pattern)} returned {@code true}.
1464      * If the match is successful, the scanner advances past the input that
1465      * matched the pattern.
1466      *
1467      * @param pattern the pattern to scan for
1468      * @return the next token
1469      * @throws NoSuchElementException if no more tokens are available
1470      * @throws IllegalStateException if this scanner is closed
1471      */
1472     public String next(Pattern pattern) {
1473         ensureOpen();
1474         if (pattern == null)
1475             throw new NullPointerException();
1476 
1477         modCount++;
1478         // Did we already find this pattern?
1479         if (hasNextPattern == pattern)
1480             return getCachedResult();
1481         clearCaches();
1482 
1483         // Search for the pattern
1484         while (true) {
1485             String token = getCompleteTokenInBuffer(pattern);
1486             if (token != null) {
1487                 matchValid = true;
1488                 skipped = false;
1489                 return token;
1490             }
1491             if (needInput)
1492                 readInput();
1493             else
1494                 throwFor();
1495         }
1496     }
1497 
1498     /**
1499      * Returns true if there is another line in the input of this scanner.
1500      * This method may block while waiting for input. The scanner does not
1501      * advance past any input.
1502      *
1503      * @return true if and only if this scanner has another line of input
1504      * @throws IllegalStateException if this scanner is closed
1505      */
1506     public boolean hasNextLine() {
1507         saveState();
1508 
1509         modCount++;
1510         String result = findWithinHorizon(linePattern(), 0);
1511         if (result != null) {
1512             MatchResult mr = this.match();
1513             String lineSep = mr.group(1);
1514             if (lineSep != null) {
1515                 result = result.substring(0, result.length() -
1516                                           lineSep.length());
1517                 cacheResult(result);
1518 
1519             } else {
1520                 cacheResult();
1521             }
1522         }
1523         revertState();
1524         return (result != null);
1525     }
1526 
1527     /**
1528      * Advances this scanner past the current line and returns the input
1529      * that was skipped.
1530      *
1531      * This method returns the rest of the current line, excluding any line
1532      * separator at the end. The position is set to the beginning of the next
1533      * line.
1534      *
1535      * <p>Since this method continues to search through the input looking
1536      * for a line separator, it may buffer all of the input searching for
1537      * the line to skip if no line separators are present.
1538      *
1539      * @return the line that was skipped
1540      * @throws NoSuchElementException if no line was found
1541      * @throws IllegalStateException if this scanner is closed
1542      */
1543     public String nextLine() {
1544         modCount++;
1545         if (hasNextPattern == linePattern())
1546             return getCachedResult();
1547         clearCaches();
1548 
1549         String result = findWithinHorizon(linePattern, 0);
1550         if (result == null)
1551             throw new NoSuchElementException("No line found");
1552         MatchResult mr = this.match();
1553         String lineSep = mr.group(1);
1554         if (lineSep != null)
1555             result = result.substring(0, result.length() - lineSep.length());
1556         if (result == null)
1557             throw new NoSuchElementException();
1558         else
1559             return result;
1560     }
1561 
1562     // Public methods that ignore delimiters
1563 
1564     /**
1565      * Attempts to find the next occurrence of a pattern constructed from the
1566      * specified string, ignoring delimiters.
1567      *
1568      * <p>An invocation of this method of the form {@code findInLine(pattern)}
1569      * behaves in exactly the same way as the invocation
1570      * {@code findInLine(Pattern.compile(pattern))}.
1571      *
1572      * @param pattern a string specifying the pattern to search for
1573      * @return the text that matched the specified pattern
1574      * @throws IllegalStateException if this scanner is closed
1575      */
1576     public String findInLine(String pattern) {
1577         return findInLine(patternCache.forName(pattern));
1578     }
1579 
1580     /**
1581      * Attempts to find the next occurrence of the specified pattern ignoring
1582      * delimiters. If the pattern is found before the next line separator, the
1583      * scanner advances past the input that matched and returns the string that
1584      * matched the pattern.
1585      * If no such pattern is detected in the input up to the next line
1586      * separator, then {@code null} is returned and the scanner's
1587      * position is unchanged. This method may block waiting for input that
1588      * matches the pattern.
1589      *
1590      * <p>Since this method continues to search through the input looking
1591      * for the specified pattern, it may buffer all of the input searching for
1592      * the desired token if no line separators are present.
1593      *
1594      * @param pattern the pattern to scan for
1595      * @return the text that matched the specified pattern
1596      * @throws IllegalStateException if this scanner is closed
1597      */
1598     public String findInLine(Pattern pattern) {
1599         ensureOpen();
1600         if (pattern == null)
1601             throw new NullPointerException();
1602         clearCaches();
1603         modCount++;
1604         // Expand buffer to include the next newline or end of input
1605         int endPosition = 0;
1606         saveState();
1607         while (true) {
1608             if (findPatternInBuffer(separatorPattern(), 0)) {
1609                 endPosition = matcher.start();
1610                 break; // up to next newline
1611             }
1612             if (needInput) {
1613                 readInput();
1614             } else {
1615                 endPosition = buf.limit();
1616                 break; // up to end of input
1617             }
1618         }
1619         revertState();
1620         int horizonForLine = endPosition - position;
1621         // If there is nothing between the current pos and the next
1622         // newline simply return null, invoking findWithinHorizon
1623         // with "horizon=0" will scan beyond the line bound.
1624         if (horizonForLine == 0)
1625             return null;
1626         // Search for the pattern
1627         return findWithinHorizon(pattern, horizonForLine);
1628     }
1629 
1630     /**
1631      * Attempts to find the next occurrence of a pattern constructed from the
1632      * specified string, ignoring delimiters.
1633      *
1634      * <p>An invocation of this method of the form
1635      * {@code findWithinHorizon(pattern)} behaves in exactly the same way as
1636      * the invocation
1637      * {@code findWithinHorizon(Pattern.compile(pattern), horizon)}.
1638      *
1639      * @param pattern a string specifying the pattern to search for
1640      * @param horizon the search horizon
1641      * @return the text that matched the specified pattern
1642      * @throws IllegalStateException if this scanner is closed
1643      * @throws IllegalArgumentException if horizon is negative
1644      */
1645     public String findWithinHorizon(String pattern, int horizon) {
1646         return findWithinHorizon(patternCache.forName(pattern), horizon);
1647     }
1648 
1649     /**
1650      * Attempts to find the next occurrence of the specified pattern.
1651      *
1652      * <p>This method searches through the input up to the specified
1653      * search horizon, ignoring delimiters. If the pattern is found the
1654      * scanner advances past the input that matched and returns the string
1655      * that matched the pattern. If no such pattern is detected then the
1656      * null is returned and the scanner's position remains unchanged. This
1657      * method may block waiting for input that matches the pattern.
1658      *
1659      * <p>A scanner will never search more than {@code horizon} code
1660      * points beyond its current position. Note that a match may be clipped
1661      * by the horizon; that is, an arbitrary match result may have been
1662      * different if the horizon had been larger. The scanner treats the
1663      * horizon as a transparent, non-anchoring bound (see {@link
1664      * Matcher#useTransparentBounds} and {@link Matcher#useAnchoringBounds}).
1665      *
1666      * <p>If horizon is {@code 0}, then the horizon is ignored and
1667      * this method continues to search through the input looking for the
1668      * specified pattern without bound. In this case it may buffer all of
1669      * the input searching for the pattern.
1670      *
1671      * <p>If horizon is negative, then an IllegalArgumentException is
1672      * thrown.
1673      *
1674      * @param pattern the pattern to scan for
1675      * @param horizon the search horizon
1676      * @return the text that matched the specified pattern
1677      * @throws IllegalStateException if this scanner is closed
1678      * @throws IllegalArgumentException if horizon is negative
1679      */
1680     public String findWithinHorizon(Pattern pattern, int horizon) {
1681         ensureOpen();
1682         if (pattern == null)
1683             throw new NullPointerException();
1684         if (horizon < 0)
1685             throw new IllegalArgumentException("horizon < 0");
1686         clearCaches();
1687         modCount++;
1688 
1689         // Search for the pattern
1690         while (true) {
1691             if (findPatternInBuffer(pattern, horizon)) {
1692                 matchValid = true;
1693                 return matcher.group();
1694             }
1695             if (needInput)
1696                 readInput();
1697             else
1698                 break; // up to end of input
1699         }
1700         return null;
1701     }
1702 
1703     /**
1704      * Skips input that matches the specified pattern, ignoring delimiters.
1705      * This method will skip input if an anchored match of the specified
1706      * pattern succeeds.
1707      *
1708      * <p>If a match to the specified pattern is not found at the
1709      * current position, then no input is skipped and a
1710      * {@code NoSuchElementException} is thrown.
1711      *
1712      * <p>Since this method seeks to match the specified pattern starting at
1713      * the scanner's current position, patterns that can match a lot of
1714      * input (".*", for example) may cause the scanner to buffer a large
1715      * amount of input.
1716      *
1717      * <p>Note that it is possible to skip something without risking a
1718      * {@code NoSuchElementException} by using a pattern that can
1719      * match nothing, e.g., {@code sc.skip("[ \t]*")}.
1720      *
1721      * @param pattern a string specifying the pattern to skip over
1722      * @return this scanner
1723      * @throws NoSuchElementException if the specified pattern is not found
1724      * @throws IllegalStateException if this scanner is closed
1725      */
1726     public Scanner skip(Pattern pattern) {
1727         ensureOpen();
1728         if (pattern == null)
1729             throw new NullPointerException();
1730         clearCaches();
1731         modCount++;
1732 
1733         // Search for the pattern
1734         while (true) {
1735             if (matchPatternInBuffer(pattern)) {
1736                 matchValid = true;
1737                 position = matcher.end();
1738                 return this;
1739             }
1740             if (needInput)
1741                 readInput();
1742             else
1743                 throw new NoSuchElementException();
1744         }
1745     }
1746 
1747     /**
1748      * Skips input that matches a pattern constructed from the specified
1749      * string.
1750      *
1751      * <p> An invocation of this method of the form {@code skip(pattern)}
1752      * behaves in exactly the same way as the invocation
1753      * {@code skip(Pattern.compile(pattern))}.
1754      *
1755      * @param pattern a string specifying the pattern to skip over
1756      * @return this scanner
1757      * @throws IllegalStateException if this scanner is closed
1758      */
1759     public Scanner skip(String pattern) {
1760         return skip(patternCache.forName(pattern));
1761     }
1762 
1763     // Convenience methods for scanning primitives
1764 
1765     /**
1766      * Returns true if the next token in this scanner's input can be
1767      * interpreted as a boolean value using a case insensitive pattern
1768      * created from the string "true|false".  The scanner does not
1769      * advance past the input that matched.
1770      *
1771      * @return true if and only if this scanner's next token is a valid
1772      *         boolean value
1773      * @throws IllegalStateException if this scanner is closed
1774      */
1775     public boolean hasNextBoolean()  {
1776         return hasNext(boolPattern());
1777     }
1778 
1779     /**
1780      * Scans the next token of the input into a boolean value and returns
1781      * that value. This method will throw {@code InputMismatchException}
1782      * if the next token cannot be translated into a valid boolean value.
1783      * If the match is successful, the scanner advances past the input that
1784      * matched.
1785      *
1786      * @return the boolean scanned from the input
1787      * @throws InputMismatchException if the next token is not a valid boolean
1788      * @throws NoSuchElementException if input is exhausted
1789      * @throws IllegalStateException if this scanner is closed
1790      */
1791     public boolean nextBoolean()  {
1792         clearCaches();
1793         return Boolean.parseBoolean(next(boolPattern()));
1794     }
1795 
1796     /**
1797      * Returns true if the next token in this scanner's input can be
1798      * interpreted as a byte value in the default radix using the
1799      * {@link #nextByte} method. The scanner does not advance past any input.
1800      *
1801      * @return true if and only if this scanner's next token is a valid
1802      *         byte value
1803      * @throws IllegalStateException if this scanner is closed
1804      */
1805     public boolean hasNextByte() {
1806         return hasNextByte(defaultRadix);
1807     }
1808 
1809     /**
1810      * Returns true if the next token in this scanner's input can be
1811      * interpreted as a byte value in the specified radix using the
1812      * {@link #nextByte} method. The scanner does not advance past any input.
1813      *
1814      * @param radix the radix used to interpret the token as a byte value
1815      * @return true if and only if this scanner's next token is a valid
1816      *         byte value
1817      * @throws IllegalStateException if this scanner is closed
1818      */
1819     public boolean hasNextByte(int radix) {
1820         setRadix(radix);
1821         boolean result = hasNext(integerPattern());
1822         if (result) { // Cache it
1823             try {
1824                 String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
1825                     processIntegerToken(hasNextResult) :
1826                     hasNextResult;
1827                 typeCache = Byte.parseByte(s, radix);
1828             } catch (NumberFormatException nfe) {
1829                 result = false;
1830             }
1831         }
1832         return result;
1833     }
1834 
1835     /**
1836      * Scans the next token of the input as a {@code byte}.
1837      *
1838      * <p> An invocation of this method of the form
1839      * {@code nextByte()} behaves in exactly the same way as the
1840      * invocation {@code nextByte(radix)}, where {@code radix}
1841      * is the default radix of this scanner.
1842      *
1843      * @return the {@code byte} scanned from the input
1844      * @throws InputMismatchException
1845      *         if the next token does not match the <i>Integer</i>
1846      *         regular expression, or is out of range
1847      * @throws NoSuchElementException if input is exhausted
1848      * @throws IllegalStateException if this scanner is closed
1849      */
1850     public byte nextByte() {
1851          return nextByte(defaultRadix);
1852     }
1853 
1854     /**
1855      * Scans the next token of the input as a {@code byte}.
1856      * This method will throw {@code InputMismatchException}
1857      * if the next token cannot be translated into a valid byte value as
1858      * described below. If the translation is successful, the scanner advances
1859      * past the input that matched.
1860      *
1861      * <p> If the next token matches the <a
1862      * href="#Integer-regex"><i>Integer</i></a> regular expression defined
1863      * above then the token is converted into a {@code byte} value as if by
1864      * removing all locale specific prefixes, group separators, and locale
1865      * specific suffixes, then mapping non-ASCII digits into ASCII
1866      * digits via {@link Character#digit Character.digit}, prepending a
1867      * negative sign (-) if the locale specific negative prefixes and suffixes
1868      * were present, and passing the resulting string to
1869      * {@link Byte#parseByte(String, int) Byte.parseByte} with the
1870      * specified radix.
1871      *
1872      * @param radix the radix used to interpret the token as a byte value
1873      * @return the {@code byte} scanned from the input
1874      * @throws InputMismatchException
1875      *         if the next token does not match the <i>Integer</i>
1876      *         regular expression, or is out of range
1877      * @throws NoSuchElementException if input is exhausted
1878      * @throws IllegalStateException if this scanner is closed
1879      */
1880     public byte nextByte(int radix) {
1881         // Check cached result
1882         if ((typeCache != null) && (typeCache instanceof Byte)
1883             && this.radix == radix) {
1884             byte val = ((Byte)typeCache).byteValue();
1885             useTypeCache();
1886             return val;
1887         }
1888         setRadix(radix);
1889         clearCaches();
1890         // Search for next byte
1891         try {
1892             String s = next(integerPattern());
1893             if (matcher.group(SIMPLE_GROUP_INDEX) == null)
1894                 s = processIntegerToken(s);
1895             return Byte.parseByte(s, radix);
1896         } catch (NumberFormatException nfe) {
1897             position = matcher.start(); // don't skip bad token
1898             throw new InputMismatchException(nfe.getMessage());
1899         }
1900     }
1901 
1902     /**
1903      * Returns true if the next token in this scanner's input can be
1904      * interpreted as a short value in the default radix using the
1905      * {@link #nextShort} method. The scanner does not advance past any input.
1906      *
1907      * @return true if and only if this scanner's next token is a valid
1908      *         short value in the default radix
1909      * @throws IllegalStateException if this scanner is closed
1910      */
1911     public boolean hasNextShort() {
1912         return hasNextShort(defaultRadix);
1913     }
1914 
1915     /**
1916      * Returns true if the next token in this scanner's input can be
1917      * interpreted as a short value in the specified radix using the
1918      * {@link #nextShort} method. The scanner does not advance past any input.
1919      *
1920      * @param radix the radix used to interpret the token as a short value
1921      * @return true if and only if this scanner's next token is a valid
1922      *         short value in the specified radix
1923      * @throws IllegalStateException if this scanner is closed
1924      */
1925     public boolean hasNextShort(int radix) {
1926         setRadix(radix);
1927         boolean result = hasNext(integerPattern());
1928         if (result) { // Cache it
1929             try {
1930                 String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
1931                     processIntegerToken(hasNextResult) :
1932                     hasNextResult;
1933                 typeCache = Short.parseShort(s, radix);
1934             } catch (NumberFormatException nfe) {
1935                 result = false;
1936             }
1937         }
1938         return result;
1939     }
1940 
1941     /**
1942      * Scans the next token of the input as a {@code short}.
1943      *
1944      * <p> An invocation of this method of the form
1945      * {@code nextShort()} behaves in exactly the same way as the
1946      * invocation {@link #nextShort(int) nextShort(radix)}, where {@code radix}
1947      * is the default radix of this scanner.
1948      *
1949      * @return the {@code short} scanned from the input
1950      * @throws InputMismatchException
1951      *         if the next token does not match the <i>Integer</i>
1952      *         regular expression, or is out of range
1953      * @throws NoSuchElementException if input is exhausted
1954      * @throws IllegalStateException if this scanner is closed
1955      */
1956     public short nextShort() {
1957         return nextShort(defaultRadix);
1958     }
1959 
1960     /**
1961      * Scans the next token of the input as a {@code short}.
1962      * This method will throw {@code InputMismatchException}
1963      * if the next token cannot be translated into a valid short value as
1964      * described below. If the translation is successful, the scanner advances
1965      * past the input that matched.
1966      *
1967      * <p> If the next token matches the <a
1968      * href="#Integer-regex"><i>Integer</i></a> regular expression defined
1969      * above then the token is converted into a {@code short} value as if by
1970      * removing all locale specific prefixes, group separators, and locale
1971      * specific suffixes, then mapping non-ASCII digits into ASCII
1972      * digits via {@link Character#digit Character.digit}, prepending a
1973      * negative sign (-) if the locale specific negative prefixes and suffixes
1974      * were present, and passing the resulting string to
1975      * {@link Short#parseShort(String, int) Short.parseShort} with the
1976      * specified radix.
1977      *
1978      * @param radix the radix used to interpret the token as a short value
1979      * @return the {@code short} scanned from the input
1980      * @throws InputMismatchException
1981      *         if the next token does not match the <i>Integer</i>
1982      *         regular expression, or is out of range
1983      * @throws NoSuchElementException if input is exhausted
1984      * @throws IllegalStateException if this scanner is closed
1985      */
1986     public short nextShort(int radix) {
1987         // Check cached result
1988         if ((typeCache != null) && (typeCache instanceof Short)
1989             && this.radix == radix) {
1990             short val = ((Short)typeCache).shortValue();
1991             useTypeCache();
1992             return val;
1993         }
1994         setRadix(radix);
1995         clearCaches();
1996         // Search for next short
1997         try {
1998             String s = next(integerPattern());
1999             if (matcher.group(SIMPLE_GROUP_INDEX) == null)
2000                 s = processIntegerToken(s);
2001             return Short.parseShort(s, radix);
2002         } catch (NumberFormatException nfe) {
2003             position = matcher.start(); // don't skip bad token
2004             throw new InputMismatchException(nfe.getMessage());
2005         }
2006     }
2007 
2008     /**
2009      * Returns true if the next token in this scanner's input can be
2010      * interpreted as an int value in the default radix using the
2011      * {@link #nextInt} method. The scanner does not advance past any input.
2012      *
2013      * @return true if and only if this scanner's next token is a valid
2014      *         int value
2015      * @throws IllegalStateException if this scanner is closed
2016      */
2017     public boolean hasNextInt() {
2018         return hasNextInt(defaultRadix);
2019     }
2020 
2021     /**
2022      * Returns true if the next token in this scanner's input can be
2023      * interpreted as an int value in the specified radix using the
2024      * {@link #nextInt} method. The scanner does not advance past any input.
2025      *
2026      * @param radix the radix used to interpret the token as an int value
2027      * @return true if and only if this scanner's next token is a valid
2028      *         int value
2029      * @throws IllegalStateException if this scanner is closed
2030      */
2031     public boolean hasNextInt(int radix) {
2032         setRadix(radix);
2033         boolean result = hasNext(integerPattern());
2034         if (result) { // Cache it
2035             try {
2036                 String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
2037                     processIntegerToken(hasNextResult) :
2038                     hasNextResult;
2039                 typeCache = Integer.parseInt(s, radix);
2040             } catch (NumberFormatException nfe) {
2041                 result = false;
2042             }
2043         }
2044         return result;
2045     }
2046 
2047     /**
2048      * The integer token must be stripped of prefixes, group separators,
2049      * and suffixes, non ascii digits must be converted into ascii digits
2050      * before parse will accept it.
2051      */
2052     private String processIntegerToken(String token) {
2053         String result = token.replaceAll(""+groupSeparator, "");
2054         boolean isNegative = false;
2055         int preLen = negativePrefix.length();
2056         if ((preLen > 0) && result.startsWith(negativePrefix)) {
2057             isNegative = true;
2058             result = result.substring(preLen);
2059         }
2060         int sufLen = negativeSuffix.length();
2061         if ((sufLen > 0) && result.endsWith(negativeSuffix)) {
2062             isNegative = true;
2063             result = result.substring(result.length() - sufLen,
2064                                       result.length());
2065         }
2066         if (isNegative)
2067             result = "-" + result;
2068         return result;
2069     }
2070 
2071     /**
2072      * Scans the next token of the input as an {@code int}.
2073      *
2074      * <p> An invocation of this method of the form
2075      * {@code nextInt()} behaves in exactly the same way as the
2076      * invocation {@code nextInt(radix)}, where {@code radix}
2077      * is the default radix of this scanner.
2078      *
2079      * @return the {@code int} scanned from the input
2080      * @throws InputMismatchException
2081      *         if the next token does not match the <i>Integer</i>
2082      *         regular expression, or is out of range
2083      * @throws NoSuchElementException if input is exhausted
2084      * @throws IllegalStateException if this scanner is closed
2085      */
2086     public int nextInt() {
2087         return nextInt(defaultRadix);
2088     }
2089 
2090     /**
2091      * Scans the next token of the input as an {@code int}.
2092      * This method will throw {@code InputMismatchException}
2093      * if the next token cannot be translated into a valid int value as
2094      * described below. If the translation is successful, the scanner advances
2095      * past the input that matched.
2096      *
2097      * <p> If the next token matches the <a
2098      * href="#Integer-regex"><i>Integer</i></a> regular expression defined
2099      * above then the token is converted into an {@code int} value as if by
2100      * removing all locale specific prefixes, group separators, and locale
2101      * specific suffixes, then mapping non-ASCII digits into ASCII
2102      * digits via {@link Character#digit Character.digit}, prepending a
2103      * negative sign (-) if the locale specific negative prefixes and suffixes
2104      * were present, and passing the resulting string to
2105      * {@link Integer#parseInt(String, int) Integer.parseInt} with the
2106      * specified radix.
2107      *
2108      * @param radix the radix used to interpret the token as an int value
2109      * @return the {@code int} scanned from the input
2110      * @throws InputMismatchException
2111      *         if the next token does not match the <i>Integer</i>
2112      *         regular expression, or is out of range
2113      * @throws NoSuchElementException if input is exhausted
2114      * @throws IllegalStateException if this scanner is closed
2115      */
2116     public int nextInt(int radix) {
2117         // Check cached result
2118         if ((typeCache != null) && (typeCache instanceof Integer)
2119             && this.radix == radix) {
2120             int val = ((Integer)typeCache).intValue();
2121             useTypeCache();
2122             return val;
2123         }
2124         setRadix(radix);
2125         clearCaches();
2126         // Search for next int
2127         try {
2128             String s = next(integerPattern());
2129             if (matcher.group(SIMPLE_GROUP_INDEX) == null)
2130                 s = processIntegerToken(s);
2131             return Integer.parseInt(s, radix);
2132         } catch (NumberFormatException nfe) {
2133             position = matcher.start(); // don't skip bad token
2134             throw new InputMismatchException(nfe.getMessage());
2135         }
2136     }
2137 
2138     /**
2139      * Returns true if the next token in this scanner's input can be
2140      * interpreted as a long value in the default radix using the
2141      * {@link #nextLong} method. The scanner does not advance past any input.
2142      *
2143      * @return true if and only if this scanner's next token is a valid
2144      *         long value
2145      * @throws IllegalStateException if this scanner is closed
2146      */
2147     public boolean hasNextLong() {
2148         return hasNextLong(defaultRadix);
2149     }
2150 
2151     /**
2152      * Returns true if the next token in this scanner's input can be
2153      * interpreted as a long value in the specified radix using the
2154      * {@link #nextLong} method. The scanner does not advance past any input.
2155      *
2156      * @param radix the radix used to interpret the token as a long value
2157      * @return true if and only if this scanner's next token is a valid
2158      *         long value
2159      * @throws IllegalStateException if this scanner is closed
2160      */
2161     public boolean hasNextLong(int radix) {
2162         setRadix(radix);
2163         boolean result = hasNext(integerPattern());
2164         if (result) { // Cache it
2165             try {
2166                 String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
2167                     processIntegerToken(hasNextResult) :
2168                     hasNextResult;
2169                 typeCache = Long.parseLong(s, radix);
2170             } catch (NumberFormatException nfe) {
2171                 result = false;
2172             }
2173         }
2174         return result;
2175     }
2176 
2177     /**
2178      * Scans the next token of the input as a {@code long}.
2179      *
2180      * <p> An invocation of this method of the form
2181      * {@code nextLong()} behaves in exactly the same way as the
2182      * invocation {@code nextLong(radix)}, where {@code radix}
2183      * is the default radix of this scanner.
2184      *
2185      * @return the {@code long} scanned from the input
2186      * @throws InputMismatchException
2187      *         if the next token does not match the <i>Integer</i>
2188      *         regular expression, or is out of range
2189      * @throws NoSuchElementException if input is exhausted
2190      * @throws IllegalStateException if this scanner is closed
2191      */
2192     public long nextLong() {
2193         return nextLong(defaultRadix);
2194     }
2195 
2196     /**
2197      * Scans the next token of the input as a {@code long}.
2198      * This method will throw {@code InputMismatchException}
2199      * if the next token cannot be translated into a valid long value as
2200      * described below. If the translation is successful, the scanner advances
2201      * past the input that matched.
2202      *
2203      * <p> If the next token matches the <a
2204      * href="#Integer-regex"><i>Integer</i></a> regular expression defined
2205      * above then the token is converted into a {@code long} value as if by
2206      * removing all locale specific prefixes, group separators, and locale
2207      * specific suffixes, then mapping non-ASCII digits into ASCII
2208      * digits via {@link Character#digit Character.digit}, prepending a
2209      * negative sign (-) if the locale specific negative prefixes and suffixes
2210      * were present, and passing the resulting string to
2211      * {@link Long#parseLong(String, int) Long.parseLong} with the
2212      * specified radix.
2213      *
2214      * @param radix the radix used to interpret the token as an int value
2215      * @return the {@code long} scanned from the input
2216      * @throws InputMismatchException
2217      *         if the next token does not match the <i>Integer</i>
2218      *         regular expression, or is out of range
2219      * @throws NoSuchElementException if input is exhausted
2220      * @throws IllegalStateException if this scanner is closed
2221      */
2222     public long nextLong(int radix) {
2223         // Check cached result
2224         if ((typeCache != null) && (typeCache instanceof Long)
2225             && this.radix == radix) {
2226             long val = ((Long)typeCache).longValue();
2227             useTypeCache();
2228             return val;
2229         }
2230         setRadix(radix);
2231         clearCaches();
2232         try {
2233             String s = next(integerPattern());
2234             if (matcher.group(SIMPLE_GROUP_INDEX) == null)
2235                 s = processIntegerToken(s);
2236             return Long.parseLong(s, radix);
2237         } catch (NumberFormatException nfe) {
2238             position = matcher.start(); // don't skip bad token
2239             throw new InputMismatchException(nfe.getMessage());
2240         }
2241     }
2242 
2243     /**
2244      * The float token must be stripped of prefixes, group separators,
2245      * and suffixes, non ascii digits must be converted into ascii digits
2246      * before parseFloat will accept it.
2247      *
2248      * If there are non-ascii digits in the token these digits must
2249      * be processed before the token is passed to parseFloat.
2250      */
2251     private String processFloatToken(String token) {
2252         String result = token.replaceAll(groupSeparator, "");
2253         if (!decimalSeparator.equals("\\."))
2254             result = result.replaceAll(decimalSeparator, ".");
2255         boolean isNegative = false;
2256         int preLen = negativePrefix.length();
2257         if ((preLen > 0) && result.startsWith(negativePrefix)) {
2258             isNegative = true;
2259             result = result.substring(preLen);
2260         }
2261         int sufLen = negativeSuffix.length();
2262         if ((sufLen > 0) && result.endsWith(negativeSuffix)) {
2263             isNegative = true;
2264             result = result.substring(result.length() - sufLen,
2265                                       result.length());
2266         }
2267         if (result.equals(nanString))
2268             result = "NaN";
2269         if (result.equals(infinityString))
2270             result = "Infinity";
2271         if (isNegative)
2272             result = "-" + result;
2273 
2274         // Translate non-ASCII digits
2275         Matcher m = NON_ASCII_DIGIT.matcher(result);
2276         if (m.find()) {
2277             StringBuilder inASCII = new StringBuilder();
2278             for (int i=0; i<result.length(); i++) {
2279                 char nextChar = result.charAt(i);
2280                 if (Character.isDigit(nextChar)) {
2281                     int d = Character.digit(nextChar, 10);
2282                     if (d != -1)
2283                         inASCII.append(d);
2284                     else
2285                         inASCII.append(nextChar);
2286                 } else {
2287                     inASCII.append(nextChar);
2288                 }
2289             }
2290             result = inASCII.toString();
2291         }
2292 
2293         return result;
2294     }
2295 
2296     /**
2297      * Returns true if the next token in this scanner's input can be
2298      * interpreted as a float value using the {@link #nextFloat}
2299      * method. The scanner does not advance past any input.
2300      *
2301      * @return true if and only if this scanner's next token is a valid
2302      *         float value
2303      * @throws IllegalStateException if this scanner is closed
2304      */
2305     public boolean hasNextFloat() {
2306         setRadix(10);
2307         boolean result = hasNext(floatPattern());
2308         if (result) { // Cache it
2309             try {
2310                 String s = processFloatToken(hasNextResult);
2311                 typeCache = Float.valueOf(Float.parseFloat(s));
2312             } catch (NumberFormatException nfe) {
2313                 result = false;
2314             }
2315         }
2316         return result;
2317     }
2318 
2319     /**
2320      * Scans the next token of the input as a {@code float}.
2321      * This method will throw {@code InputMismatchException}
2322      * if the next token cannot be translated into a valid float value as
2323      * described below. If the translation is successful, the scanner advances
2324      * past the input that matched.
2325      *
2326      * <p> If the next token matches the <a
2327      * href="#Float-regex"><i>Float</i></a> regular expression defined above
2328      * then the token is converted into a {@code float} value as if by
2329      * removing all locale specific prefixes, group separators, and locale
2330      * specific suffixes, then mapping non-ASCII digits into ASCII
2331      * digits via {@link Character#digit Character.digit}, prepending a
2332      * negative sign (-) if the locale specific negative prefixes and suffixes
2333      * were present, and passing the resulting string to
2334      * {@link Float#parseFloat Float.parseFloat}. If the token matches
2335      * the localized NaN or infinity strings, then either "Nan" or "Infinity"
2336      * is passed to {@link Float#parseFloat(String) Float.parseFloat} as
2337      * appropriate.
2338      *
2339      * @return the {@code float} scanned from the input
2340      * @throws InputMismatchException
2341      *         if the next token does not match the <i>Float</i>
2342      *         regular expression, or is out of range
2343      * @throws NoSuchElementException if input is exhausted
2344      * @throws IllegalStateException if this scanner is closed
2345      */
2346     public float nextFloat() {
2347         // Check cached result
2348         if ((typeCache != null) && (typeCache instanceof Float)) {
2349             float val = ((Float)typeCache).floatValue();
2350             useTypeCache();
2351             return val;
2352         }
2353         setRadix(10);
2354         clearCaches();
2355         try {
2356             return Float.parseFloat(processFloatToken(next(floatPattern())));
2357         } catch (NumberFormatException nfe) {
2358             position = matcher.start(); // don't skip bad token
2359             throw new InputMismatchException(nfe.getMessage());
2360         }
2361     }
2362 
2363     /**
2364      * Returns true if the next token in this scanner's input can be
2365      * interpreted as a double value using the {@link #nextDouble}
2366      * method. The scanner does not advance past any input.
2367      *
2368      * @return true if and only if this scanner's next token is a valid
2369      *         double value
2370      * @throws IllegalStateException if this scanner is closed
2371      */
2372     public boolean hasNextDouble() {
2373         setRadix(10);
2374         boolean result = hasNext(floatPattern());
2375         if (result) { // Cache it
2376             try {
2377                 String s = processFloatToken(hasNextResult);
2378                 typeCache = Double.valueOf(Double.parseDouble(s));
2379             } catch (NumberFormatException nfe) {
2380                 result = false;
2381             }
2382         }
2383         return result;
2384     }
2385 
2386     /**
2387      * Scans the next token of the input as a {@code double}.
2388      * This method will throw {@code InputMismatchException}
2389      * if the next token cannot be translated into a valid double value.
2390      * If the translation is successful, the scanner advances past the input
2391      * that matched.
2392      *
2393      * <p> If the next token matches the <a
2394      * href="#Float-regex"><i>Float</i></a> regular expression defined above
2395      * then the token is converted into a {@code double} value as if by
2396      * removing all locale specific prefixes, group separators, and locale
2397      * specific suffixes, then mapping non-ASCII digits into ASCII
2398      * digits via {@link Character#digit Character.digit}, prepending a
2399      * negative sign (-) if the locale specific negative prefixes and suffixes
2400      * were present, and passing the resulting string to
2401      * {@link Double#parseDouble Double.parseDouble}. If the token matches
2402      * the localized NaN or infinity strings, then either "Nan" or "Infinity"
2403      * is passed to {@link Double#parseDouble(String) Double.parseDouble} as
2404      * appropriate.
2405      *
2406      * @return the {@code double} scanned from the input
2407      * @throws InputMismatchException
2408      *         if the next token does not match the <i>Float</i>
2409      *         regular expression, or is out of range
2410      * @throws NoSuchElementException if the input is exhausted
2411      * @throws IllegalStateException if this scanner is closed
2412      */
2413     public double nextDouble() {
2414         // Check cached result
2415         if ((typeCache != null) && (typeCache instanceof Double)) {
2416             double val = ((Double)typeCache).doubleValue();
2417             useTypeCache();
2418             return val;
2419         }
2420         setRadix(10);
2421         clearCaches();
2422         // Search for next float
2423         try {
2424             return Double.parseDouble(processFloatToken(next(floatPattern())));
2425         } catch (NumberFormatException nfe) {
2426             position = matcher.start(); // don't skip bad token
2427             throw new InputMismatchException(nfe.getMessage());
2428         }
2429     }
2430 
2431     // Convenience methods for scanning multi precision numbers
2432 
2433     /**
2434      * Returns true if the next token in this scanner's input can be
2435      * interpreted as a {@code BigInteger} in the default radix using the
2436      * {@link #nextBigInteger} method. The scanner does not advance past any
2437      * input.
2438      *
2439      * @return true if and only if this scanner's next token is a valid
2440      *         {@code BigInteger}
2441      * @throws IllegalStateException if this scanner is closed
2442      */
2443     public boolean hasNextBigInteger() {
2444         return hasNextBigInteger(defaultRadix);
2445     }
2446 
2447     /**
2448      * Returns true if the next token in this scanner's input can be
2449      * interpreted as a {@code BigInteger} in the specified radix using
2450      * the {@link #nextBigInteger} method. The scanner does not advance past
2451      * any input.
2452      *
2453      * @param radix the radix used to interpret the token as an integer
2454      * @return true if and only if this scanner's next token is a valid
2455      *         {@code BigInteger}
2456      * @throws IllegalStateException if this scanner is closed
2457      */
2458     public boolean hasNextBigInteger(int radix) {
2459         setRadix(radix);
2460         boolean result = hasNext(integerPattern());
2461         if (result) { // Cache it
2462             try {
2463                 String s = (matcher.group(SIMPLE_GROUP_INDEX) == null) ?
2464                     processIntegerToken(hasNextResult) :
2465                     hasNextResult;
2466                 typeCache = new BigInteger(s, radix);
2467             } catch (NumberFormatException nfe) {
2468                 result = false;
2469             }
2470         }
2471         return result;
2472     }
2473 
2474     /**
2475      * Scans the next token of the input as a {@link java.math.BigInteger
2476      * BigInteger}.
2477      *
2478      * <p> An invocation of this method of the form
2479      * {@code nextBigInteger()} behaves in exactly the same way as the
2480      * invocation {@code nextBigInteger(radix)}, where {@code radix}
2481      * is the default radix of this scanner.
2482      *
2483      * @return the {@code BigInteger} scanned from the input
2484      * @throws InputMismatchException
2485      *         if the next token does not match the <i>Integer</i>
2486      *         regular expression, or is out of range
2487      * @throws NoSuchElementException if the input is exhausted
2488      * @throws IllegalStateException if this scanner is closed
2489      */
2490     public BigInteger nextBigInteger() {
2491         return nextBigInteger(defaultRadix);
2492     }
2493 
2494     /**
2495      * Scans the next token of the input as a {@link java.math.BigInteger
2496      * BigInteger}.
2497      *
2498      * <p> If the next token matches the <a
2499      * href="#Integer-regex"><i>Integer</i></a> regular expression defined
2500      * above then the token is converted into a {@code BigInteger} value as if
2501      * by removing all group separators, mapping non-ASCII digits into ASCII
2502      * digits via the {@link Character#digit Character.digit}, and passing the
2503      * resulting string to the {@link
2504      * java.math.BigInteger#BigInteger(java.lang.String)
2505      * BigInteger(String, int)} constructor with the specified radix.
2506      *
2507      * @param radix the radix used to interpret the token
2508      * @return the {@code BigInteger} scanned from the input
2509      * @throws InputMismatchException
2510      *         if the next token does not match the <i>Integer</i>
2511      *         regular expression, or is out of range
2512      * @throws NoSuchElementException if the input is exhausted
2513      * @throws IllegalStateException if this scanner is closed
2514      */
2515     public BigInteger nextBigInteger(int radix) {
2516         // Check cached result
2517         if ((typeCache != null) && (typeCache instanceof BigInteger)
2518             && this.radix == radix) {
2519             BigInteger val = (BigInteger)typeCache;
2520             useTypeCache();
2521             return val;
2522         }
2523         setRadix(radix);
2524         clearCaches();
2525         // Search for next int
2526         try {
2527             String s = next(integerPattern());
2528             if (matcher.group(SIMPLE_GROUP_INDEX) == null)
2529                 s = processIntegerToken(s);
2530             return new BigInteger(s, radix);
2531         } catch (NumberFormatException nfe) {
2532             position = matcher.start(); // don't skip bad token
2533             throw new InputMismatchException(nfe.getMessage());
2534         }
2535     }
2536 
2537     /**
2538      * Returns true if the next token in this scanner's input can be
2539      * interpreted as a {@code BigDecimal} using the
2540      * {@link #nextBigDecimal} method. The scanner does not advance past any
2541      * input.
2542      *
2543      * @return true if and only if this scanner's next token is a valid
2544      *         {@code BigDecimal}
2545      * @throws IllegalStateException if this scanner is closed
2546      */
2547     public boolean hasNextBigDecimal() {
2548         setRadix(10);
2549         boolean result = hasNext(decimalPattern());
2550         if (result) { // Cache it
2551             try {
2552                 String s = processFloatToken(hasNextResult);
2553                 typeCache = new BigDecimal(s);
2554             } catch (NumberFormatException nfe) {
2555                 result = false;
2556             }
2557         }
2558         return result;
2559     }
2560 
2561     /**
2562      * Scans the next token of the input as a {@link java.math.BigDecimal
2563      * BigDecimal}.
2564      *
2565      * <p> If the next token matches the <a
2566      * href="#Decimal-regex"><i>Decimal</i></a> regular expression defined
2567      * above then the token is converted into a {@code BigDecimal} value as if
2568      * by removing all group separators, mapping non-ASCII digits into ASCII
2569      * digits via the {@link Character#digit Character.digit}, and passing the
2570      * resulting string to the {@link
2571      * java.math.BigDecimal#BigDecimal(java.lang.String) BigDecimal(String)}
2572      * constructor.
2573      *
2574      * @return the {@code BigDecimal} scanned from the input
2575      * @throws InputMismatchException
2576      *         if the next token does not match the <i>Decimal</i>
2577      *         regular expression, or is out of range
2578      * @throws NoSuchElementException if the input is exhausted
2579      * @throws IllegalStateException if this scanner is closed
2580      */
2581     public BigDecimal nextBigDecimal() {
2582         // Check cached result
2583         if ((typeCache != null) && (typeCache instanceof BigDecimal)) {
2584             BigDecimal val = (BigDecimal)typeCache;
2585             useTypeCache();
2586             return val;
2587         }
2588         setRadix(10);
2589         clearCaches();
2590         // Search for next float
2591         try {
2592             String s = processFloatToken(next(decimalPattern()));
2593             return new BigDecimal(s);
2594         } catch (NumberFormatException nfe) {
2595             position = matcher.start(); // don't skip bad token
2596             throw new InputMismatchException(nfe.getMessage());
2597         }
2598     }
2599 
2600     /**
2601      * Resets this scanner.
2602      *
2603      * <p> Resetting a scanner discards all of its explicit state
2604      * information which may have been changed by invocations of
2605      * {@link #useDelimiter useDelimiter()},
2606      * {@link #useLocale useLocale()}, or
2607      * {@link #useRadix useRadix()}.
2608      *
2609      * <p> An invocation of this method of the form
2610      * {@code scanner.reset()} behaves in exactly the same way as the
2611      * invocation
2612      *
2613      * <blockquote><pre>{@code
2614      *   scanner.useDelimiter("\\p{javaWhitespace}+")
2615      *          .useLocale(Locale.getDefault(Locale.Category.FORMAT))
2616      *          .useRadix(10);
2617      * }</pre></blockquote>
2618      *
2619      * @return this scanner
2620      *
2621      * @since 1.6
2622      */
2623     public Scanner reset() {
2624         delimPattern = WHITESPACE_PATTERN;
2625         useLocale(Locale.getDefault(Locale.Category.FORMAT));
2626         useRadix(10);
2627         clearCaches();
2628         modCount++;
2629         return this;
2630     }
2631 
2632     /**
2633      * Returns a stream of delimiter-separated tokens from this scanner. The
2634      * stream contains the same tokens that would be returned, starting from
2635      * this scanner's current state, by calling the {@link #next} method
2636      * repeatedly until the {@link #hasNext} method returns false.
2637      *
2638      * <p>The resulting stream is sequential and ordered. All stream elements are
2639      * non-null.
2640      *
2641      * <p>Scanning starts upon initiation of the terminal stream operation, using the
2642      * current state of this scanner. Subsequent calls to any methods on this scanner
2643      * other than {@link #close} and {@link #ioException} may return undefined results
2644      * or may cause undefined effects on the returned stream. The returned stream's source
2645      * {@code Spliterator} is <em>fail-fast</em> and will, on a best-effort basis, throw a
2646      * {@link java.util.ConcurrentModificationException} if any such calls are detected
2647      * during stream pipeline execution.
2648      *
2649      * <p>After stream pipeline execution completes, this scanner is left in an indeterminate
2650      * state and cannot be reused.
2651      *
2652      * <p>If this scanner contains a resource that must be released, this scanner
2653      * should be closed, either by calling its {@link #close} method, or by
2654      * closing the returned stream. Closing the stream will close the underlying scanner.
2655      * {@code IllegalStateException} is thrown if the scanner has been closed when this
2656      * method is called, or if this scanner is closed during stream pipeline execution.
2657      *
2658      * <p>This method might block waiting for more input.
2659      *
2660      * @apiNote
2661      * For example, the following code will create a list of
2662      * comma-delimited tokens from a string:
2663      *
2664      * <pre>{@code
2665      * List<String> result = new Scanner("abc,def,,ghi")
2666      *     .useDelimiter(",")
2667      *     .tokens()
2668      *     .collect(Collectors.toList());
2669      * }</pre>
2670      *
2671      * <p>The resulting list would contain {@code "abc"}, {@code "def"},
2672      * the empty string, and {@code "ghi"}.
2673      *
2674      * @return a sequential stream of token strings
2675      * @throws IllegalStateException if this scanner is closed
2676      * @since 9
2677      */
2678     public Stream<String> tokens() {
2679         ensureOpen();
2680         Stream<String> stream = StreamSupport.stream(new TokenSpliterator(), false);
2681         return stream.onClose(this::close);
2682     }
2683 
2684     class TokenSpliterator extends Spliterators.AbstractSpliterator<String> {
2685         int expectedCount = -1;
2686 
2687         TokenSpliterator() {
2688             super(Long.MAX_VALUE,
2689                   Spliterator.IMMUTABLE | Spliterator.NONNULL | Spliterator.ORDERED);
2690         }
2691 
2692         @Override
2693         public boolean tryAdvance(Consumer<? super String> cons) {
2694             if (expectedCount >= 0 && expectedCount != modCount) {
2695                 throw new ConcurrentModificationException();
2696             }
2697 
2698             if (hasNext()) {
2699                 String token = next();
2700                 expectedCount = modCount;
2701                 cons.accept(token);
2702                 if (expectedCount != modCount) {
2703                     throw new ConcurrentModificationException();
2704                 }
2705                 return true;
2706             } else {
2707                 expectedCount = modCount;
2708                 return false;
2709             }
2710         }
2711     }
2712 
2713     /**
2714      * Returns a stream of match results from this scanner. The stream
2715      * contains the same results in the same order that would be returned by
2716      * calling {@code findWithinHorizon(pattern, 0)} and then {@link #match}
2717      * successively as long as {@link #findWithinHorizon findWithinHorizon()}
2718      * finds matches.
2719      *
2720      * <p>The resulting stream is sequential and ordered. All stream elements are
2721      * non-null.
2722      *
2723      * <p>Scanning starts upon initiation of the terminal stream operation, using the
2724      * current state of this scanner. Subsequent calls to any methods on this scanner
2725      * other than {@link #close} and {@link #ioException} may return undefined results
2726      * or may cause undefined effects on the returned stream. The returned stream's source
2727      * {@code Spliterator} is <em>fail-fast</em> and will, on a best-effort basis, throw a
2728      * {@link java.util.ConcurrentModificationException} if any such calls are detected
2729      * during stream pipeline execution.
2730      *
2731      * <p>After stream pipeline execution completes, this scanner is left in an indeterminate
2732      * state and cannot be reused.
2733      *
2734      * <p>If this scanner contains a resource that must be released, this scanner
2735      * should be closed, either by calling its {@link #close} method, or by
2736      * closing the returned stream. Closing the stream will close the underlying scanner.
2737      * {@code IllegalStateException} is thrown if the scanner has been closed when this
2738      * method is called, or if this scanner is closed during stream pipeline execution.
2739      *
2740      * <p>As with the {@link #findWithinHorizon findWithinHorizon()} methods, this method
2741      * might block waiting for additional input, and it might buffer an unbounded amount of
2742      * input searching for a match.
2743      *
2744      * @apiNote
2745      * For example, the following code will read a file and return a list
2746      * of all sequences of characters consisting of seven or more Latin capital
2747      * letters:
2748      *
2749      * <pre>{@code
2750      * try (Scanner sc = new Scanner(Paths.get("input.txt"))) {
2751      *     Pattern pat = Pattern.compile("[A-Z]{7,}");
2752      *     List<String> capWords = sc.findAll(pat)
2753      *                               .map(MatchResult::group)
2754      *                               .collect(Collectors.toList());
2755      * }
2756      * }</pre>
2757      *
2758      * @param pattern the pattern to be matched
2759      * @return a sequential stream of match results
2760      * @throws NullPointerException if pattern is null
2761      * @throws IllegalStateException if this scanner is closed
2762      * @since 9
2763      */
2764     public Stream<MatchResult> findAll(Pattern pattern) {
2765         Objects.requireNonNull(pattern);
2766         ensureOpen();
2767         Stream<MatchResult> stream = StreamSupport.stream(new FindSpliterator(pattern), false);
2768         return stream.onClose(this::close);
2769     }
2770 
2771     /**
2772      * Returns a stream of match results that match the provided pattern string.
2773      * The effect is equivalent to the following code:
2774      *
2775      * <pre>{@code
2776      *     scanner.findAll(Pattern.compile(patString))
2777      * }</pre>
2778      *
2779      * @param patString the pattern string
2780      * @return a sequential stream of match results
2781      * @throws NullPointerException if patString is null
2782      * @throws IllegalStateException if this scanner is closed
2783      * @throws PatternSyntaxException if the regular expression's syntax is invalid
2784      * @since 9
2785      * @see java.util.regex.Pattern
2786      */
2787     public Stream<MatchResult> findAll(String patString) {
2788         Objects.requireNonNull(patString);
2789         ensureOpen();
2790         return findAll(patternCache.forName(patString));
2791     }
2792 
2793     class FindSpliterator extends Spliterators.AbstractSpliterator<MatchResult> {
2794         final Pattern pattern;
2795         int expectedCount = -1;
2796 
2797         FindSpliterator(Pattern pattern) {
2798             super(Long.MAX_VALUE,
2799                   Spliterator.IMMUTABLE | Spliterator.NONNULL | Spliterator.ORDERED);
2800             this.pattern = pattern;
2801         }
2802 
2803         @Override
2804         public boolean tryAdvance(Consumer<? super MatchResult> cons) {
2805             ensureOpen();
2806             if (expectedCount >= 0) {
2807                 if (expectedCount != modCount) {
2808                     throw new ConcurrentModificationException();
2809                 }
2810             } else {
2811                 expectedCount = modCount;
2812             }
2813 
2814             while (true) {
2815                 // assert expectedCount == modCount
2816                 if (findPatternInBuffer(pattern, 0)) { // doesn't increment modCount
2817                     cons.accept(matcher.toMatchResult());
2818                     if (expectedCount != modCount) {
2819                         throw new ConcurrentModificationException();
2820                     }
2821                     return true;
2822                 }
2823                 if (needInput)
2824                     readInput(); // doesn't increment modCount
2825                 else
2826                     return false; // reached end of input
2827             }
2828         }
2829     }
2830 
2831     /** Small LRU cache of Patterns. */
2832     private static class PatternLRUCache {
2833 
2834         private Pattern[] oa = null;
2835         private final int size;
2836 
2837         PatternLRUCache(int size) {
2838             this.size = size;
2839         }
2840 
2841         boolean hasName(Pattern p, String s) {
2842             return p.pattern().equals(s);
2843         }
2844 
2845         void moveToFront(Object[] oa, int i) {
2846             Object ob = oa[i];
2847             for (int j = i; j > 0; j--)
2848                 oa[j] = oa[j - 1];
2849             oa[0] = ob;
2850         }
2851 
2852         Pattern forName(String name) {
2853             if (oa == null) {
2854                 Pattern[] temp = new Pattern[size];
2855                 oa = temp;
2856             } else {
2857                 for (int i = 0; i < oa.length; i++) {
2858                     Pattern ob = oa[i];
2859                     if (ob == null)
2860                         continue;
2861                     if (hasName(ob, name)) {
2862                         if (i > 0)
2863                             moveToFront(oa, i);
2864                         return ob;
2865                     }
2866                 }
2867             }
2868 
2869             // Create a new object
2870             Pattern ob = Pattern.compile(name);
2871             oa[oa.length - 1] = ob;
2872             moveToFront(oa, oa.length - 1);
2873             return ob;
2874         }
2875     }
2876 }