2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.coyote.http11.upgrade;
19
import java.io.EOFException;
20
import java.io.IOException;
21
import java.nio.ByteBuffer;
22
import java.nio.channels.Selector;
24
import org.apache.tomcat.util.net.NioChannel;
25
import org.apache.tomcat.util.net.NioEndpoint;
26
import org.apache.tomcat.util.net.NioSelectorPool;
27
import org.apache.tomcat.util.net.SocketWrapper;
29
public class NioServletInputStream extends AbstractServletInputStream {
31
private final NioChannel channel;
32
private final NioSelectorPool pool;
34
public NioServletInputStream(SocketWrapper<NioChannel> wrapper,
35
NioSelectorPool pool) {
36
this.channel = wrapper.getSocket();
41
protected boolean doIsReady() throws IOException {
42
ByteBuffer readBuffer = channel.getBufHandler().getReadBuffer();
44
if (readBuffer.remaining() > 0) {
49
fillReadBuffer(false);
51
boolean isReady = readBuffer.position() > 0;
57
protected int doRead(boolean block, byte[] b, int off, int len)
60
ByteBuffer readBuffer = channel.getBufHandler().getReadBuffer();
61
int remaining = readBuffer.remaining();
63
// Is there enough data in the read buffer to satisfy this request?
64
if (remaining >= len) {
65
readBuffer.get(b, off, len);
69
// Copy what data there is in the read buffer to the byte array
70
int leftToWrite = len;
73
readBuffer.get(b, off, remaining);
74
leftToWrite -= remaining;
75
newOffset += remaining;
78
// Fill the read buffer as best we can
80
int nRead = fillReadBuffer(block);
82
// Full as much of the remaining byte array as possible with the data
86
if (nRead > leftToWrite) {
87
readBuffer.get(b, newOffset, leftToWrite);
90
readBuffer.get(b, newOffset, nRead);
93
} else if (nRead == 0) {
95
} else if (nRead == -1) {
97
throw new EOFException();
100
return len - leftToWrite;
106
protected void doClose() throws IOException {
111
private int fillReadBuffer(boolean block) throws IOException {
114
Selector selector = null;
116
selector = pool.get();
117
} catch ( IOException x ) {
121
NioEndpoint.KeyAttachment att =
122
(NioEndpoint.KeyAttachment) channel.getAttachment(false);
124
throw new IOException("Key must be cancelled.");
126
nRead = pool.read(channel.getBufHandler().getReadBuffer(),
127
channel, selector, att.getTimeout());
128
} catch (EOFException eof) {
131
if (selector != null) {
136
nRead = channel.read(channel.getBufHandler().getReadBuffer());