From 998b408db3403e44566ab3f08dd381a7e03f7d1d Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Thu, 4 Jul 2013 12:04:28 +0900 Subject: [PATCH] Fix NPE in OioByteStreamChannel - Do not assign null to 'is' and 'os' but assign an alternative stream implementation --- .../channel/oio/OioByteStreamChannel.java | 47 ++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/transport/src/main/java/io/netty/channel/oio/OioByteStreamChannel.java b/transport/src/main/java/io/netty/channel/oio/OioByteStreamChannel.java index dd0d3ab294..3a12af25a4 100644 --- a/transport/src/main/java/io/netty/channel/oio/OioByteStreamChannel.java +++ b/transport/src/main/java/io/netty/channel/oio/OioByteStreamChannel.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.channels.Channels; +import java.nio.channels.ClosedChannelException; import java.nio.channels.NotYetConnectedException; import java.nio.channels.WritableByteChannel; @@ -31,6 +32,20 @@ import java.nio.channels.WritableByteChannel; */ public abstract class OioByteStreamChannel extends AbstractOioByteChannel { + private static final InputStream CLOSED_IN = new InputStream() { + @Override + public int read() { + return -1; + } + }; + + private static final OutputStream CLOSED_OUT = new OutputStream() { + @Override + public void write(int b) throws IOException { + throw new ClosedChannelException(); + } + }; + private InputStream is; private OutputStream os; private WritableByteChannel outChannel; @@ -68,7 +83,17 @@ public abstract class OioByteStreamChannel extends AbstractOioByteChannel { @Override public boolean isActive() { - return is != null && os != null; + InputStream is = this.is; + if (is == null || is == CLOSED_IN) { + return false; + } + + OutputStream os = this.os; + if (os == null || os == CLOSED_OUT) { + return false; + } + + return true; } @Override @@ -122,29 +147,19 @@ public abstract class OioByteStreamChannel extends AbstractOioByteChannel { @Override protected void doClose() throws Exception { - IOException ex = null; + InputStream is = this.is; + OutputStream os = this.os; + this.is = CLOSED_IN; + this.os = CLOSED_OUT; try { if (is != null) { is.close(); } - } catch (IOException e) { - ex = e; - } - - try { + } finally { if (os != null) { os.close(); } - } catch (IOException e) { - ex = e; - } - - is = null; - os = null; - - if (ex != null) { - throw ex; } } }