forked from XWxiaowei/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanner.java
More file actions
2617 lines (2447 loc) · 97.6 KB
/
Scanner.java
File metadata and controls
2617 lines (2447 loc) · 97.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package java.util;
import java.nio.file.Path;
import java.nio.file.Files;
import java.util.regex.*;
import java.io.*;
import java.math.*;
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.text.*;
import java.util.Locale;
import sun.misc.LRUCache;
/**
* A simple text scanner which can parse primitive types and strings using
* regular expressions.
*
* <p>A <code>Scanner</code> breaks its input into tokens using a
* delimiter pattern, which by default matches whitespace. The resulting
* tokens may then be converted into values of different types using the
* various <tt>next</tt> methods.
*
* <p>For example, this code allows a user to read a number from
* <tt>System.in</tt>:
* <blockquote><pre>{@code
* Scanner sc = new Scanner(System.in);
* int i = sc.nextInt();
* }</pre></blockquote>
*
* <p>As another example, this code allows <code>long</code> types to be
* assigned from entries in a file <code>myNumbers</code>:
* <blockquote><pre>{@code
* Scanner sc = new Scanner(new File("myNumbers"));
* while (sc.hasNextLong()) {
* long aLong = sc.nextLong();
* }
* }</pre></blockquote>
*
* <p>The scanner can also use delimiters other than whitespace. This
* example reads several items in from a string:
* <blockquote><pre>{@code
* String input = "1 fish 2 fish red fish blue fish";
* Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
* System.out.println(s.nextInt());
* System.out.println(s.nextInt());
* System.out.println(s.next());
* System.out.println(s.next());
* s.close();
* }</pre></blockquote>
* <p>
* prints the following output:
* <blockquote><pre>{@code
* 1
* 2
* red
* blue
* }</pre></blockquote>
*
* <p>The same output can be generated with this code, which uses a regular
* expression to parse all four tokens at once:
* <blockquote><pre>{@code
* String input = "1 fish 2 fish red fish blue fish";
* Scanner s = new Scanner(input);
* s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");
* MatchResult result = s.match();
* for (int i=1; i<=result.groupCount(); i++)
* System.out.println(result.group(i));
* s.close();
* }</pre></blockquote>
*
* <p>The <a name="default-delimiter">default whitespace delimiter</a> used
* by a scanner is as recognized by {@link java.lang.Character}.{@link
* java.lang.Character#isWhitespace(char) isWhitespace}. The {@link #reset}
* method will reset the value of the scanner's delimiter to the default
* whitespace delimiter regardless of whether it was previously changed.
*
* <p>A scanning operation may block waiting for input.
*
* <p>The {@link #next} and {@link #hasNext} methods and their
* primitive-type companion methods (such as {@link #nextInt} and
* {@link #hasNextInt}) first skip any input that matches the delimiter
* pattern, and then attempt to return the next token. Both <tt>hasNext</tt>
* and <tt>next</tt> methods may block waiting for further input. Whether a
* <tt>hasNext</tt> method blocks has no connection to whether or not its
* associated <tt>next</tt> method will block.
*
* <p> The {@link #findInLine}, {@link #findWithinHorizon}, and {@link #skip}
* methods operate independently of the delimiter pattern. These methods will
* attempt to match the specified pattern with no regard to delimiters in the
* input and thus can be used in special circumstances where delimiters are
* not relevant. These methods may block waiting for more input.
*
* <p>When a scanner throws an {@link InputMismatchException}, the scanner
* will not pass the token that caused the exception, so that it may be
* retrieved or skipped via some other method.
*
* <p>Depending upon the type of delimiting pattern, empty tokens may be
* returned. For example, the pattern <tt>"\\s+"</tt> will return no empty
* tokens since it matches multiple instances of the delimiter. The delimiting
* pattern <tt>"\\s"</tt> could return empty tokens since it only passes one
* space at a time.
*
* <p> A scanner can read text from any object which implements the {@link
* java.lang.Readable} interface. If an invocation of the underlying
* readable's {@link java.lang.Readable#read} method throws an {@link
* java.io.IOException} then the scanner assumes that the end of the input
* has been reached. The most recent <tt>IOException</tt> thrown by the
* underlying readable can be retrieved via the {@link #ioException} method.
*
* <p>When a <code>Scanner</code> is closed, it will close its input source
* if the source implements the {@link java.io.Closeable} interface.
*
* <p>A <code>Scanner</code> is not safe for multithreaded use without
* external synchronization.
*
* <p>Unless otherwise mentioned, passing a <code>null</code> parameter into
* any method of a <code>Scanner</code> will cause a
* <code>NullPointerException</code> to be thrown.
*
* <p>A scanner will default to interpreting numbers as decimal unless a
* different radix has been set by using the {@link #useRadix} method. The
* {@link #reset} method will reset the value of the scanner's radix to
* <code>10</code> regardless of whether it was previously changed.
*
* <h3> <a name="localized-numbers">Localized numbers</a> </h3>
*
* <p> An instance of this class is capable of scanning numbers in the standard
* formats as well as in the formats of the scanner's locale. A scanner's
* <a name="initial-locale">initial locale </a>is the value returned by the {@link
* java.util.Locale#getDefault(Locale.Category)
* Locale.getDefault(Locale.Category.FORMAT)} method; it may be changed via the {@link
* #useLocale} method. The {@link #reset} method will reset the value of the
* scanner's locale to the initial locale regardless of whether it was
* previously changed.
*
* <p>The localized formats are defined in terms of the following parameters,
* which for a particular locale are taken from that locale's {@link
* java.text.DecimalFormat DecimalFormat} object, <tt>df</tt>, and its and
* {@link java.text.DecimalFormatSymbols DecimalFormatSymbols} object,
* <tt>dfs</tt>.
*
* <blockquote><dl>
* <dt><i>LocalGroupSeparator </i>
* <dd>The character used to separate thousands groups,
* <i>i.e.,</i> <tt>dfs.</tt>{@link
* java.text.DecimalFormatSymbols#getGroupingSeparator
* getGroupingSeparator()}
* <dt><i>LocalDecimalSeparator </i>
* <dd>The character used for the decimal point,
* <i>i.e.,</i> <tt>dfs.</tt>{@link
* java.text.DecimalFormatSymbols#getDecimalSeparator
* getDecimalSeparator()}
* <dt><i>LocalPositivePrefix </i>
* <dd>The string that appears before a positive number (may
* be empty), <i>i.e.,</i> <tt>df.</tt>{@link
* java.text.DecimalFormat#getPositivePrefix
* getPositivePrefix()}
* <dt><i>LocalPositiveSuffix </i>
* <dd>The string that appears after a positive number (may be
* empty), <i>i.e.,</i> <tt>df.</tt>{@link
* java.text.DecimalFormat#getPositiveSuffix
* getPositiveSuffix()}
* <dt><i>LocalNegativePrefix </i>
* <dd>The string that appears before a negative number (may
* be empty), <i>i.e.,</i> <tt>df.</tt>{@link
* java.text.DecimalFormat#getNegativePrefix
* getNegativePrefix()}
* <dt><i>LocalNegativeSuffix </i>
* <dd>The string that appears after a negative number (may be
* empty), <i>i.e.,</i> <tt>df.</tt>{@link
* java.text.DecimalFormat#getNegativeSuffix
* getNegativeSuffix()}
* <dt><i>LocalNaN </i>
* <dd>The string that represents not-a-number for
* floating-point values,
* <i>i.e.,</i> <tt>dfs.</tt>{@link
* java.text.DecimalFormatSymbols#getNaN
* getNaN()}
* <dt><i>LocalInfinity </i>
* <dd>The string that represents infinity for floating-point
* values, <i>i.e.,</i> <tt>dfs.</tt>{@link
* java.text.DecimalFormatSymbols#getInfinity
* getInfinity()}
* </dl></blockquote>
*
* <h4> <a name="number-syntax">Number syntax</a> </h4>
*
* <p> The strings that can be parsed as numbers by an instance of this class
* are specified in terms of the following regular-expression grammar, where
* Rmax is the highest digit in the radix being used (for example, Rmax is 9 in base 10).
*
* <dl>
* <dt><i>NonAsciiDigit</i>:
* <dd>A non-ASCII character c for which
* {@link java.lang.Character#isDigit Character.isDigit}<tt>(c)</tt>
* returns true
*
* <dt><i>Non0Digit</i>:
* <dd><tt>[1-</tt><i>Rmax</i><tt>] | </tt><i>NonASCIIDigit</i>
*
* <dt><i>Digit</i>:
* <dd><tt>[0-</tt><i>Rmax</i><tt>] | </tt><i>NonASCIIDigit</i>
*
* <dt><i>GroupedNumeral</i>:
* <dd><tt>( </tt><i>Non0Digit</i>
* <i>Digit</i><tt>?
* </tt><i>Digit</i><tt>?</tt>
* <dd> <tt>( </tt><i>LocalGroupSeparator</i>
* <i>Digit</i>
* <i>Digit</i>
* <i>Digit</i><tt> )+ )</tt>
*
* <dt><i>Numeral</i>:
* <dd><tt>( ( </tt><i>Digit</i><tt>+ )
* | </tt><i>GroupedNumeral</i><tt> )</tt>
*
* <dt><a name="Integer-regex"><i>Integer</i>:</a>
* <dd><tt>( [-+]? ( </tt><i>Numeral</i><tt>
* ) )</tt>
* <dd><tt>| </tt><i>LocalPositivePrefix</i> <i>Numeral</i>
* <i>LocalPositiveSuffix</i>
* <dd><tt>| </tt><i>LocalNegativePrefix</i> <i>Numeral</i>
* <i>LocalNegativeSuffix</i>
*
* <dt><i>DecimalNumeral</i>:
* <dd><i>Numeral</i>
* <dd><tt>| </tt><i>Numeral</i>
* <i>LocalDecimalSeparator</i>
* <i>Digit</i><tt>*</tt>
* <dd><tt>| </tt><i>LocalDecimalSeparator</i>
* <i>Digit</i><tt>+</tt>
*
* <dt><i>Exponent</i>:
* <dd><tt>( [eE] [+-]? </tt><i>Digit</i><tt>+ )</tt>
*
* <dt><a name="Decimal-regex"><i>Decimal</i>:</a>
* <dd><tt>( [-+]? </tt><i>DecimalNumeral</i>
* <i>Exponent</i><tt>? )</tt>
* <dd><tt>| </tt><i>LocalPositivePrefix</i>
* <i>DecimalNumeral</i>
* <i>LocalPositiveSuffix</i>
* <i>Exponent</i><tt>?</tt>
* <dd><tt>| </tt><i>LocalNegativePrefix</i>
* <i>DecimalNumeral</i>
* <i>LocalNegativeSuffix</i>
* <i>Exponent</i><tt>?</tt>
*
* <dt><i>HexFloat</i>:
* <dd><tt>[-+]? 0[xX][0-9a-fA-F]*\.[0-9a-fA-F]+
* ([pP][-+]?[0-9]+)?</tt>
*
* <dt><i>NonNumber</i>:
* <dd><tt>NaN
* | </tt><i>LocalNan</i><tt>
* | Infinity
* | </tt><i>LocalInfinity</i>
*
* <dt><i>SignedNonNumber</i>:
* <dd><tt>( [-+]? </tt><i>NonNumber</i><tt> )</tt>
* <dd><tt>| </tt><i>LocalPositivePrefix</i>
* <i>NonNumber</i>
* <i>LocalPositiveSuffix</i>
* <dd><tt>| </tt><i>LocalNegativePrefix</i>
* <i>NonNumber</i>
* <i>LocalNegativeSuffix</i>
*
* <dt><a name="Float-regex"><i>Float</i></a>:
* <dd><i>Decimal</i>
* <tt>| </tt><i>HexFloat</i>
* <tt>| </tt><i>SignedNonNumber</i>
*
* </dl>
* <p>Whitespace is not significant in the above regular expressions.
*
* @since 1.5
*/
public final class Scanner implements Iterator<String>, Closeable {
// Internal buffer used to hold input
private CharBuffer buf;
// Size of internal character buffer
private static final int BUFFER_SIZE = 1024; // change to 1024;
// The index into the buffer currently held by the Scanner
private int position;
// Internal matcher used for finding delimiters
private Matcher matcher;
// Pattern used to delimit tokens
private Pattern delimPattern;
// Pattern found in last hasNext operation
private Pattern hasNextPattern;
// Position after last hasNext operation
private int hasNextPosition;
// Result after last hasNext operation
private String hasNextResult;
// The input source
private Readable source;
// Boolean is true if source is done
private boolean sourceClosed = false;
// Boolean indicating more input is required
private boolean needInput = false;
// Boolean indicating if a delim has been skipped this operation
private boolean skipped = false;
// A store of a position that the scanner may fall back to
private int savedScannerPosition = -1;
// A cache of the last primitive type scanned
private Object typeCache = null;
// Boolean indicating if a match result is available
private boolean matchValid = false;
// Boolean indicating if this scanner has been closed
private boolean closed = false;
// The current radix used by this scanner
private int radix = 10;
// The default radix for this scanner
private int defaultRadix = 10;
// The locale used by this scanner
private Locale locale = null;
// A cache of the last few recently used Patterns
private LRUCache<String,Pattern> patternCache =
new LRUCache<String,Pattern>(7) {
protected Pattern create(String s) {
return Pattern.compile(s);
}
protected boolean hasName(Pattern p, String s) {
return p.pattern().equals(s);
}
};
// A holder of the last IOException encountered
private IOException lastException;
// A pattern for java whitespace
private static Pattern WHITESPACE_PATTERN = Pattern.compile(
"\\p{javaWhitespace}+");
// A pattern for any token
private static Pattern FIND_ANY_PATTERN = Pattern.compile("(?s).*");
// A pattern for non-ASCII digits
private static Pattern NON_ASCII_DIGIT = Pattern.compile(
"[\\p{javaDigit}&&[^0-9]]");
// Fields and methods to support scanning primitive types
/**
* Locale dependent values used to scan numbers
*/
private String groupSeparator = "\\,";
private String decimalSeparator = "\\.";
private String nanString = "NaN";
private String infinityString = "Infinity";
private String positivePrefix = "";
private String negativePrefix = "\\-";
private String positiveSuffix = "";
private String negativeSuffix = "";
/**
* Fields and an accessor method to match booleans
*/
private static volatile Pattern boolPattern;
private static final String BOOLEAN_PATTERN = "true|false";
private static Pattern boolPattern() {
Pattern bp = boolPattern;
if (bp == null)
boolPattern = bp = Pattern.compile(BOOLEAN_PATTERN,
Pattern.CASE_INSENSITIVE);
return bp;
}
/**
* Fields and methods to match bytes, shorts, ints, and longs
*/
private Pattern integerPattern;
private String digits = "0123456789abcdefghijklmnopqrstuvwxyz";
private String non0Digit = "[\\p{javaDigit}&&[^0]]";
private int SIMPLE_GROUP_INDEX = 5;
private String buildIntegerPatternString() {
String radixDigits = digits.substring(0, radix);
// \\p{javaDigit} is not guaranteed to be appropriate
// here but what can we do? The final authority will be
// whatever parse method is invoked, so ultimately the
// Scanner will do the right thing
String digit = "((?i)["+radixDigits+"]|\\p{javaDigit})";
String groupedNumeral = "("+non0Digit+digit+"?"+digit+"?("+
groupSeparator+digit+digit+digit+")+)";
// digit++ is the possessive form which is necessary for reducing
// backtracking that would otherwise cause unacceptable performance
String numeral = "(("+ digit+"++)|"+groupedNumeral+")";
String javaStyleInteger = "([-+]?(" + numeral + "))";
String negativeInteger = negativePrefix + numeral + negativeSuffix;
String positiveInteger = positivePrefix + numeral + positiveSuffix;
return "("+ javaStyleInteger + ")|(" +
positiveInteger + ")|(" +
negativeInteger + ")";
}
private Pattern integerPattern() {
if (integerPattern == null) {
integerPattern = patternCache.forName(buildIntegerPatternString());
}
return integerPattern;
}
/**
* Fields and an accessor method to match line separators
*/
private static volatile Pattern separatorPattern;
private static volatile Pattern linePattern;
private static final String LINE_SEPARATOR_PATTERN =
"\r\n|[\n\r\u2028\u2029\u0085]";
private static final String LINE_PATTERN = ".*("+LINE_SEPARATOR_PATTERN+")|.+$";
private static Pattern separatorPattern() {
Pattern sp = separatorPattern;
if (sp == null)
separatorPattern = sp = Pattern.compile(LINE_SEPARATOR_PATTERN);
return sp;
}
private static Pattern linePattern() {
Pattern lp = linePattern;
if (lp == null)
linePattern = lp = Pattern.compile(LINE_PATTERN);
return lp;
}
/**
* Fields and methods to match floats and doubles
*/
private Pattern floatPattern;
private Pattern decimalPattern;
private void buildFloatAndDecimalPattern() {
// \\p{javaDigit} may not be perfect, see above
String digit = "([0-9]|(\\p{javaDigit}))";
String exponent = "([eE][+-]?"+digit+"+)?";
String groupedNumeral = "("+non0Digit+digit+"?"+digit+"?("+
groupSeparator+digit+digit+digit+")+)";
// Once again digit++ is used for performance, as above
String numeral = "(("+digit+"++)|"+groupedNumeral+")";
String decimalNumeral = "("+numeral+"|"+numeral +
decimalSeparator + digit + "*+|"+ decimalSeparator +
digit + "++)";
String nonNumber = "(NaN|"+nanString+"|Infinity|"+
infinityString+")";
String positiveFloat = "(" + positivePrefix + decimalNumeral +
positiveSuffix + exponent + ")";
String negativeFloat = "(" + negativePrefix + decimalNumeral +
negativeSuffix + exponent + ")";
String decimal = "(([-+]?" + decimalNumeral + exponent + ")|"+
positiveFloat + "|" + negativeFloat + ")";
String hexFloat =
"[-+]?0[xX][0-9a-fA-F]*\\.[0-9a-fA-F]+([pP][-+]?[0-9]+)?";
String positiveNonNumber = "(" + positivePrefix + nonNumber +
positiveSuffix + ")";
String negativeNonNumber = "(" + negativePrefix + nonNumber +
negativeSuffix + ")";
String signedNonNumber = "(([-+]?"+nonNumber+")|" +
positiveNonNumber + "|" +
negativeNonNumber + ")";
floatPattern = Pattern.compile(decimal + "|" + hexFloat + "|" +
signedNonNumber);
decimalPattern = Pattern.compile(decimal);
}
private Pattern floatPattern() {
if (floatPattern == null) {
buildFloatAndDecimalPattern();
}
return floatPattern;
}
private Pattern decimalPattern() {
if (decimalPattern == null) {
buildFloatAndDecimalPattern();
}
return decimalPattern;
}
// Constructors
/**
* Constructs a <code>Scanner</code> that returns values scanned
* from the specified source delimited by the specified pattern.
*
* @param source A character source implementing the Readable interface
* @param pattern A delimiting pattern
*/
private Scanner(Readable source, Pattern pattern) {
assert source != null : "source should not be null";
assert pattern != null : "pattern should not be null";
this.source = source;
delimPattern = pattern;
buf = CharBuffer.allocate(BUFFER_SIZE);
buf.limit(0);
matcher = delimPattern.matcher(buf);
matcher.useTransparentBounds(true);
matcher.useAnchoringBounds(false);
useLocale(Locale.getDefault(Locale.Category.FORMAT));
}
/**
* Constructs a new <code>Scanner</code> that produces values scanned
* from the specified source.
*
* @param source A character source implementing the {@link Readable}
* interface
*/
public Scanner(Readable source) {
this(Objects.requireNonNull(source, "source"), WHITESPACE_PATTERN);
}
/**
* Constructs a new <code>Scanner</code> that produces values scanned
* from the specified input stream. Bytes from the stream are converted
* into characters using the underlying platform's
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
*
* @param source An input stream to be scanned
*/
public Scanner(InputStream source) {
this(new InputStreamReader(source), WHITESPACE_PATTERN);
}
/**
* Constructs a new <code>Scanner</code> that produces values scanned
* from the specified input stream. Bytes from the stream are converted
* into characters using the specified charset.
*
* @param source An input stream to be scanned
* @param charsetName The encoding type used to convert bytes from the
* stream into characters to be scanned
* @throws IllegalArgumentException if the specified character set
* does not exist
*/
public Scanner(InputStream source, String charsetName) {
this(makeReadable(Objects.requireNonNull(source, "source"), toCharset(charsetName)),
WHITESPACE_PATTERN);
}
/**
* Returns a charset object for the given charset name.
* @throws NullPointerException is csn is null
* @throws IllegalArgumentException if the charset is not supported
*/
private static Charset toCharset(String csn) {
Objects.requireNonNull(csn, "charsetName");
try {
return Charset.forName(csn);
} catch (IllegalCharsetNameException|UnsupportedCharsetException e) {
// IllegalArgumentException should be thrown
throw new IllegalArgumentException(e);
}
}
private static Readable makeReadable(InputStream source, Charset charset) {
return new InputStreamReader(source, charset);
}
/**
* Constructs a new <code>Scanner</code> that produces values scanned
* from the specified file. Bytes from the file are converted into
* characters using the underlying platform's
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
*
* @param source A file to be scanned
* @throws FileNotFoundException if source is not found
*/
public Scanner(File source) throws FileNotFoundException {
this((ReadableByteChannel)(new FileInputStream(source).getChannel()));
}
/**
* Constructs a new <code>Scanner</code> that produces values scanned
* from the specified file. Bytes from the file are converted into
* characters using the specified charset.
*
* @param source A file to be scanned
* @param charsetName The encoding type used to convert bytes from the file
* into characters to be scanned
* @throws FileNotFoundException if source is not found
* @throws IllegalArgumentException if the specified encoding is
* not found
*/
public Scanner(File source, String charsetName)
throws FileNotFoundException
{
this(Objects.requireNonNull(source), toDecoder(charsetName));
}
private Scanner(File source, CharsetDecoder dec)
throws FileNotFoundException
{
this(makeReadable((ReadableByteChannel)(new FileInputStream(source).getChannel()), dec));
}
private static CharsetDecoder toDecoder(String charsetName) {
Objects.requireNonNull(charsetName, "charsetName");
try {
return Charset.forName(charsetName).newDecoder();
} catch (IllegalCharsetNameException|UnsupportedCharsetException unused) {
throw new IllegalArgumentException(charsetName);
}
}
private static Readable makeReadable(ReadableByteChannel source,
CharsetDecoder dec) {
return Channels.newReader(source, dec, -1);
}
/**
* Constructs a new <code>Scanner</code> that produces values scanned
* from the specified file. Bytes from the file are converted into
* characters using the underlying platform's
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
*
* @param source
* the path to the file to be scanned
* @throws IOException
* if an I/O error occurs opening source
*
* @since 1.7
*/
public Scanner(Path source)
throws IOException
{
this(Files.newInputStream(source));
}
/**
* Constructs a new <code>Scanner</code> that produces values scanned
* from the specified file. Bytes from the file are converted into
* characters using the specified charset.
*
* @param source
* the path to the file to be scanned
* @param charsetName
* The encoding type used to convert bytes from the file
* into characters to be scanned
* @throws IOException
* if an I/O error occurs opening source
* @throws IllegalArgumentException
* if the specified encoding is not found
* @since 1.7
*/
public Scanner(Path source, String charsetName) throws IOException {
this(Objects.requireNonNull(source), toCharset(charsetName));
}
private Scanner(Path source, Charset charset) throws IOException {
this(makeReadable(Files.newInputStream(source), charset));
}
/**
* Constructs a new <code>Scanner</code> that produces values scanned
* from the specified string.
*
* @param source A string to scan
*/
public Scanner(String source) {
this(new StringReader(source), WHITESPACE_PATTERN);
}
/**
* Constructs a new <code>Scanner</code> that produces values scanned
* from the specified channel. Bytes from the source are converted into
* characters using the underlying platform's
* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
*
* @param source A channel to scan
*/
public Scanner(ReadableByteChannel source) {
this(makeReadable(Objects.requireNonNull(source, "source")),
WHITESPACE_PATTERN);
}
private static Readable makeReadable(ReadableByteChannel source) {
return makeReadable(source, Charset.defaultCharset().newDecoder());
}
/**
* Constructs a new <code>Scanner</code> that produces values scanned
* from the specified channel. Bytes from the source are converted into
* characters using the specified charset.
*
* @param source A channel to scan
* @param charsetName The encoding type used to convert bytes from the
* channel into characters to be scanned
* @throws IllegalArgumentException if the specified character set
* does not exist
*/
public Scanner(ReadableByteChannel source, String charsetName) {
this(makeReadable(Objects.requireNonNull(source, "source"), toDecoder(charsetName)),
WHITESPACE_PATTERN);
}
// Private primitives used to support scanning
private void saveState() {
savedScannerPosition = position;
}
private void revertState() {
this.position = savedScannerPosition;
savedScannerPosition = -1;
skipped = false;
}
private boolean revertState(boolean b) {
this.position = savedScannerPosition;
savedScannerPosition = -1;
skipped = false;
return b;
}
private void cacheResult() {
hasNextResult = matcher.group();
hasNextPosition = matcher.end();
hasNextPattern = matcher.pattern();
}
private void cacheResult(String result) {
hasNextResult = result;
hasNextPosition = matcher.end();
hasNextPattern = matcher.pattern();
}
// Clears both regular cache and type cache
private void clearCaches() {
hasNextPattern = null;
typeCache = null;
}
// Also clears both the regular cache and the type cache
private String getCachedResult() {
position = hasNextPosition;
hasNextPattern = null;
typeCache = null;
return hasNextResult;
}
// Also clears both the regular cache and the type cache
private void useTypeCache() {
if (closed)
throw new IllegalStateException("Scanner closed");
position = hasNextPosition;
hasNextPattern = null;
typeCache = null;
}
// Tries to read more input. May block.
private void readInput() {
if (buf.limit() == buf.capacity())
makeSpace();
// Prepare to receive data
int p = buf.position();
buf.position(buf.limit());
buf.limit(buf.capacity());
int n = 0;
try {
n = source.read(buf);
} catch (IOException ioe) {
lastException = ioe;
n = -1;
}
if (n == -1) {
sourceClosed = true;
needInput = false;
}
if (n > 0)
needInput = false;
// Restore current position and limit for reading
buf.limit(buf.position());
buf.position(p);
}
// After this method is called there will either be an exception
// or else there will be space in the buffer
private boolean makeSpace() {
clearCaches();
int offset = savedScannerPosition == -1 ?
position : savedScannerPosition;
buf.position(offset);
// Gain space by compacting buffer
if (offset > 0) {
buf.compact();
translateSavedIndexes(offset);
position -= offset;
buf.flip();
return true;
}
// Gain space by growing buffer
int newSize = buf.capacity() * 2;
CharBuffer newBuf = CharBuffer.allocate(newSize);
newBuf.put(buf);
newBuf.flip();
translateSavedIndexes(offset);
position -= offset;
buf = newBuf;
matcher.reset(buf);
return true;
}
// When a buffer compaction/reallocation occurs the saved indexes must
// be modified appropriately
private void translateSavedIndexes(int offset) {
if (savedScannerPosition != -1)
savedScannerPosition -= offset;
}
// If we are at the end of input then NoSuchElement;
// If there is still input left then InputMismatch
private void throwFor() {
skipped = false;
if ((sourceClosed) && (position == buf.limit()))
throw new NoSuchElementException();
else
throw new InputMismatchException();
}
// Returns true if a complete token or partial token is in the buffer.
// It is not necessary to find a complete token since a partial token
// means that there will be another token with or without more input.
private boolean hasTokenInBuffer() {
matchValid = false;
matcher.usePattern(delimPattern);
matcher.region(position, buf.limit());
// Skip delims first
if (matcher.lookingAt())
position = matcher.end();
// If we are sitting at the end, no more tokens in buffer
if (position == buf.limit())
return false;
return true;
}
/*
* Returns a "complete token" that matches the specified pattern
*
* A token is complete if surrounded by delims; a partial token
* is prefixed by delims but not postfixed by them
*
* The position is advanced to the end of that complete token
*
* Pattern == null means accept any token at all
*
* Triple return:
* 1. valid string means it was found
* 2. null with needInput=false means we won't ever find it
* 3. null with needInput=true means try again after readInput
*/
private String getCompleteTokenInBuffer(Pattern pattern) {
matchValid = false;
// Skip delims first
matcher.usePattern(delimPattern);
if (!skipped) { // Enforcing only one skip of leading delims
matcher.region(position, buf.limit());
if (matcher.lookingAt()) {
// If more input could extend the delimiters then we must wait
// for more input
if (matcher.hitEnd() && !sourceClosed) {
needInput = true;
return null;
}
// The delims were whole and the matcher should skip them
skipped = true;
position = matcher.end();
}
}
// If we are sitting at the end, no more tokens in buffer
if (position == buf.limit()) {
if (sourceClosed)
return null;
needInput = true;
return null;
}
// Must look for next delims. Simply attempting to match the
// pattern at this point may find a match but it might not be
// the first longest match because of missing input, or it might
// match a partial token instead of the whole thing.
// Then look for next delims
matcher.region(position, buf.limit());
boolean foundNextDelim = matcher.find();
if (foundNextDelim && (matcher.end() == position)) {
// Zero length delimiter match; we should find the next one
// using the automatic advance past a zero length match;
// Otherwise we have just found the same one we just skipped
foundNextDelim = matcher.find();
}
if (foundNextDelim) {
// In the rare case that more input could cause the match
// to be lost and there is more input coming we must wait
// for more input. Note that hitting the end is okay as long
// as the match cannot go away. It is the beginning of the
// next delims we want to be sure about, we don't care if
// they potentially extend further.
if (matcher.requireEnd() && !sourceClosed) {
needInput = true;
return null;
}
int tokenEnd = matcher.start();
// There is a complete token.
if (pattern == null) {
// Must continue with match to provide valid MatchResult
pattern = FIND_ANY_PATTERN;
}
// Attempt to match against the desired pattern
matcher.usePattern(pattern);
matcher.region(position, tokenEnd);
if (matcher.matches()) {
String s = matcher.group();
position = matcher.end();
return s;
} else { // Complete token but it does not match
return null;
}
}
// If we can't find the next delims but no more input is coming,
// then we can treat the remainder as a whole token
if (sourceClosed) {
if (pattern == null) {
// Must continue with match to provide valid MatchResult
pattern = FIND_ANY_PATTERN;
}
// Last token; Match the pattern here or throw
matcher.usePattern(pattern);
matcher.region(position, buf.limit());
if (matcher.matches()) {
String s = matcher.group();
position = matcher.end();
return s;
}
// Last piece does not match
return null;
}
// There is a partial token in the buffer; must read more
// to complete it
needInput = true;
return null;
}
// Finds the specified pattern in the buffer up to horizon.
// Returns a match for the specified input pattern.
private String findPatternInBuffer(Pattern pattern, int horizon) {
matchValid = false;