jdk/src/share/classes/sun/net/httpserver/Request.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2612 d7fb0809c7e4
child 1511 65ddd8f149f3
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
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();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            /* skip blank lines */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        } while (startLine.equals (""));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    char[] buf = new char [BUF_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    int pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    StringBuffer lineBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public InputStream inputStream () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public OutputStream outputStream () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * read a line from the stream returning as a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Not used for reading headers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public String readLine () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        boolean gotCR = false, gotLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        pos = 0; lineBuf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        while (!gotLF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            int c = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (gotCR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                if (c == LF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                    gotLF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    gotCR = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    consume (CR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    consume (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                if (c == CR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    gotCR = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    consume (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        lineBuf.append (buf, 0, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return new String (lineBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private void consume (int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (pos == BUF_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            lineBuf.append (buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        buf[pos++] = (char)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * returns the request line (first line of a request)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public String requestLine () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return startLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    Headers hdrs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    Headers headers () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (hdrs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return hdrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        hdrs = new Headers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        char s[] = new char[10];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        int firstc = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        while (firstc != LF && firstc != CR && firstc >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            int len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            int keyend = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            boolean inKey = firstc > ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            s[len++] = (char) firstc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    parseloop:{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                while ((c = is.read()) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    switch (c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                      case ':':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        if (inKey && len > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                            keyend = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                        inKey = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                      case '\t':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        c = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                      case ' ':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        inKey = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                      case CR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                      case LF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                        firstc = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                        if (c == CR && firstc == LF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                            firstc = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                            if (firstc == CR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                firstc = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        if (firstc == LF || firstc == CR || firstc > ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                            break parseloop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                        /* continuation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        c = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    if (len >= s.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                        char ns[] = new char[s.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        System.arraycopy(s, 0, ns, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        s = ns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    s[len++] = (char) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                firstc = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            while (len > 0 && s[len - 1] <= ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                len--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            String k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            if (keyend <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                k = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                keyend = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                k = String.copyValueOf(s, 0, keyend);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                if (keyend < len && s[keyend] == ':')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    keyend++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                while (keyend < len && s[keyend] <= ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    keyend++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            String v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if (keyend >= len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                v = new String();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                v = String.copyValueOf(s, keyend, len - keyend);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            hdrs.add (k,v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return hdrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Implements blocking reading semantics on top of a non-blocking channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    static class ReadStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        SocketChannel channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        SelectorCache sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        Selector selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        ByteBuffer chanbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        SelectionKey key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        int available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        byte[] one;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        boolean closed = false, eof = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        ByteBuffer markBuf; /* reads may be satisifed from this buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        boolean marked;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        boolean reset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        int readlimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        static long readTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        ServerImpl server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            readTimeout = ServerConfig.getReadTimeout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        public ReadStream (ServerImpl server, SocketChannel chan) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            this.channel = chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            this.server = server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            sc = SelectorCache.getSelectorCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            selector = sc.getSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            chanbuf = ByteBuffer.allocate (8* 1024);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            key = chan.register (selector, SelectionKey.OP_READ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            available = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            one = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            closed = marked = reset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        public synchronized int read (byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            return read (b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        public synchronized int read () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            int result = read (one, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            if (result == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                return one[0] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        public synchronized int read (byte[] b, int off, int srclen) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            int canreturn, willreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                throw new IOException ("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            if (eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            if (reset) { /* satisfy from markBuf */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                canreturn = markBuf.remaining ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                willreturn = canreturn>srclen ? srclen : canreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                markBuf.get(b, off, willreturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                if (canreturn == willreturn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    reset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            } else { /* satisfy from channel */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                canreturn = available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                while (canreturn == 0 && !eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                    block ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    canreturn = available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                if (eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                willreturn = canreturn>srclen ? srclen : canreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                chanbuf.get(b, off, willreturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                available -= willreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                if (marked) { /* copy into markBuf */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                        markBuf.put (b, off, willreturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    } catch (BufferOverflowException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                        marked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            return willreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        public synchronized int available () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                throw new IOException ("Stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (eof)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            if (reset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                return markBuf.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            if (available > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                return available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            chanbuf.clear ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            available = channel.read (chanbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            if (available > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                chanbuf.flip();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            } else if (available == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                eof = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                available = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return available;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
         * block() only called when available==0 and buf is empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        private synchronized void block () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            long currtime = server.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            long maxtime = currtime + readTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            while (currtime < maxtime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                if (selector.select (readTimeout) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    selector.selectedKeys().clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                    available ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                currtime = server.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            throw new SocketTimeoutException ("no data received");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        public void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            channel.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            selector.selectNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            sc.freeSelector(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        public synchronized void mark (int readlimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            this.readlimit = readlimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            markBuf = ByteBuffer.allocate (readlimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            marked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            reset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        public synchronized void reset () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            if (closed )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            if (!marked)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                throw new IOException ("Stream not marked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            marked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            reset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            markBuf.flip ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    static class WriteStream extends java.io.OutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        SocketChannel channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        ByteBuffer buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        SelectionKey key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        SelectorCache sc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        Selector selector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        boolean closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        byte[] one;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        ServerImpl server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        static long writeTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            writeTimeout = ServerConfig.getWriteTimeout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        public WriteStream (ServerImpl server, SocketChannel channel) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            this.channel = channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            this.server = server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            sc = SelectorCache.getSelectorCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            selector = sc.getSelector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            key = channel.register (selector, SelectionKey.OP_WRITE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            one = new byte [1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            buf = ByteBuffer.allocate (4096);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        public synchronized void write (int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            one[0] = (byte)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            write (one, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        public synchronized void write (byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            write (b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        public synchronized void write (byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            int l = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                throw new IOException ("stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            int cap = buf.capacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            if (cap < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                int diff = len - cap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                buf = ByteBuffer.allocate (2*(cap+diff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            buf.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            buf.put (b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            buf.flip ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            while ((n = channel.write (buf)) < l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                l -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                if (l == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                block();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        void block () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            long currtime = server.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            long maxtime = currtime + writeTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            while (currtime < maxtime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                if (selector.select (writeTimeout) == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    selector.selectedKeys().clear ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                currtime = server.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            throw new SocketTimeoutException ("write blocked too long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        public void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            channel.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            selector.selectNow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            sc.freeSelector(selector);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
}