Path: blob/master/src/jdk.httpserver/share/classes/sun/net/httpserver/LeftOverInputStream.java
41159 views
/*1* Copyright (c) 2005, 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.net.httpserver;2627import java.io.*;28import com.sun.net.httpserver.*;29import com.sun.net.httpserver.spi.*;3031/**32* a (filter) input stream which can tell us if bytes are "left over"33* on the underlying stream which can be read (without blocking)34* on another instance of this class.35*36* The class can also report if all bytes "expected" to be read37* were read, by the time close() was called. In that case,38* bytes may be drained to consume them (by calling drain() ).39*40* isEOF() returns true, when all expected bytes have been read41*/42abstract class LeftOverInputStream extends FilterInputStream {43final ExchangeImpl t;44final ServerImpl server;45protected boolean closed = false;46protected boolean eof = false;47byte[] one = new byte [1];4849public LeftOverInputStream (ExchangeImpl t, InputStream src) {50super (src);51this.t = t;52this.server = t.getServerImpl();53}54/**55* if bytes are left over buffered on *the UNDERLYING* stream56*/57public boolean isDataBuffered () throws IOException {58assert eof;59return super.available() > 0;60}6162public void close () throws IOException {63if (closed) {64return;65}66closed = true;67if (!eof) {68eof = drain (ServerConfig.getDrainAmount());69}70}7172public boolean isClosed () {73return closed;74}7576public boolean isEOF () {77return eof;78}7980protected abstract int readImpl (byte[]b, int off, int len) throws IOException;8182public synchronized int read () throws IOException {83if (closed) {84throw new IOException ("Stream is closed");85}86int c = readImpl (one, 0, 1);87if (c == -1 || c == 0) {88return c;89} else {90return one[0] & 0xFF;91}92}9394public synchronized int read (byte[]b, int off, int len) throws IOException {95if (closed) {96throw new IOException ("Stream is closed");97}98return readImpl (b, off, len);99}100101/**102* read and discard up to l bytes or "eof" occurs,103* (whichever is first). Then return true if the stream104* is at eof (ie. all bytes were read) or false if not105* (still bytes to be read)106*/107public boolean drain (long l) throws IOException {108int bufSize = 2048;109byte[] db = new byte [bufSize];110while (l > 0) {111if (server.isFinishing()) {112break;113}114long len = readImpl (db, 0, bufSize);115if (len == -1) {116eof = true;117return true;118} else {119l = l - len;120}121}122return false;123}124}125126127