< prev index next >

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

Print this page




 558      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
 559      *
 560      * @param  source An input stream to be scanned
 561      */
 562     public Scanner(InputStream source) {
 563         this(new InputStreamReader(source), WHITESPACE_PATTERN);
 564     }
 565 
 566     /**
 567      * Constructs a new {@code Scanner} that produces values scanned
 568      * from the specified input stream. Bytes from the stream are converted
 569      * into characters using the specified charset.
 570      *
 571      * @param  source An input stream to be scanned
 572      * @param charsetName The encoding type used to convert bytes from the
 573      *        stream into characters to be scanned
 574      * @throws IllegalArgumentException if the specified character set
 575      *         does not exist
 576      */
 577     public Scanner(InputStream source, String charsetName) {
 578         this(makeReadable(Objects.requireNonNull(source, "source"), toCharset(charsetName)),














 579              WHITESPACE_PATTERN);
 580     }
 581 
 582     /**
 583      * Returns a charset object for the given charset name.
 584      * @throws NullPointerException          is csn is null
 585      * @throws IllegalArgumentException      if the charset is not supported
 586      */
 587     private static Charset toCharset(String csn) {
 588         Objects.requireNonNull(csn, "charsetName");
 589         try {
 590             return Charset.forName(csn);
 591         } catch (IllegalCharsetNameException|UnsupportedCharsetException e) {
 592             // IllegalArgumentException should be thrown
 593             throw new IllegalArgumentException(e);
 594         }
 595     }
 596 










 597     private static Readable makeReadable(InputStream source, Charset charset) {

 598         return new InputStreamReader(source, charset);
 599     }
 600 
 601     /**
 602      * Constructs a new {@code Scanner} that produces values scanned
 603      * from the specified file. Bytes from the file are converted into
 604      * characters using the underlying platform's
 605      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
 606      *
 607      * @param  source A file to be scanned
 608      * @throws FileNotFoundException if source is not found
 609      */
 610     public Scanner(File source) throws FileNotFoundException {
 611         this((ReadableByteChannel)(new FileInputStream(source).getChannel()));
 612     }
 613 
 614     /**
 615      * Constructs a new {@code Scanner} that produces values scanned
 616      * from the specified file. Bytes from the file are converted into
 617      * characters using the specified charset.
 618      *
 619      * @param  source A file to be scanned
 620      * @param charsetName The encoding type used to convert bytes from the file
 621      *        into characters to be scanned
 622      * @throws FileNotFoundException if source is not found
 623      * @throws IllegalArgumentException if the specified encoding is
 624      *         not found
 625      */
 626     public Scanner(File source, String charsetName)
 627         throws FileNotFoundException
 628     {
 629         this(Objects.requireNonNull(source), toDecoder(charsetName));
 630     }
 631 
















 632     private Scanner(File source, CharsetDecoder dec)
 633         throws FileNotFoundException
 634     {
 635         this(makeReadable((ReadableByteChannel)(new FileInputStream(source).getChannel()), dec));
 636     }
 637 
 638     private static CharsetDecoder toDecoder(String charsetName) {
 639         Objects.requireNonNull(charsetName, "charsetName");
 640         try {
 641             return Charset.forName(charsetName).newDecoder();
 642         } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
 643             throw new IllegalArgumentException(charsetName);
 644         }
 645     }
 646 
 647     private static Readable makeReadable(ReadableByteChannel source,
 648                                          CharsetDecoder dec) {
 649         return Channels.newReader(source, dec, -1);
 650     }
 651 






 652     /**
 653      * Constructs a new {@code Scanner} that produces values scanned
 654      * from the specified file. Bytes from the file are converted into
 655      * characters using the underlying platform's
 656      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
 657      *
 658      * @param   source
 659      *          the path to the file to be scanned
 660      * @throws  IOException
 661      *          if an I/O error occurs opening source
 662      *
 663      * @since   1.7
 664      */
 665     public Scanner(Path source)
 666         throws IOException
 667     {
 668         this(Files.newInputStream(source));
 669     }
 670 
 671     /**
 672      * Constructs a new {@code Scanner} that produces values scanned
 673      * from the specified file. Bytes from the file are converted into
 674      * characters using the specified charset.
 675      *
 676      * @param   source
 677      *          the path to the file to be scanned
 678      * @param   charsetName
 679      *          The encoding type used to convert bytes from the file
 680      *          into characters to be scanned
 681      * @throws  IOException
 682      *          if an I/O error occurs opening source
 683      * @throws  IllegalArgumentException
 684      *          if the specified encoding is not found
 685      * @since   1.7
 686      */
 687     public Scanner(Path source, String charsetName) throws IOException {
 688         this(Objects.requireNonNull(source), toCharset(charsetName));
 689     }
 690 
 691     private Scanner(Path source, Charset charset)  throws IOException {
 692         this(makeReadable(Files.newInputStream(source), charset));














 693     }
 694 
 695     /**
 696      * Constructs a new {@code Scanner} that produces values scanned
 697      * from the specified string.
 698      *
 699      * @param  source A string to scan
 700      */
 701     public Scanner(String source) {
 702         this(new StringReader(source), WHITESPACE_PATTERN);
 703     }
 704 
 705     /**
 706      * Constructs a new {@code Scanner} that produces values scanned
 707      * from the specified channel. Bytes from the source are converted into
 708      * characters using the underlying platform's
 709      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
 710      *
 711      * @param  source A channel to scan
 712      */


 715              WHITESPACE_PATTERN);
 716     }
 717 
 718     private static Readable makeReadable(ReadableByteChannel source) {
 719         return makeReadable(source, Charset.defaultCharset().newDecoder());
 720     }
 721 
 722     /**
 723      * Constructs a new {@code Scanner} that produces values scanned
 724      * from the specified channel. Bytes from the source are converted into
 725      * characters using the specified charset.
 726      *
 727      * @param  source A channel to scan
 728      * @param charsetName The encoding type used to convert bytes from the
 729      *        channel into characters to be scanned
 730      * @throws IllegalArgumentException if the specified character set
 731      *         does not exist
 732      */
 733     public Scanner(ReadableByteChannel source, String charsetName) {
 734         this(makeReadable(Objects.requireNonNull(source, "source"), toDecoder(charsetName)),















 735              WHITESPACE_PATTERN);
 736     }
 737 
 738     // Private primitives used to support scanning
 739 
 740     private void saveState() {
 741         savedScannerPosition = position;
 742     }
 743 
 744     private void revertState() {
 745         this.position = savedScannerPosition;
 746         savedScannerPosition = -1;
 747         skipped = false;
 748     }
 749 
 750     private boolean revertState(boolean b) {
 751         this.position = savedScannerPosition;
 752         savedScannerPosition = -1;
 753         skipped = false;
 754         return b;




 558      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
 559      *
 560      * @param  source An input stream to be scanned
 561      */
 562     public Scanner(InputStream source) {
 563         this(new InputStreamReader(source), WHITESPACE_PATTERN);
 564     }
 565 
 566     /**
 567      * Constructs a new {@code Scanner} that produces values scanned
 568      * from the specified input stream. Bytes from the stream are converted
 569      * into characters using the specified charset.
 570      *
 571      * @param  source An input stream to be scanned
 572      * @param charsetName The encoding type used to convert bytes from the
 573      *        stream into characters to be scanned
 574      * @throws IllegalArgumentException if the specified character set
 575      *         does not exist
 576      */
 577     public Scanner(InputStream source, String charsetName) {
 578         this(source, toCharset(charsetName));
 579     }
 580 
 581     /**
 582      * Constructs a new {@code Scanner} that produces values scanned
 583      * from the specified input stream. Bytes from the stream are converted
 584      * into characters using the specified charset.
 585      *
 586      * @param  source an input stream to be scanned
 587      * @param  charset the charset used to convert bytes from the file
 588      *         into characters to be scanned
 589      * @since  10
 590      */
 591     public Scanner(InputStream source, Charset charset) {
 592         this(makeReadable(Objects.requireNonNull(source, "source"), charset),
 593              WHITESPACE_PATTERN);
 594     }
 595 
 596     /**
 597      * Returns a charset object for the given charset name.
 598      * @throws NullPointerException          is csn is null
 599      * @throws IllegalArgumentException      if the charset is not supported
 600      */
 601     private static Charset toCharset(String csn) {
 602         Objects.requireNonNull(csn, "charsetName");
 603         try {
 604             return Charset.forName(csn);
 605         } catch (IllegalCharsetNameException|UnsupportedCharsetException e) {
 606             // IllegalArgumentException should be thrown
 607             throw new IllegalArgumentException(e);
 608         }
 609     }
 610 
 611     /*
 612      * This method is added so that null-check on charset can be performed before
 613      * creating InputStream as an existing test required it.
 614     */
 615     private static Readable makeReadable(Path source, Charset charset)
 616             throws IOException {
 617         Objects.requireNonNull(charset, "charset");
 618         return makeReadable(Files.newInputStream(source), charset);
 619     }
 620 
 621     private static Readable makeReadable(InputStream source, Charset charset) {
 622         Objects.requireNonNull(charset, "charset");
 623         return new InputStreamReader(source, charset);
 624     }
 625 
 626     /**
 627      * Constructs a new {@code Scanner} that produces values scanned
 628      * from the specified file. Bytes from the file are converted into
 629      * characters using the underlying platform's
 630      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
 631      *
 632      * @param  source A file to be scanned
 633      * @throws FileNotFoundException if source is not found
 634      */
 635     public Scanner(File source) throws FileNotFoundException {
 636         this((ReadableByteChannel)(new FileInputStream(source).getChannel()));
 637     }
 638 
 639     /**
 640      * Constructs a new {@code Scanner} that produces values scanned
 641      * from the specified file. Bytes from the file are converted into
 642      * characters using the specified charset.
 643      *
 644      * @param  source A file to be scanned
 645      * @param charsetName The encoding type used to convert bytes from the file
 646      *        into characters to be scanned
 647      * @throws FileNotFoundException if source is not found
 648      * @throws IllegalArgumentException if the specified encoding is
 649      *         not found
 650      */
 651     public Scanner(File source, String charsetName)
 652         throws FileNotFoundException
 653     {
 654         this(Objects.requireNonNull(source), toDecoder(charsetName));
 655     }
 656 
 657     /**
 658      * Constructs a new {@code Scanner} that produces values scanned
 659      * from the specified file. Bytes from the file are converted into
 660      * characters using the specified charset.
 661      *
 662      * @param  source A file to be scanned
 663      * @param  charset The charset used to convert bytes from the file
 664      *         into characters to be scanned
 665      * @throws IOException
 666      *         if an I/O error occurs opening the source
 667      * @since  10
 668      */
 669     public Scanner(File source, Charset charset) throws IOException {
 670         this(Objects.requireNonNull(source), charset.newDecoder());
 671     }
 672 
 673     private Scanner(File source, CharsetDecoder dec)
 674         throws FileNotFoundException
 675     {
 676         this(makeReadable((ReadableByteChannel)(new FileInputStream(source).getChannel()), dec));
 677     }
 678 
 679     private static CharsetDecoder toDecoder(String charsetName) {
 680         Objects.requireNonNull(charsetName, "charsetName");
 681         try {
 682             return Charset.forName(charsetName).newDecoder();
 683         } catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
 684             throw new IllegalArgumentException(charsetName);
 685         }
 686     }
 687 
 688     private static Readable makeReadable(ReadableByteChannel source,
 689                                          CharsetDecoder dec) {
 690         return Channels.newReader(source, dec, -1);
 691     }
 692 
 693     private static Readable makeReadable(ReadableByteChannel source,
 694                                          Charset charset) {
 695         Objects.requireNonNull(charset, "charset");
 696         return Channels.newReader(source, charset);
 697     }
 698 
 699     /**
 700      * Constructs a new {@code Scanner} that produces values scanned
 701      * from the specified file. Bytes from the file are converted into
 702      * characters using the underlying platform's
 703      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
 704      *
 705      * @param   source
 706      *          the path to the file to be scanned
 707      * @throws  IOException
 708      *          if an I/O error occurs opening source
 709      *
 710      * @since   1.7
 711      */
 712     public Scanner(Path source)
 713         throws IOException
 714     {
 715         this(Files.newInputStream(source));
 716     }
 717 
 718     /**
 719      * Constructs a new {@code Scanner} that produces values scanned
 720      * from the specified file. Bytes from the file are converted into
 721      * characters using the specified charset.
 722      *
 723      * @param   source
 724      *          the path to the file to be scanned
 725      * @param   charsetName
 726      *          The encoding type used to convert bytes from the file
 727      *          into characters to be scanned
 728      * @throws  IOException
 729      *          if an I/O error occurs opening source
 730      * @throws  IllegalArgumentException
 731      *          if the specified encoding is not found
 732      * @since   1.7
 733      */
 734     public Scanner(Path source, String charsetName) throws IOException {
 735         this(Objects.requireNonNull(source), toCharset(charsetName));
 736     }
 737 
 738     /**
 739      * Constructs a new {@code Scanner} that produces values scanned
 740      * from the specified file. Bytes from the file are converted into
 741      * characters using the specified charset.
 742      *
 743      * @param   source
 744      *          the path to the file to be scanned
 745      * @param   charset
 746      *          the charset used to convert bytes from the file
 747      *          into characters to be scanned
 748      * @throws  IOException
 749      *          if an I/O error occurs opening the source
 750      * @since   10
 751      */
 752     public Scanner(Path source, Charset charset)  throws IOException {
 753         this(makeReadable(source, charset));
 754     }
 755 
 756     /**
 757      * Constructs a new {@code Scanner} that produces values scanned
 758      * from the specified string.
 759      *
 760      * @param  source A string to scan
 761      */
 762     public Scanner(String source) {
 763         this(new StringReader(source), WHITESPACE_PATTERN);
 764     }
 765 
 766     /**
 767      * Constructs a new {@code Scanner} that produces values scanned
 768      * from the specified channel. Bytes from the source are converted into
 769      * characters using the underlying platform's
 770      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
 771      *
 772      * @param  source A channel to scan
 773      */


 776              WHITESPACE_PATTERN);
 777     }
 778 
 779     private static Readable makeReadable(ReadableByteChannel source) {
 780         return makeReadable(source, Charset.defaultCharset().newDecoder());
 781     }
 782 
 783     /**
 784      * Constructs a new {@code Scanner} that produces values scanned
 785      * from the specified channel. Bytes from the source are converted into
 786      * characters using the specified charset.
 787      *
 788      * @param  source A channel to scan
 789      * @param charsetName The encoding type used to convert bytes from the
 790      *        channel into characters to be scanned
 791      * @throws IllegalArgumentException if the specified character set
 792      *         does not exist
 793      */
 794     public Scanner(ReadableByteChannel source, String charsetName) {
 795         this(makeReadable(Objects.requireNonNull(source, "source"), toDecoder(charsetName)),
 796              WHITESPACE_PATTERN);
 797     }
 798 
 799     /**
 800      * Constructs a new {@code Scanner} that produces values scanned
 801      * from the specified channel. Bytes from the source are converted into
 802      * characters using the specified charset.
 803      *
 804      * @param source a channel to scan
 805      * @param charset the encoding type used to convert bytes from the
 806      *        channel into characters to be scanned
 807      * @since 10
 808      */
 809     public Scanner(ReadableByteChannel source, Charset charset) {
 810         this(makeReadable(Objects.requireNonNull(source, "source"), charset),
 811              WHITESPACE_PATTERN);
 812     }
 813 
 814     // Private primitives used to support scanning
 815 
 816     private void saveState() {
 817         savedScannerPosition = position;
 818     }
 819 
 820     private void revertState() {
 821         this.position = savedScannerPosition;
 822         savedScannerPosition = -1;
 823         skipped = false;
 824     }
 825 
 826     private boolean revertState(boolean b) {
 827         this.position = savedScannerPosition;
 828         savedScannerPosition = -1;
 829         skipped = false;
 830         return b;


< prev index next >