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