Showing the changes to java/pkg/sun/net/httpserver/LeftOverInputStream.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,122 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (c) 2005, 2007, Oracle and/or its affiliates. All rights reserved. | |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
| 4 | + * | |
| 5 | + * This code is free software; you can redistribute it and/or modify it | |
| 6 | + * under the terms of the GNU General Public License version 2 only, as | |
| 7 | + * published by the Free Software Foundation. Oracle designates this | |
| 8 | + * particular file as subject to the "Classpath" exception as provided | |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. | |
| 10 | + * | |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT | |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that | |
| 15 | + * accompanied this code). | |
| 16 | + * | |
| 17 | + * You should have received a copy of the GNU General Public License version | |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, | |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
| 20 | + * | |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
| 22 | + * or visit www.oracle.com if you need additional information or have any | |
| 23 | + * questions. | |
| 24 | + */ | |
| 25 | + | |
| 26 | +package sun.net.httpserver; | |
| 27 | + | |
| 28 | +import java.io.*; | |
| 29 | +import com.sun.net.httpserver.*; | |
| 30 | +import com.sun.net.httpserver.spi.*; | |
| 31 | + | |
| 32 | +/** | |
| 33 | + * a (filter) input stream which can tell us if bytes are "left over" | |
| 34 | + * on the underlying stream which can be read (without blocking) | |
| 35 | + * on another instance of this class. | |
| 36 | + * | |
| 37 | + * The class can also report if all bytes "expected" to be read | |
| 38 | + * were read, by the time close() was called. In that case, | |
| 39 | + * bytes may be drained to consume them (by calling drain() ). | |
| 40 | + * | |
| 41 | + * isEOF() returns true, when all expected bytes have been read | |
| 42 | + */ | |
| 43 | +abstract class LeftOverInputStream extends FilterInputStream { | |
| 44 | + ExchangeImpl t; | |
| 45 | + ServerImpl server; | |
| 46 | + protected boolean closed = false; | |
| 47 | + protected boolean eof = false; | |
| 48 | + byte[] one = new byte [1]; | |
| 49 | + | |
| 50 | + public LeftOverInputStream (ExchangeImpl t, InputStream src) { | |
| 51 | + super (src); | |
| 52 | + this.t = t; | |
| 53 | + this.server = t.getServerImpl(); | |
| 54 | + } | |
| 55 | + /** | |
| 56 | + * if bytes are left over buffered on *the UNDERLYING* stream | |
| 57 | + */ | |
| 58 | + public boolean isDataBuffered () throws IOException { | |
| 59 | + assert eof; | |
| 60 | + return super.available() > 0; | |
| 61 | + } | |
| 62 | + | |
| 63 | + public void close () throws IOException { | |
| 64 | + if (closed) { | |
| 65 | + return; | |
| 66 | + } | |
| 67 | + closed = true; | |
| 68 | + if (!eof) { | |
| 69 | + eof = drain (ServerConfig.getDrainAmount()); | |
| 70 | + } | |
| 71 | + } | |
| 72 | + | |
| 73 | + public boolean isClosed () { | |
| 74 | + return closed; | |
| 75 | + } | |
| 76 | + | |
| 77 | + public boolean isEOF () { | |
| 78 | + return eof; | |
| 79 | + } | |
| 80 | + | |
| 81 | + protected abstract int readImpl (byte[]b, int off, int len) throws IOException; | |
| 82 | + | |
| 83 | + public synchronized int read () throws IOException { | |
| 84 | + if (closed) { | |
| 85 | + throw new IOException ("Stream is closed"); | |
| 86 | + } | |
| 87 | + int c = readImpl (one, 0, 1); | |
| 88 | + if (c == -1 || c == 0) { | |
| 89 | + return c; | |
| 90 | + } else { | |
| 91 | + return one[0] & 0xFF; | |
| 92 | + } | |
| 93 | + } | |
| 94 | + | |
| 95 | + public synchronized int read (byte[]b, int off, int len) throws IOException { | |
| 96 | + if (closed) { | |
| 97 | + throw new IOException ("Stream is closed"); | |
| 98 | + } | |
| 99 | + return readImpl (b, off, len); | |
| 100 | + } | |
| 101 | + | |
| 102 | + /** | |
| 103 | + * read and discard up to l bytes or "eof" occurs, | |
| 104 | + * (whichever is first). Then return true if the stream | |
| 105 | + * is at eof (ie. all bytes were read) or false if not | |
| 106 | + * (still bytes to be read) | |
| 107 | + */ | |
| 108 | + public boolean drain (long l) throws IOException { | |
| 109 | + int bufSize = 2048; | |
| 110 | + byte[] db = new byte [bufSize]; | |
| 111 | + while (l > 0) { | |
| 112 | + long len = readImpl (db, 0, bufSize); | |
| 113 | + if (len == -1) { | |
| 114 | + eof = true; | |
| 115 | + return true; | |
| 116 | + } else { | |
| 117 | + l = l - len; | |
| 118 | + } | |
| 119 | + } | |
| 120 | + return false; | |
| 121 | + } | |
| 122 | +} | |