001: /*
002: * @(#)MeteredStream.java 1.41 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.net.www;
029:
030: import java.net.URL;
031: import java.io.*;
032: import sun.net.ProgressData;
033: import sun.net.ProgressEntry;
034:
035: public class MeteredStream extends FilterInputStream {
036:
037: // Instance variables.
038: /* after we've read >= expected, we're "closed" and return -1
039: * from subsequest read() 's
040: */
041: protected boolean closed = false;
042: protected int expected;
043: protected int count = 0;
044: protected ProgressEntry te;
045:
046: public MeteredStream(InputStream is, ProgressEntry te) {
047: super (is);
048: this .te = te;
049: expected = te.need;
050: ProgressData.pdata.update(te);
051: }
052:
053: private final void justRead(int n) throws IOException {
054: if (n == -1) {
055: close();
056: return;
057: }
058: count += n;
059: te.update(count, expected);
060: ProgressData.pdata.update(te);
061: if (count >= expected) {
062: close();
063: }
064: }
065:
066: public synchronized int read() throws java.io.IOException {
067: if (closed) {
068: return -1;
069: }
070: int c = in.read();
071: if (c != -1) {
072: justRead(1);
073: } else {
074: close();
075: }
076: return c;
077: }
078:
079: public synchronized int read(byte b[], int off, int len)
080: throws java.io.IOException {
081: if (closed) {
082: return -1;
083: }
084: int n = in.read(b, off, len);
085: justRead(n);
086: return n;
087: }
088:
089: public synchronized long skip(long n) throws IOException {
090: if (closed) {
091: return 0;
092: }
093: // just skip min(n, num_bytes_left)
094: int min = (n > expected - count) ? expected - count : (int) n;
095: n = in.skip(min);
096: justRead((int) n);
097: return n;
098: }
099:
100: public void close() throws IOException {
101: if (closed) {
102: return;
103: }
104: ProgressData.pdata.unregister(te);
105: closed = true;
106: in.close();
107: }
108:
109: public synchronized int available() throws IOException {
110: return closed ? 0 : in.available();
111: }
112:
113: public synchronized void mark(int readlimit) {
114: if (closed) {
115: return;
116: }
117: super .mark(readlimit);
118: }
119:
120: public synchronized void reset() throws IOException {
121: if (closed) {
122: return;
123: }
124: super .reset();
125: }
126:
127: public boolean markSupported() {
128: if (closed) {
129: return false;
130: }
131: return super .markSupported();
132: }
133:
134: protected void finalize() {
135: /* Do not explicitly de-register from finalizer. THIS MEANS YOU! */
136: te.what = ProgressData.DELETE;
137: }
138: }
|