< prev index next >

src/java.base/share/classes/java/util/Scanner.java

Print this page
rev 53930 : 8148917: enhanced-for statement should allow streams
Reviewed-by: XXX


 290  *
 291  *   <dt><i>SignedNonNumber</i>:
 292  *       <dd>{@code ( [-+]? }<i>NonNumber</i>{@code  )}
 293  *       <dd>{@code | }<i>LocalPositivePrefix</i>
 294  *                 <i>NonNumber</i>
 295  *                 <i>LocalPositiveSuffix</i>
 296  *       <dd>{@code | }<i>LocalNegativePrefix</i>
 297  *                 <i>NonNumber</i>
 298  *                 <i>LocalNegativeSuffix</i>
 299  *
 300  *   <dt><a id="Float-regex"><i>Float</i></a>:
 301  *       <dd><i>Decimal</i>
 302  *           {@code | }<i>HexFloat</i>
 303  *           {@code | }<i>SignedNonNumber</i>
 304  *
 305  * </dl>
 306  * <p>Whitespace is not significant in the above regular expressions.
 307  *
 308  * @since   1.5
 309  */
 310 public final class Scanner implements Iterator<String>, Closeable {
 311 
 312     // Internal buffer used to hold input
 313     private CharBuffer buf;
 314 
 315     // Size of internal character buffer
 316     private static final int BUFFER_SIZE = 1024; // change to 1024;
 317 
 318     // The index into the buffer currently held by the Scanner
 319     private int position;
 320 
 321     // Internal matcher used for finding delimiters
 322     private Matcher matcher;
 323 
 324     // Pattern used to delimit tokens
 325     private Pattern delimPattern;
 326 
 327     // Pattern found in last hasNext operation
 328     private Pattern hasNextPattern;
 329 
 330     // Position after last hasNext operation


 355     private boolean matchValid = false;
 356 
 357     // Boolean indicating if this scanner has been closed
 358     private boolean closed = false;
 359 
 360     // The current radix used by this scanner
 361     private int radix = 10;
 362 
 363     // The default radix for this scanner
 364     private int defaultRadix = 10;
 365 
 366     // The locale used by this scanner
 367     private Locale locale = null;
 368 
 369     // A cache of the last few recently used Patterns
 370     private PatternLRUCache patternCache = new PatternLRUCache(7);
 371 
 372     // A holder of the last IOException encountered
 373     private IOException lastException;
 374 



 375     // Number of times this scanner's state has been modified.
 376     // Generally incremented on most public APIs and checked
 377     // within spliterator implementations.
 378     int modCount;
 379 
 380     // A pattern for java whitespace
 381     private static Pattern WHITESPACE_PATTERN = Pattern.compile(
 382                                                 "\\p{javaWhitespace}+");
 383 
 384     // A pattern for any token
 385     private static Pattern FIND_ANY_PATTERN = Pattern.compile("(?s).*");
 386 
 387     // A pattern for non-ASCII digits
 388     private static Pattern NON_ASCII_DIGIT = Pattern.compile(
 389         "[\\p{javaDigit}&&[^0-9]]");
 390 
 391     // Fields and methods to support scanning primitive types
 392 
 393     /**
 394      * Locale dependent values used to scan numbers


1463      */
1464     public String next() {
1465         ensureOpen();
1466         clearCaches();
1467         modCount++;
1468         while (true) {
1469             String token = getCompleteTokenInBuffer(null);
1470             if (token != null) {
1471                 matchValid = true;
1472                 skipped = false;
1473                 return token;
1474             }
1475             if (needInput)
1476                 readInput();
1477             else
1478                 throwFor();
1479         }
1480     }
1481 
1482     /**


















1483      * The remove operation is not supported by this implementation of
1484      * {@code Iterator}.
1485      *
1486      * @throws UnsupportedOperationException if this method is invoked.
1487      * @see java.util.Iterator
1488      */
1489     public void remove() {
1490         throw new UnsupportedOperationException();
1491     }
1492 
1493     /**
1494      * Returns true if the next token matches the pattern constructed from the
1495      * specified string. The scanner does not advance past any input.
1496      *
1497      * <p> An invocation of this method of the form {@code hasNext(pattern)}
1498      * behaves in exactly the same way as the invocation
1499      * {@code hasNext(Pattern.compile(pattern))}.
1500      *
1501      * @param pattern a string specifying the pattern to scan
1502      * @return true if and only if this scanner has another token matching




 290  *
 291  *   <dt><i>SignedNonNumber</i>:
 292  *       <dd>{@code ( [-+]? }<i>NonNumber</i>{@code  )}
 293  *       <dd>{@code | }<i>LocalPositivePrefix</i>
 294  *                 <i>NonNumber</i>
 295  *                 <i>LocalPositiveSuffix</i>
 296  *       <dd>{@code | }<i>LocalNegativePrefix</i>
 297  *                 <i>NonNumber</i>
 298  *                 <i>LocalNegativeSuffix</i>
 299  *
 300  *   <dt><a id="Float-regex"><i>Float</i></a>:
 301  *       <dd><i>Decimal</i>
 302  *           {@code | }<i>HexFloat</i>
 303  *           {@code | }<i>SignedNonNumber</i>
 304  *
 305  * </dl>
 306  * <p>Whitespace is not significant in the above regular expressions.
 307  *
 308  * @since   1.5
 309  */
 310 public final class Scanner implements Iterator<String>, Closeable, IterableOnce<String> {
 311 
 312     // Internal buffer used to hold input
 313     private CharBuffer buf;
 314 
 315     // Size of internal character buffer
 316     private static final int BUFFER_SIZE = 1024; // change to 1024;
 317 
 318     // The index into the buffer currently held by the Scanner
 319     private int position;
 320 
 321     // Internal matcher used for finding delimiters
 322     private Matcher matcher;
 323 
 324     // Pattern used to delimit tokens
 325     private Pattern delimPattern;
 326 
 327     // Pattern found in last hasNext operation
 328     private Pattern hasNextPattern;
 329 
 330     // Position after last hasNext operation


 355     private boolean matchValid = false;
 356 
 357     // Boolean indicating if this scanner has been closed
 358     private boolean closed = false;
 359 
 360     // The current radix used by this scanner
 361     private int radix = 10;
 362 
 363     // The default radix for this scanner
 364     private int defaultRadix = 10;
 365 
 366     // The locale used by this scanner
 367     private Locale locale = null;
 368 
 369     // A cache of the last few recently used Patterns
 370     private PatternLRUCache patternCache = new PatternLRUCache(7);
 371 
 372     // A holder of the last IOException encountered
 373     private IOException lastException;
 374 
 375     // Whether this Scanner, as IterableOnce, has been consumed
 376     private boolean consumed = false;
 377 
 378     // Number of times this scanner's state has been modified.
 379     // Generally incremented on most public APIs and checked
 380     // within spliterator implementations.
 381     int modCount;
 382 
 383     // A pattern for java whitespace
 384     private static Pattern WHITESPACE_PATTERN = Pattern.compile(
 385                                                 "\\p{javaWhitespace}+");
 386 
 387     // A pattern for any token
 388     private static Pattern FIND_ANY_PATTERN = Pattern.compile("(?s).*");
 389 
 390     // A pattern for non-ASCII digits
 391     private static Pattern NON_ASCII_DIGIT = Pattern.compile(
 392         "[\\p{javaDigit}&&[^0-9]]");
 393 
 394     // Fields and methods to support scanning primitive types
 395 
 396     /**
 397      * Locale dependent values used to scan numbers


1466      */
1467     public String next() {
1468         ensureOpen();
1469         clearCaches();
1470         modCount++;
1471         while (true) {
1472             String token = getCompleteTokenInBuffer(null);
1473             if (token != null) {
1474                 matchValid = true;
1475                 skipped = false;
1476                 return token;
1477             }
1478             if (needInput)
1479                 readInput();
1480             else
1481                 throwFor();
1482         }
1483     }
1484 
1485     /**
1486      * Returns this Scanner instance. This method exists to implement the
1487      * {@link IterableOnce} interface, allowing a Scanner instance to be
1488      * used in an enhanced-for ("for each") statement. This method can be
1489      * called no more than once on any Scanner instance. The second and
1490      * subsequent calls result in an exception.
1491      *
1492      * @throws IllegalStateException if this method had been called previously
1493      * @return this Scanner instance
1494      */
1495     public Scanner iterator() {
1496         if (consumed) {
1497             throw new IllegalStateException();
1498         }
1499         consumed = true;
1500         return this;
1501     }
1502 
1503     /**
1504      * The remove operation is not supported by this implementation of
1505      * {@code Iterator}.
1506      *
1507      * @throws UnsupportedOperationException if this method is invoked.
1508      * @see java.util.Iterator
1509      */
1510     public void remove() {
1511         throw new UnsupportedOperationException();
1512     }
1513 
1514     /**
1515      * Returns true if the next token matches the pattern constructed from the
1516      * specified string. The scanner does not advance past any input.
1517      *
1518      * <p> An invocation of this method of the form {@code hasNext(pattern)}
1519      * behaves in exactly the same way as the invocation
1520      * {@code hasNext(Pattern.compile(pattern))}.
1521      *
1522      * @param pattern a string specifying the pattern to scan
1523      * @return true if and only if this scanner has another token matching


< prev index next >