~ubuntu-branches/ubuntu/vivid/tomcat7/vivid-proposed

« back to all changes in this revision

Viewing changes to java/org/apache/coyote/http11/upgrade/NioServletInputStream.java

  • Committer: Package Import Robot
  • Author(s): tony mancill, Gianfranco Costamagna, tony mancill
  • Date: 2013-12-24 16:46:34 UTC
  • mfrom: (1.1.14)
  • Revision ID: package-import@ubuntu.com-20131224164634-2racvo4wty70t0za
Tags: 7.0.47-1
[ Gianfranco Costamagna ]
* Team upload.
* New upstream release, patch refresh.
* Renamed patch fix-manager-webapp.path
  to fix-manager-webapp.patch (extension typo).
* Refresh patches for upstream release.
* Removed -Djava.net.preferIPv4Stack=true
  from init script (lp: #1088681),
  thanks Hendrik Haddorp.
* Added webapp manager path patch (lp: #1128067)
  thanks TJ.

[ tony mancill ]
* Bump Standards-Version to 3.9.5.
* Change copyright year in javadocs to 2013.
* Add patch to include the distribution name in error pages.
  (Closes: #729840)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
8
 *
 
9
 *      http://www.apache.org/licenses/LICENSE-2.0
 
10
 *
 
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.
 
16
 */
 
17
package org.apache.coyote.http11.upgrade;
 
18
 
 
19
import java.io.EOFException;
 
20
import java.io.IOException;
 
21
import java.nio.ByteBuffer;
 
22
import java.nio.channels.Selector;
 
23
 
 
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;
 
28
 
 
29
public class NioServletInputStream extends AbstractServletInputStream {
 
30
 
 
31
    private final NioChannel channel;
 
32
    private final NioSelectorPool pool;
 
33
 
 
34
    public NioServletInputStream(SocketWrapper<NioChannel> wrapper,
 
35
            NioSelectorPool pool) {
 
36
        this.channel = wrapper.getSocket();
 
37
        this.pool = pool;
 
38
    }
 
39
 
 
40
    @Override
 
41
    protected boolean doIsReady() throws IOException {
 
42
        ByteBuffer readBuffer = channel.getBufHandler().getReadBuffer();
 
43
 
 
44
        if (readBuffer.remaining() > 0) {
 
45
            return true;
 
46
        }
 
47
 
 
48
        readBuffer.clear();
 
49
        fillReadBuffer(false);
 
50
 
 
51
        boolean isReady = readBuffer.position() > 0;
 
52
        readBuffer.flip();
 
53
        return isReady;
 
54
    }
 
55
 
 
56
    @Override
 
57
    protected int doRead(boolean block, byte[] b, int off, int len)
 
58
            throws IOException {
 
59
 
 
60
        ByteBuffer readBuffer = channel.getBufHandler().getReadBuffer();
 
61
        int remaining = readBuffer.remaining();
 
62
 
 
63
        // Is there enough data in the read buffer to satisfy this request?
 
64
        if (remaining >= len) {
 
65
            readBuffer.get(b, off, len);
 
66
            return len;
 
67
        }
 
68
 
 
69
        // Copy what data there is in the read buffer to the byte array
 
70
        int leftToWrite = len;
 
71
        int newOffset = off;
 
72
        if (remaining > 0) {
 
73
            readBuffer.get(b, off, remaining);
 
74
            leftToWrite -= remaining;
 
75
            newOffset += remaining;
 
76
        }
 
77
 
 
78
        // Fill the read buffer as best we can
 
79
        readBuffer.clear();
 
80
        int nRead = fillReadBuffer(block);
 
81
 
 
82
        // Full as much of the remaining byte array as possible with the data
 
83
        // that was just read
 
84
        if (nRead > 0) {
 
85
            readBuffer.flip();
 
86
            if (nRead > leftToWrite) {
 
87
                readBuffer.get(b, newOffset, leftToWrite);
 
88
                leftToWrite = 0;
 
89
            } else {
 
90
                readBuffer.get(b, newOffset, nRead);
 
91
                leftToWrite -= nRead;
 
92
            }
 
93
        } else if (nRead == 0) {
 
94
            readBuffer.flip();
 
95
        } else if (nRead == -1) {
 
96
            // TODO i18n
 
97
            throw new EOFException();
 
98
        }
 
99
 
 
100
        return len - leftToWrite;
 
101
    }
 
102
 
 
103
 
 
104
 
 
105
    @Override
 
106
    protected void doClose() throws IOException {
 
107
        channel.close();
 
108
    }
 
109
 
 
110
 
 
111
    private int fillReadBuffer(boolean block) throws IOException {
 
112
        int nRead;
 
113
        if (block) {
 
114
            Selector selector = null;
 
115
            try {
 
116
                selector = pool.get();
 
117
            } catch ( IOException x ) {
 
118
                // Ignore
 
119
            }
 
120
            try {
 
121
                NioEndpoint.KeyAttachment att =
 
122
                        (NioEndpoint.KeyAttachment) channel.getAttachment(false);
 
123
                if (att == null) {
 
124
                    throw new IOException("Key must be cancelled.");
 
125
                }
 
126
                nRead = pool.read(channel.getBufHandler().getReadBuffer(),
 
127
                        channel, selector, att.getTimeout());
 
128
            } catch (EOFException eof) {
 
129
                nRead = -1;
 
130
            } finally {
 
131
                if (selector != null) {
 
132
                    pool.put(selector);
 
133
                }
 
134
            }
 
135
        } else {
 
136
            nRead = channel.read(channel.getBufHandler().getReadBuffer());
 
137
        }
 
138
        return nRead;
 
139
    }
 
140
}