src/jdk.httpserver/share/classes/sun/net/httpserver/LeftOverInputStream.java
author goetz
Sat, 01 Sep 2018 18:15:27 +0200
changeset 51616 b071f4fff1f1
parent 47216 71c04702a3d5
permissions -rw-r--r--
8210259: [testbug] IncompatibleOptions.java fails if VM configured without ZGC Reviewed-by: pliden, kbarrett
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, 2007, Oracle and/or its affiliates. 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
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.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import com.sun.net.httpserver.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * a (filter) input stream which can tell us if bytes are "left over"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * on the underlying stream which can be read (without blocking)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * on another instance of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * The class can also report if all bytes "expected" to be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * were read, by the time close() was called. In that case,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * bytes may be drained to consume them (by calling drain() ).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * isEOF() returns true, when all expected bytes have been read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
abstract class LeftOverInputStream extends FilterInputStream {
44913
1b08f0eb012e 8179273: sun.net.httpserver.LeftOverInputStream should stop attempting to drain the stream when the server is stopped
dfuchs
parents: 25859
diff changeset
    44
    final ExchangeImpl t;
1b08f0eb012e 8179273: sun.net.httpserver.LeftOverInputStream should stop attempting to drain the stream when the server is stopped
dfuchs
parents: 25859
diff changeset
    45
    final ServerImpl server;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    protected boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    protected boolean eof = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    byte[] one = new byte [1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public LeftOverInputStream (ExchangeImpl t, InputStream src) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        super (src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        this.t = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        this.server = t.getServerImpl();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * if bytes are left over buffered on *the UNDERLYING* stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public boolean isDataBuffered () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        assert eof;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        return super.available() > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (!eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            eof = drain (ServerConfig.getDrainAmount());
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
    public boolean isClosed () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        return closed;
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 boolean isEOF () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return eof;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    protected abstract int readImpl (byte[]b, int off, int len) throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public synchronized int read () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            throw new IOException ("Stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        int c = readImpl (one, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (c == -1 || c == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return one[0] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public synchronized int read (byte[]b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new IOException ("Stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return readImpl (b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * read and discard up to l bytes or "eof" occurs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * (whichever is first). Then return true if the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * is at eof (ie. all bytes were read) or false if not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * (still bytes to be read)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public boolean drain (long l) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        int bufSize = 2048;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        byte[] db = new byte [bufSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        while (l > 0) {
44913
1b08f0eb012e 8179273: sun.net.httpserver.LeftOverInputStream should stop attempting to drain the stream when the server is stopped
dfuchs
parents: 25859
diff changeset
   112
            if (server.isFinishing()) {
1b08f0eb012e 8179273: sun.net.httpserver.LeftOverInputStream should stop attempting to drain the stream when the server is stopped
dfuchs
parents: 25859
diff changeset
   113
                break;
1b08f0eb012e 8179273: sun.net.httpserver.LeftOverInputStream should stop attempting to drain the stream when the server is stopped
dfuchs
parents: 25859
diff changeset
   114
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            long len = readImpl (db, 0, bufSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (len == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                eof = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                l = l - len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
}