jdk/src/share/classes/sun/net/httpserver/Request.java
author asaha
Thu, 16 Apr 2009 21:08:04 -0700
changeset 2624 1ae5a9028dd4
parent 1639 a97859015238
parent 2612 d7fb0809c7e4
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1639
a97859015238 6785258: Update copyright year
xdono
parents: 1511
diff changeset
     2
 * Copyright 2005-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net.httpserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.channels.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.net.httpserver.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
class Request {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    final static int BUF_LEN = 2048;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    final static byte CR = 13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    final static byte LF = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private String startLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private SocketChannel chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private InputStream is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private OutputStream os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    Request (InputStream rawInputStream, OutputStream rawout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        this.chan = chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        is = rawInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        os = rawout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            startLine = readLine();
2612
d7fb0809c7e4 6630639: lightweight HttpServer leaks file descriptors on no-data connections
michaelm
parents: 2
diff changeset
    55
            if (startLine == null) {
d7fb0809c7e4 6630639: lightweight HttpServer leaks file descriptors on no-data connections
michaelm
parents: 2
diff changeset
    56
                return;
d7fb0809c7e4 6630639: lightweight HttpServer leaks file descriptors on no-data connections
michaelm
parents: 2
diff changeset
    57
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            /* skip blank lines */
1511
65ddd8f149f3 6756771: com.sun.net.httpserver.HttpServer should handle POSTs larger than 2Gig
chegar
parents: 2
diff changeset
    59
        } while (startLine == null ? false : startLine.equals (""));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    char[] buf = new char [BUF_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    int pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    StringBuffer lineBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public InputStream inputStream () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public OutputStream outputStream () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * read a line from the stream returning as a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * Not used for reading headers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public String readLine () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        boolean gotCR = false, gotLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        pos = 0; lineBuf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        while (!gotLF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            int c = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            if (gotCR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                if (c == LF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    gotLF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    gotCR = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    consume (CR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    consume (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                if (c == CR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    gotCR = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    consume (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        lineBuf.append (buf, 0, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return new String (lineBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private void consume (int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (pos == BUF_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            lineBuf.append (buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        buf[pos++] = (char)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * returns the request line (first line of a request)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public String requestLine () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return startLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    Headers hdrs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    Headers headers () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (hdrs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return hdrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        hdrs = new Headers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        char s[] = new char[10];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        int firstc = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        while (firstc != LF && firstc != CR && firstc >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            int keyend = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            boolean inKey = firstc > ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            s[len++] = (char) firstc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    parseloop:{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                while ((c = is.read()) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    switch (c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                      case ':':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                        if (inKey && len > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                            keyend = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        inKey = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                      case '\t':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        c = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                      case ' ':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        inKey = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                      case CR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                      case LF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                        firstc = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        if (c == CR && firstc == LF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                            firstc = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                            if (firstc == CR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                firstc = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        if (firstc == LF || firstc == CR || firstc > ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                            break parseloop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        /* continuation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                        c = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    if (len >= s.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                        char ns[] = new char[s.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                        System.arraycopy(s, 0, ns, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                        s = ns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    s[len++] = (char) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                firstc = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            while (len > 0 && s[len - 1] <= ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                len--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            String k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            if (keyend <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                k = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                keyend = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                k = String.copyValueOf(s, 0, keyend);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                if (keyend < len && s[keyend] == ':')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    keyend++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                while (keyend < len && s[keyend] <= ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    keyend++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            String v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            if (keyend >= len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                v = new String();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                v = String.copyValueOf(s, keyend, len - keyend);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            hdrs.add (k,v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return hdrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Implements blocking reading semantics on top of a non-blocking channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    static class ReadStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        SocketChannel channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        SelectorCache sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        Selector selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        ByteBuffer chanbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        SelectionKey key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        int available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        byte[] one;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        boolean closed = false, eof = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        ByteBuffer markBuf; /* reads may be satisifed from this buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        boolean marked;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        boolean reset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        int readlimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        static long readTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        ServerImpl server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            readTimeout = ServerConfig.getReadTimeout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        public ReadStream (ServerImpl server, SocketChannel chan) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            this.channel = chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            this.server = server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            sc = SelectorCache.getSelectorCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            selector = sc.getSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            chanbuf = ByteBuffer.allocate (8* 1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            key = chan.register (selector, SelectionKey.OP_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            available = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            one = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            closed = marked = reset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        public synchronized int read (byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            return read (b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        public synchronized int read () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            int result = read (one, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            if (result == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                return one[0] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        public synchronized int read (byte[] b, int off, int srclen) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            int canreturn, willreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                throw new IOException ("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            if (eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            if (reset) { /* satisfy from markBuf */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                canreturn = markBuf.remaining ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                willreturn = canreturn>srclen ? srclen : canreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                markBuf.get(b, off, willreturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                if (canreturn == willreturn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                    reset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            } else { /* satisfy from channel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                canreturn = available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                while (canreturn == 0 && !eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    block ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    canreturn = available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                if (eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                willreturn = canreturn>srclen ? srclen : canreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                chanbuf.get(b, off, willreturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                available -= willreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                if (marked) { /* copy into markBuf */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                        markBuf.put (b, off, willreturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    } catch (BufferOverflowException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        marked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            return willreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        public synchronized int available () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                throw new IOException ("Stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            if (eof)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            if (reset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                return markBuf.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            if (available > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                return available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            chanbuf.clear ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            available = channel.read (chanbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            if (available > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                chanbuf.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            } else if (available == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                eof = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                available = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            return available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
         * block() only called when available==0 and buf is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        private synchronized void block () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            long currtime = server.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            long maxtime = currtime + readTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            while (currtime < maxtime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                if (selector.select (readTimeout) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    selector.selectedKeys().clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    available ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                currtime = server.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            throw new SocketTimeoutException ("no data received");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        public void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            channel.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            selector.selectNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            sc.freeSelector(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        public synchronized void mark (int readlimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            this.readlimit = readlimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            markBuf = ByteBuffer.allocate (readlimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            marked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            reset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        public synchronized void reset () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            if (closed )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            if (!marked)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                throw new IOException ("Stream not marked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            marked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            reset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            markBuf.flip ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    static class WriteStream extends java.io.OutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        SocketChannel channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        ByteBuffer buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        SelectionKey key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        SelectorCache sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        Selector selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        boolean closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        byte[] one;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        ServerImpl server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        static long writeTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            writeTimeout = ServerConfig.getWriteTimeout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        public WriteStream (ServerImpl server, SocketChannel channel) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            this.channel = channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            this.server = server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            sc = SelectorCache.getSelectorCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            selector = sc.getSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            key = channel.register (selector, SelectionKey.OP_WRITE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            one = new byte [1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            buf = ByteBuffer.allocate (4096);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        public synchronized void write (int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            one[0] = (byte)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            write (one, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        public synchronized void write (byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            write (b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        public synchronized void write (byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            int l = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                throw new IOException ("stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            int cap = buf.capacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            if (cap < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                int diff = len - cap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                buf = ByteBuffer.allocate (2*(cap+diff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            buf.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            buf.put (b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            buf.flip ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            while ((n = channel.write (buf)) < l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                l -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                if (l == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                block();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        void block () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            long currtime = server.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            long maxtime = currtime + writeTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            while (currtime < maxtime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                if (selector.select (writeTimeout) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    selector.selectedKeys().clear ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                currtime = server.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            throw new SocketTimeoutException ("write blocked too long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        public void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            channel.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            selector.selectNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            sc.freeSelector(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
}