--- old/src/share/classes/java/util/Scanner.java Thu Dec 23 15:18:36 2010
+++ new/src/share/classes/java/util/Scanner.java Thu Dec 23 15:18:35 2010
@@ -571,10 +571,8 @@
* @return A scanner with the specified source and pattern
*/
private Scanner(Readable source, Pattern pattern) {
- if (source == null)
- throw new NullPointerException("source");
- if (pattern == null)
- throw new NullPointerException("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);
@@ -593,7 +591,7 @@
* interface
*/
public Scanner(Readable source) {
- this(source, WHITESPACE_PATTERN);
+ this(Objects.nonNull(source, "source"), WHITESPACE_PATTERN);
}
/**
@@ -620,25 +618,30 @@
* does not exist
*/
public Scanner(InputStream source, String charsetName) {
- this(makeReadable(source, charsetName), WHITESPACE_PATTERN);
+ this(makeReadable(Objects.nonNull(source, "source"), toCharset(charsetName)),
+ WHITESPACE_PATTERN);
}
- private static Readable makeReadable(InputStream source,
- String charsetName)
- {
- if (source == null)
- throw new NullPointerException("source");
- InputStreamReader isr = null;
+ /**
+ * 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.nonNull(csn, "charsetName");
try {
- isr = new InputStreamReader(source, charsetName);
- } catch (UnsupportedEncodingException uee) {
- IllegalArgumentException iae = new IllegalArgumentException();
- iae.initCause(uee);
- throw iae;
+ return Charset.forName(csn);
+ } catch (IllegalCharsetNameException unused) {
+ /* swallow this exception since IllegalArgumentException
+ * will be thrown */
}
- return isr;
+ throw new IllegalArgumentException(new UnsupportedEncodingException(csn));
}
+ private static Readable makeReadable(InputStream source, Charset charset) {
+ return new InputStreamReader(source, charset);
+ }
+
/**
* Constructs a new Scanner
that produces values scanned
* from the specified file. Bytes from the file are converted into
@@ -648,9 +651,7 @@
* @param source A file to be scanned
* @throws FileNotFoundException if source is not found
*/
- public Scanner(File source)
- throws FileNotFoundException
- {
+ public Scanner(File source) throws FileNotFoundException {
this((ReadableByteChannel)(new FileInputStream(source).getChannel()));
}
@@ -669,10 +670,28 @@
public Scanner(File source, String charsetName)
throws FileNotFoundException
{
- this((ReadableByteChannel)(new FileInputStream(source).getChannel()),
- charsetName);
+ this(Objects.nonNull(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) {
+ try {
+ return Charset.forName(charsetName).newDecoder();
+ } catch (UnsupportedCharsetException unused) {
+ throw new IllegalArgumentException(charsetName);
+ }
+ }
+
+ private static Readable makeReadable(ReadableByteChannel source,
+ CharsetDecoder dec) {
+ return Channels.newReader(source, dec, -1);
+ }
+
/**
* Constructs a new Scanner
that produces values scanned
* from the specified file. Bytes from the file are converted into
@@ -708,12 +727,14 @@
* if the specified encoding is not found
* @since 1.7
*/
- public Scanner(FileRef source, String charsetName)
- throws IOException
- {
- this(source.newInputStream(), charsetName);
+ public Scanner(FileRef source, String charsetName) throws IOException {
+ this(Objects.nonNull(source), toCharset(charsetName));
}
+ private Scanner(FileRef source, Charset charset) throws IOException {
+ this(makeReadable(source.newInputStream(), charset));
+ }
+
/**
* Constructs a new Scanner
that produces values scanned
* from the specified string.
@@ -733,16 +754,12 @@
* @param source A channel to scan
*/
public Scanner(ReadableByteChannel source) {
- this(makeReadable(source), WHITESPACE_PATTERN);
+ this(makeReadable(Objects.nonNull(source, "source")),
+ WHITESPACE_PATTERN);
}
private static Readable makeReadable(ReadableByteChannel source) {
- if (source == null)
- throw new NullPointerException("source");
- String defaultCharsetName =
- java.nio.charset.Charset.defaultCharset().name();
- return Channels.newReader(source,
- java.nio.charset.Charset.defaultCharset().name());
+ return makeReadable(source, Charset.defaultCharset().newDecoder());
}
/**
@@ -757,19 +774,10 @@
* does not exist
*/
public Scanner(ReadableByteChannel source, String charsetName) {
- this(makeReadable(source, charsetName), WHITESPACE_PATTERN);
+ this(makeReadable(Objects.nonNull(source, "source"), toDecoder(charsetName)),
+ WHITESPACE_PATTERN);
}
- private static Readable makeReadable(ReadableByteChannel source,
- String charsetName)
- {
- if (source == null)
- throw new NullPointerException("source");
- if (!Charset.isSupported(charsetName))
- throw new IllegalArgumentException(charsetName);
- return Channels.newReader(source, charsetName);
- }
-
// Private primitives used to support scanning
private void saveState() {