554 return floatPattern; 555 } 556 private Pattern decimalPattern() { 557 if (decimalPattern == null) { 558 buildFloatAndDecimalPattern(); 559 } 560 return decimalPattern; 561 } 562 563 // Constructors 564 565 /** 566 * Constructs a <code>Scanner</code> that returns values scanned 567 * from the specified source delimited by the specified pattern. 568 * 569 * @param source A character source implementing the Readable interface 570 * @param pattern A delimiting pattern 571 * @return A scanner with the specified source and pattern 572 */ 573 private Scanner(Readable source, Pattern pattern) { 574 if (source == null) 575 throw new NullPointerException("source"); 576 if (pattern == null) 577 throw new NullPointerException("pattern"); 578 this.source = source; 579 delimPattern = pattern; 580 buf = CharBuffer.allocate(BUFFER_SIZE); 581 buf.limit(0); 582 matcher = delimPattern.matcher(buf); 583 matcher.useTransparentBounds(true); 584 matcher.useAnchoringBounds(false); 585 useLocale(Locale.getDefault(Locale.Category.FORMAT)); 586 } 587 588 /** 589 * Constructs a new <code>Scanner</code> that produces values scanned 590 * from the specified source. 591 * 592 * @param source A character source implementing the {@link Readable} 593 * interface 594 */ 595 public Scanner(Readable source) { 596 this(source, WHITESPACE_PATTERN); 597 } 598 599 /** 600 * Constructs a new <code>Scanner</code> that produces values scanned 601 * from the specified input stream. Bytes from the stream are converted 602 * into characters using the underlying platform's 603 * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}. 604 * 605 * @param source An input stream to be scanned 606 */ 607 public Scanner(InputStream source) { 608 this(new InputStreamReader(source), WHITESPACE_PATTERN); 609 } 610 611 /** 612 * Constructs a new <code>Scanner</code> that produces values scanned 613 * from the specified input stream. Bytes from the stream are converted 614 * into characters using the specified charset. 615 * 616 * @param source An input stream to be scanned 617 * @param charsetName The encoding type used to convert bytes from the 618 * stream into characters to be scanned 619 * @throws IllegalArgumentException if the specified character set 620 * does not exist 621 */ 622 public Scanner(InputStream source, String charsetName) { 623 this(makeReadable(source, charsetName), WHITESPACE_PATTERN); 624 } 625 626 private static Readable makeReadable(InputStream source, 627 String charsetName) 628 { 629 if (source == null) 630 throw new NullPointerException("source"); 631 InputStreamReader isr = null; 632 try { 633 isr = new InputStreamReader(source, charsetName); 634 } catch (UnsupportedEncodingException uee) { 635 IllegalArgumentException iae = new IllegalArgumentException(); 636 iae.initCause(uee); 637 throw iae; 638 } 639 return isr; 640 } 641 642 /** 643 * Constructs a new <code>Scanner</code> that produces values scanned 644 * from the specified file. Bytes from the file are converted into 645 * characters using the underlying platform's 646 * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}. 647 * 648 * @param source A file to be scanned 649 * @throws FileNotFoundException if source is not found 650 */ 651 public Scanner(File source) 652 throws FileNotFoundException 653 { 654 this((ReadableByteChannel)(new FileInputStream(source).getChannel())); 655 } 656 657 /** 658 * Constructs a new <code>Scanner</code> 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 charsetName The encoding type used to convert bytes from the file 664 * into characters to be scanned 665 * @throws FileNotFoundException if source is not found 666 * @throws IllegalArgumentException if the specified encoding is 667 * not found 668 */ 669 public Scanner(File source, String charsetName) 670 throws FileNotFoundException 671 { 672 this((ReadableByteChannel)(new FileInputStream(source).getChannel()), 673 charsetName); 674 } 675 676 /** 677 * Constructs a new <code>Scanner</code> that produces values scanned 678 * from the specified file. Bytes from the file are converted into 679 * characters using the underlying platform's 680 * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}. 681 * 682 * @param source 683 * A file to be scanned 684 * @throws IOException 685 * if an I/O error occurs opening source 686 * 687 * @since 1.7 688 */ 689 public Scanner(FileRef source) 690 throws IOException 691 { 692 this(source.newInputStream()); 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 specified charset. 699 * 700 * @param source 701 * A file to be scanned 702 * @param charsetName 703 * The encoding type used to convert bytes from the file 704 * into characters to be scanned 705 * @throws IOException 706 * if an I/O error occurs opening source 707 * @throws IllegalArgumentException 708 * if the specified encoding is not found 709 * @since 1.7 710 */ 711 public Scanner(FileRef source, String charsetName) 712 throws IOException 713 { 714 this(source.newInputStream(), charsetName); 715 } 716 717 /** 718 * Constructs a new <code>Scanner</code> that produces values scanned 719 * from the specified string. 720 * 721 * @param source A string to scan 722 */ 723 public Scanner(String source) { 724 this(new StringReader(source), WHITESPACE_PATTERN); 725 } 726 727 /** 728 * Constructs a new <code>Scanner</code> that produces values scanned 729 * from the specified channel. Bytes from the source are converted into 730 * characters using the underlying platform's 731 * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}. 732 * 733 * @param source A channel to scan 734 */ 735 public Scanner(ReadableByteChannel source) { 736 this(makeReadable(source), WHITESPACE_PATTERN); 737 } 738 739 private static Readable makeReadable(ReadableByteChannel source) { 740 if (source == null) 741 throw new NullPointerException("source"); 742 String defaultCharsetName = 743 java.nio.charset.Charset.defaultCharset().name(); 744 return Channels.newReader(source, 745 java.nio.charset.Charset.defaultCharset().name()); 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 specified charset. 752 * 753 * @param source A channel to scan 754 * @param charsetName The encoding type used to convert bytes from the 755 * channel into characters to be scanned 756 * @throws IllegalArgumentException if the specified character set 757 * does not exist 758 */ 759 public Scanner(ReadableByteChannel source, String charsetName) { 760 this(makeReadable(source, charsetName), WHITESPACE_PATTERN); 761 } 762 763 private static Readable makeReadable(ReadableByteChannel source, 764 String charsetName) 765 { 766 if (source == null) 767 throw new NullPointerException("source"); 768 if (!Charset.isSupported(charsetName)) 769 throw new IllegalArgumentException(charsetName); 770 return Channels.newReader(source, charsetName); 771 } 772 773 // Private primitives used to support scanning 774 775 private void saveState() { 776 savedScannerPosition = position; 777 } 778 779 private void revertState() { 780 this.position = savedScannerPosition; 781 savedScannerPosition = -1; 782 skipped = false; 783 } 784 785 private boolean revertState(boolean b) { 786 this.position = savedScannerPosition; 787 savedScannerPosition = -1; 788 skipped = false; 789 return b; 790 } 791 792 private void cacheResult() { | 554 return floatPattern; 555 } 556 private Pattern decimalPattern() { 557 if (decimalPattern == null) { 558 buildFloatAndDecimalPattern(); 559 } 560 return decimalPattern; 561 } 562 563 // Constructors 564 565 /** 566 * Constructs a <code>Scanner</code> that returns values scanned 567 * from the specified source delimited by the specified pattern. 568 * 569 * @param source A character source implementing the Readable interface 570 * @param pattern A delimiting pattern 571 * @return A scanner with the specified source and pattern 572 */ 573 private Scanner(Readable source, Pattern pattern) { 574 assert source != null : "source should not be null"; 575 assert pattern != null : "pattern should not be null"; 576 this.source = source; 577 delimPattern = pattern; 578 buf = CharBuffer.allocate(BUFFER_SIZE); 579 buf.limit(0); 580 matcher = delimPattern.matcher(buf); 581 matcher.useTransparentBounds(true); 582 matcher.useAnchoringBounds(false); 583 useLocale(Locale.getDefault(Locale.Category.FORMAT)); 584 } 585 586 /** 587 * Constructs a new <code>Scanner</code> that produces values scanned 588 * from the specified source. 589 * 590 * @param source A character source implementing the {@link Readable} 591 * interface 592 */ 593 public Scanner(Readable source) { 594 this(Objects.nonNull(source, "source"), WHITESPACE_PATTERN); 595 } 596 597 /** 598 * Constructs a new <code>Scanner</code> that produces values scanned 599 * from the specified input stream. Bytes from the stream are converted 600 * into characters using the underlying platform's 601 * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}. 602 * 603 * @param source An input stream to be scanned 604 */ 605 public Scanner(InputStream source) { 606 this(new InputStreamReader(source), WHITESPACE_PATTERN); 607 } 608 609 /** 610 * Constructs a new <code>Scanner</code> that produces values scanned 611 * from the specified input stream. Bytes from the stream are converted 612 * into characters using the specified charset. 613 * 614 * @param source An input stream to be scanned 615 * @param charsetName The encoding type used to convert bytes from the 616 * stream into characters to be scanned 617 * @throws IllegalArgumentException if the specified character set 618 * does not exist 619 */ 620 public Scanner(InputStream source, String charsetName) { 621 this(makeReadable(Objects.nonNull(source, "source"), toCharset(charsetName)), 622 WHITESPACE_PATTERN); 623 } 624 625 /** 626 * Returns a charset object for the given charset name. 627 * @throws NullPointerException is csn is null 628 * @throws IllegalArgumentException if the charset is not supported 629 */ 630 private static Charset toCharset(String csn) { 631 Objects.nonNull(csn, "charsetName"); 632 try { 633 return Charset.forName(csn); 634 } catch (IllegalCharsetNameException unused) { 635 /* swallow this exception since IllegalArgumentException 636 * will be thrown */ 637 } 638 throw new IllegalArgumentException(new UnsupportedEncodingException(csn)); 639 } 640 641 private static Readable makeReadable(InputStream source, Charset charset) { 642 return new InputStreamReader(source, charset); 643 } 644 645 /** 646 * Constructs a new <code>Scanner</code> that produces values scanned 647 * from the specified file. Bytes from the file are converted into 648 * characters using the underlying platform's 649 * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}. 650 * 651 * @param source A file to be scanned 652 * @throws FileNotFoundException if source is not found 653 */ 654 public Scanner(File source) throws FileNotFoundException { 655 this((ReadableByteChannel)(new FileInputStream(source).getChannel())); 656 } 657 658 /** 659 * Constructs a new <code>Scanner</code> that produces values scanned 660 * from the specified file. Bytes from the file are converted into 661 * characters using the specified charset. 662 * 663 * @param source A file to be scanned 664 * @param charsetName The encoding type used to convert bytes from the file 665 * into characters to be scanned 666 * @throws FileNotFoundException if source is not found 667 * @throws IllegalArgumentException if the specified encoding is 668 * not found 669 */ 670 public Scanner(File source, String charsetName) 671 throws FileNotFoundException 672 { 673 this(Objects.nonNull(source), toDecoder(charsetName)); 674 } 675 676 private Scanner(File source, CharsetDecoder dec) 677 throws FileNotFoundException 678 { 679 this(makeReadable((ReadableByteChannel)(new FileInputStream(source).getChannel()), dec)); 680 } 681 682 private static CharsetDecoder toDecoder(String charsetName) { 683 try { 684 return Charset.forName(charsetName).newDecoder(); 685 } catch (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 */ 756 public Scanner(ReadableByteChannel source) { 757 this(makeReadable(Objects.nonNull(source, "source")), 758 WHITESPACE_PATTERN); 759 } 760 761 private static Readable makeReadable(ReadableByteChannel source) { 762 return makeReadable(source, Charset.defaultCharset().newDecoder()); 763 } 764 765 /** 766 * Constructs a new <code>Scanner</code> that produces values scanned 767 * from the specified channel. Bytes from the source are converted into 768 * characters using the specified charset. 769 * 770 * @param source A channel to scan 771 * @param charsetName The encoding type used to convert bytes from the 772 * channel into characters to be scanned 773 * @throws IllegalArgumentException if the specified character set 774 * does not exist 775 */ 776 public Scanner(ReadableByteChannel source, String charsetName) { 777 this(makeReadable(Objects.nonNull(source, "source"), toDecoder(charsetName)), 778 WHITESPACE_PATTERN); 779 } 780 781 // Private primitives used to support scanning 782 783 private void saveState() { 784 savedScannerPosition = position; 785 } 786 787 private void revertState() { 788 this.position = savedScannerPosition; 789 savedScannerPosition = -1; 790 skipped = false; 791 } 792 793 private boolean revertState(boolean b) { 794 this.position = savedScannerPosition; 795 savedScannerPosition = -1; 796 skipped = false; 797 return b; 798 } 799 800 private void cacheResult() { |