6725399: (ch) Channels.newInputStream should check for null
Summary: update to check null arg for all Channels methods
Reviewed-by: alanb
--- a/jdk/src/share/classes/java/nio/channels/Channels.java Thu Nov 27 15:44:32 2008 +0100
+++ b/jdk/src/share/classes/java/nio/channels/Channels.java Sat Nov 29 20:55:29 2008 -0800
@@ -65,6 +65,10 @@
private Channels() { } // No instantiation
+ private static void checkNotNull(Object o, String name) {
+ if (o == null)
+ throw new NullPointerException("\"" + name + "\" is null!");
+ }
/**
* Write all remaining bytes in buffer to the given channel.
@@ -120,6 +124,7 @@
* @return A new input stream
*/
public static InputStream newInputStream(ReadableByteChannel ch) {
+ checkNotNull(ch, "ch");
return new sun.nio.ch.ChannelInputStream(ch);
}
@@ -138,6 +143,8 @@
* @return A new output stream
*/
public static OutputStream newOutputStream(final WritableByteChannel ch) {
+ checkNotNull(ch, "ch");
+
return new OutputStream() {
private ByteBuffer bb = null;
@@ -193,9 +200,7 @@
* @return A new readable byte channel
*/
public static ReadableByteChannel newChannel(final InputStream in) {
- if (in == null) {
- throw new NullPointerException();
- }
+ checkNotNull(in, "in");
if (in instanceof FileInputStream &&
FileInputStream.class.equals(in.getClass())) {
@@ -270,9 +275,7 @@
* @return A new writable byte channel
*/
public static WritableByteChannel newChannel(final OutputStream out) {
- if (out == null) {
- throw new NullPointerException();
- }
+ checkNotNull(out, "out");
if (out instanceof FileOutputStream &&
FileOutputStream.class.equals(out.getClass())) {
@@ -357,8 +360,8 @@
CharsetDecoder dec,
int minBufferCap)
{
- dec.reset();
- return StreamDecoder.forDecoder(ch, dec, minBufferCap);
+ checkNotNull(ch, "ch");
+ return StreamDecoder.forDecoder(ch, dec.reset(), minBufferCap);
}
/**
@@ -393,6 +396,7 @@
public static Reader newReader(ReadableByteChannel ch,
String csName)
{
+ checkNotNull(csName, "csName");
return newReader(ch, Charset.forName(csName).newDecoder(), -1);
}
@@ -425,8 +429,8 @@
final CharsetEncoder enc,
final int minBufferCap)
{
- enc.reset();
- return StreamEncoder.forEncoder(ch, enc, minBufferCap);
+ checkNotNull(ch, "ch");
+ return StreamEncoder.forEncoder(ch, enc.reset(), minBufferCap);
}
/**
@@ -461,6 +465,7 @@
public static Writer newWriter(WritableByteChannel ch,
String csName)
{
+ checkNotNull(csName, "csName");
return newWriter(ch, Charset.forName(csName).newEncoder(), -1);
}
--- a/jdk/test/java/nio/channels/Channels/Basic.java Thu Nov 27 15:44:32 2008 +0100
+++ b/jdk/test/java/nio/channels/Channels/Basic.java Sat Nov 29 20:55:29 2008 -0800
@@ -22,12 +22,13 @@
*/
/* @test
- * @bug 4417152 4481572 6248930
+ * @bug 4417152 4481572 6248930 6725399
* @summary Test Channels basic functionality
*/
import java.io.*;
import java.nio.*;
+import java.nio.charset.*;
import java.nio.channels.*;
@@ -50,22 +51,106 @@
test();
}
+ static void failNpeExpected() {
+ throw new RuntimeException("Did not get the expected NullPointerException.");
+ }
+
private static void test() throws Exception {
+ //Test if methods of Channels throw NPE with null argument(s)
+ try {
+ Channels.newInputStream((ReadableByteChannel)null);
+ failNpeExpected();
+ } catch (NullPointerException npe) {}
+
+ try {
+ Channels.newOutputStream((WritableByteChannel)null);
+ failNpeExpected();
+ } catch (NullPointerException npe) {}
+
try {
ReadableByteChannel channel = Channels.newChannel((InputStream)null);
-
- throw new RuntimeException("Did not get the expected NullPointerException.");
- } catch (NullPointerException ne) {
- // OK. As expected.
- }
+ failNpeExpected();
+ } catch (NullPointerException ne) {} // OK. As expected.
try {
WritableByteChannel channel = Channels.newChannel((OutputStream)null);
+ failNpeExpected();
+ } catch (NullPointerException ne) {} // OK. As expected.
- throw new RuntimeException("Did not get the expected NullPointerException.");
- } catch (NullPointerException ne) {
- // OK. As expected.
- }
+ WritableByteChannel wbc = new WritableByteChannel() {
+ public int write(ByteBuffer src) { return 0; }
+ public void close() throws IOException { }
+ public boolean isOpen() { return true; }
+ };
+
+ ReadableByteChannel rbc = new ReadableByteChannel() {
+ public int read(ByteBuffer dst) { return 0; }
+ public void close() {}
+ public boolean isOpen() { return true; }
+ };
+
+ try {
+ Channels.newReader((ReadableByteChannel)null,
+ Charset.defaultCharset().newDecoder(),
+ -1);
+ failNpeExpected();
+ } catch (NullPointerException npe) {}
+
+ try {
+ Channels.newReader(rbc, (CharsetDecoder)null, -1);
+ failNpeExpected();
+ } catch (NullPointerException npe) {}
+
+ try {
+ Channels.newReader((ReadableByteChannel)null,
+ Charset.defaultCharset().name());
+ failNpeExpected();
+ } catch (NullPointerException npe) {}
+
+ try {
+ Channels.newReader(rbc, null);
+ failNpeExpected();
+ } catch (NullPointerException npe) {}
+
+
+ try {
+ Channels.newReader(null, null);
+ failNpeExpected();
+ } catch (NullPointerException npe) {}
+
+ try {
+ Channels.newWriter((WritableByteChannel)null,
+ Charset.defaultCharset().newEncoder(),
+ -1);
+ failNpeExpected();
+ } catch (NullPointerException npe) {}
+
+ try {
+ Channels.newWriter(null, null, -1);
+ failNpeExpected();
+ } catch (NullPointerException npe) {}
+
+ try {
+ Channels.newWriter(wbc, null, -1);
+ failNpeExpected();
+ } catch (NullPointerException npe) {}
+
+ try {
+ Channels.newWriter((WritableByteChannel)null,
+ Charset.defaultCharset().name());
+ failNpeExpected();
+ } catch (NullPointerException npe) {}
+
+ try {
+ Channels.newWriter(wbc, null);
+ failNpeExpected();
+ } catch (NullPointerException npe) {}
+
+ try {
+ Channels.newWriter(null, null);
+ failNpeExpected();
+ } catch (NullPointerException npe) {}
+
try {
blah = File.createTempFile("blah", null);