Fix NPE in OioByteStreamChannel

- Do not assign null to 'is' and 'os' but assign an alternative stream implementation
This commit is contained in:
Trustin Lee 2013-07-04 12:04:28 +09:00
parent 79576d15c2
commit 998b408db3

View File

@ -23,6 +23,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.nio.channels.Channels; import java.nio.channels.Channels;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.NotYetConnectedException; import java.nio.channels.NotYetConnectedException;
import java.nio.channels.WritableByteChannel; import java.nio.channels.WritableByteChannel;
@ -31,6 +32,20 @@ import java.nio.channels.WritableByteChannel;
*/ */
public abstract class OioByteStreamChannel extends AbstractOioByteChannel { 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 InputStream is;
private OutputStream os; private OutputStream os;
private WritableByteChannel outChannel; private WritableByteChannel outChannel;
@ -68,7 +83,17 @@ public abstract class OioByteStreamChannel extends AbstractOioByteChannel {
@Override @Override
public boolean isActive() { 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 @Override
@ -122,29 +147,19 @@ public abstract class OioByteStreamChannel extends AbstractOioByteChannel {
@Override @Override
protected void doClose() throws Exception { 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 { try {
if (is != null) { if (is != null) {
is.close(); is.close();
} }
} catch (IOException e) { } finally {
ex = e;
}
try {
if (os != null) { if (os != null) {
os.close(); os.close();
} }
} catch (IOException e) {
ex = e;
}
is = null;
os = null;
if (ex != null) {
throw ex;
} }
} }
} }