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

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.nio.file.FileRef;

  29 import java.util.regex.*;
  30 import java.io.*;
  31 import java.math.*;
  32 import java.nio.*;
  33 import java.nio.channels.*;
  34 import java.nio.charset.*;
  35 import java.text.*;
  36 import java.util.Locale;
  37 import sun.misc.LRUCache;
  38 
  39 /**
  40  * A simple text scanner which can parse primitive types and strings using
  41  * regular expressions.
  42  *
  43  * <p>A <code>Scanner</code> breaks its input into tokens using a
  44  * delimiter pattern, which by default matches whitespace. The resulting
  45  * tokens may then be converted into values of different types using the
  46  * various <tt>next</tt> methods.
  47  *
  48  * <p>For example, this code allows a user to read a number from


 682         Objects.nonNull(charsetName, "charsetName");
 683         try {
 684             return Charset.forName(charsetName).newDecoder();
 685         } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
 686             throw new IllegalArgumentException(charsetName);
 687         }
 688     }
 689 
 690     private static Readable makeReadable(ReadableByteChannel source,
 691                                          CharsetDecoder dec) {
 692         return Channels.newReader(source, dec, -1);
 693     }
 694 
 695     /**
 696      * Constructs a new <code>Scanner</code> that produces values scanned
 697      * from the specified file. Bytes from the file are converted into
 698      * characters using the underlying platform's
 699      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
 700      *
 701      * @param   source
 702      *          A file to be scanned
 703      * @throws  IOException
 704      *          if an I/O error occurs opening source
 705      *
 706      * @since   1.7
 707      */
 708     public Scanner(FileRef source)
 709         throws IOException
 710     {
 711         this(source.newInputStream());
 712     }
 713 
 714     /**
 715      * Constructs a new <code>Scanner</code> that produces values scanned
 716      * from the specified file. Bytes from the file are converted into
 717      * characters using the specified charset.
 718      *
 719      * @param   source
 720      *          A file to be scanned
 721      * @param   charsetName
 722      *          The encoding type used to convert bytes from the file
 723      *          into characters to be scanned
 724      * @throws  IOException
 725      *          if an I/O error occurs opening source
 726      * @throws  IllegalArgumentException
 727      *          if the specified encoding is not found
 728      * @since   1.7
 729      */
 730     public Scanner(FileRef source, String charsetName) throws IOException {
 731         this(Objects.nonNull(source), toCharset(charsetName));
 732     }
 733 
 734     private Scanner(FileRef source, Charset charset)  throws IOException {
 735         this(makeReadable(source.newInputStream(), charset));
 736     }
 737 
 738     /**
 739      * Constructs a new <code>Scanner</code> that produces values scanned
 740      * from the specified string.
 741      *
 742      * @param  source A string to scan
 743      */
 744     public Scanner(String source) {
 745         this(new StringReader(source), WHITESPACE_PATTERN);
 746     }
 747 
 748     /**
 749      * Constructs a new <code>Scanner</code> that produces values scanned
 750      * from the specified channel. Bytes from the source are converted into
 751      * characters using the underlying platform's
 752      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
 753      *
 754      * @param  source A channel to scan
 755      */




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.nio.file.Path;
  29 import java.nio.file.Files;
  30 import java.util.regex.*;
  31 import java.io.*;
  32 import java.math.*;
  33 import java.nio.*;
  34 import java.nio.channels.*;
  35 import java.nio.charset.*;
  36 import java.text.*;
  37 import java.util.Locale;
  38 import sun.misc.LRUCache;
  39 
  40 /**
  41  * A simple text scanner which can parse primitive types and strings using
  42  * regular expressions.
  43  *
  44  * <p>A <code>Scanner</code> breaks its input into tokens using a
  45  * delimiter pattern, which by default matches whitespace. The resulting
  46  * tokens may then be converted into values of different types using the
  47  * various <tt>next</tt> methods.
  48  *
  49  * <p>For example, this code allows a user to read a number from


 683         Objects.nonNull(charsetName, "charsetName");
 684         try {
 685             return Charset.forName(charsetName).newDecoder();
 686         } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
 687             throw new IllegalArgumentException(charsetName);
 688         }
 689     }
 690 
 691     private static Readable makeReadable(ReadableByteChannel source,
 692                                          CharsetDecoder dec) {
 693         return Channels.newReader(source, dec, -1);
 694     }
 695 
 696     /**
 697      * Constructs a new <code>Scanner</code> that produces values scanned
 698      * from the specified file. Bytes from the file are converted into
 699      * characters using the underlying platform's
 700      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
 701      *
 702      * @param   source
 703      *          the path to the file to be scanned
 704      * @throws  IOException
 705      *          if an I/O error occurs opening source
 706      *
 707      * @since   1.7
 708      */
 709     public Scanner(Path source)
 710         throws IOException
 711     {
 712         this(Files.newInputStream(source));
 713     }
 714 
 715     /**
 716      * Constructs a new <code>Scanner</code> that produces values scanned
 717      * from the specified file. Bytes from the file are converted into
 718      * characters using the specified charset.
 719      *
 720      * @param   source
 721      *          the path to the file to be scanned
 722      * @param   charsetName
 723      *          The encoding type used to convert bytes from the file
 724      *          into characters to be scanned
 725      * @throws  IOException
 726      *          if an I/O error occurs opening source
 727      * @throws  IllegalArgumentException
 728      *          if the specified encoding is not found
 729      * @since   1.7
 730      */
 731     public Scanner(Path source, String charsetName) throws IOException {
 732         this(Objects.nonNull(source), toCharset(charsetName));
 733     }
 734 
 735     private Scanner(Path source, Charset charset)  throws IOException {
 736         this(makeReadable(Files.newInputStream(source), charset));
 737     }
 738 
 739     /**
 740      * Constructs a new <code>Scanner</code> that produces values scanned
 741      * from the specified string.
 742      *
 743      * @param  source A string to scan
 744      */
 745     public Scanner(String source) {
 746         this(new StringReader(source), WHITESPACE_PATTERN);
 747     }
 748 
 749     /**
 750      * Constructs a new <code>Scanner</code> that produces values scanned
 751      * from the specified channel. Bytes from the source are converted into
 752      * characters using the underlying platform's
 753      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
 754      *
 755      * @param  source A channel to scan
 756      */