提交 fabbaec9 编写于 作者: S sherman

6725399: (ch) Channels.newInputStream should check for null

Summary: update to check null arg for all Channels methods
Reviewed-by: alanb
上级 51a75f90
......@@ -65,6 +65,10 @@ public final class Channels {
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 @@ public final class Channels {
* @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 @@ public final class Channels {
* @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 @@ public final class Channels {
* @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 @@ public final class Channels {
* @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 @@ public final class Channels {
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 final class Channels {
public static Reader newReader(ReadableByteChannel ch,
String csName)
{
checkNotNull(csName, "csName");
return newReader(ch, Charset.forName(csName).newDecoder(), -1);
}
......@@ -425,8 +429,8 @@ public final class Channels {
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 final class Channels {
public static Writer newWriter(WritableByteChannel ch,
String csName)
{
checkNotNull(csName, "csName");
return newWriter(ch, Charset.forName(csName).newEncoder(), -1);
}
......
......@@ -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 @@ public class Basic {
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 {
ReadableByteChannel channel = Channels.newChannel((InputStream)null);
Channels.newInputStream((ReadableByteChannel)null);
failNpeExpected();
} catch (NullPointerException npe) {}
throw new RuntimeException("Did not get the expected NullPointerException.");
} catch (NullPointerException ne) {
// OK. As expected.
}
try {
Channels.newOutputStream((WritableByteChannel)null);
failNpeExpected();
} catch (NullPointerException npe) {}
try {
ReadableByteChannel channel = Channels.newChannel((InputStream)null);
failNpeExpected();
} catch (NullPointerException ne) {} // OK. As expected.
try {
WritableByteChannel channel = Channels.newChannel((OutputStream)null);
failNpeExpected();
} 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) {}
throw new RuntimeException("Did not get the expected NullPointerException.");
} catch (NullPointerException ne) {
// OK. As expected.
}
try {
blah = File.createTempFile("blah", null);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册