Skip to content

Commit

Permalink
The closed channels need to be immutable so override a few more methods
Browse files Browse the repository at this point in the history
Avoiding a NPE in NioBlockignSelector is not possible however and
getting there is wasteful, so filter out read as well. In theory ==
CLOSED_NIO_CHANNEL would be enough but use instanceof for now.
  • Loading branch information
rmaucher committed May 29, 2019
1 parent 7046644 commit 339b40b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
5 changes: 4 additions & 1 deletion java/org/apache/tomcat/util/net/Nio2Channel.java
Expand Up @@ -251,7 +251,7 @@ public Integer get(long timeout, TimeUnit unit)
static final Nio2Channel CLOSED_NIO2_CHANNEL = new ClosedNio2Channel();
public static class ClosedNio2Channel extends Nio2Channel {
public ClosedNio2Channel() {
super(null);
super(SocketBufferHandler.EMPTY);
}
@Override
public void close() throws IOException {
Expand All @@ -267,6 +267,9 @@ public void reset(AsynchronousSocketChannel channel, SocketWrapperBase<Nio2Chann
public void free() {
}
@Override
public void setAppReadBufHandler(ApplicationBufferHandler handler) {
}
@Override
public Future<Integer> read(ByteBuffer dst) {
return DONE_INT;
}
Expand Down
13 changes: 11 additions & 2 deletions java/org/apache/tomcat/util/net/NioChannel.java
Expand Up @@ -246,7 +246,6 @@ protected void checkInterruptStatus() throws IOException {
}
}


private ApplicationBufferHandler appReadBufHandler;
public void setAppReadBufHandler(ApplicationBufferHandler handler) {
this.appReadBufHandler = handler;
Expand All @@ -258,7 +257,7 @@ protected ApplicationBufferHandler getAppReadBufHandler() {
static final NioChannel CLOSED_NIO_CHANNEL = new ClosedNioChannel();
public static class ClosedNioChannel extends NioChannel {
public ClosedNioChannel() {
super(null, null);
super(null, SocketBufferHandler.EMPTY);
}
@Override
public void close() throws IOException {
Expand All @@ -274,6 +273,15 @@ public void reset() throws IOException {
public void free() {
}
@Override
void setSocketWrapper(NioSocketWrapper socketWrapper) {
}
@Override
public void setIOChannel(SocketChannel sc) {
}
@Override
public void setAppReadBufHandler(ApplicationBufferHandler handler) {
}
@Override
public int read(ByteBuffer dst) throws IOException {
return -1;
}
Expand All @@ -297,4 +305,5 @@ public String toString() {
return "Closed NioChannel";
}
}

}
9 changes: 6 additions & 3 deletions java/org/apache/tomcat/util/net/NioEndpoint.java
Expand Up @@ -1215,7 +1215,10 @@ private int fillReadBuffer(boolean block) throws IOException {

private int fillReadBuffer(boolean block, ByteBuffer to) throws IOException {
int nRead;
NioChannel channel = getSocket();
NioChannel socket = getSocket();
if (socket instanceof ClosedNioChannel) {
throw new ClosedChannelException();
}
if (block) {
Selector selector = null;
try {
Expand All @@ -1224,14 +1227,14 @@ private int fillReadBuffer(boolean block, ByteBuffer to) throws IOException {
// Ignore
}
try {
nRead = pool.read(to, channel, selector, getReadTimeout());
nRead = pool.read(to, socket, selector, getReadTimeout());
} finally {
if (selector != null) {
pool.put(selector);
}
}
} else {
nRead = channel.read(to);
nRead = socket.read(to);
if (nRead == -1) {
throw new EOFException();
}
Expand Down

0 comments on commit 339b40b

Please sign in to comment.