jdk/src/share/classes/sun/net/www/MeteredStream.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
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) 1994, 2004, 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.www;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import sun.net.ProgressSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.net.www.http.ChunkedInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class MeteredStream extends FilterInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    // Instance variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    /* if expected != -1, after we've read >= expected, we're "closed" and return -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     * from subsequest read() 's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    protected boolean closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    protected long expected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    protected long count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    protected long markedCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    protected int markLimit = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    protected ProgressSource pi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public MeteredStream(InputStream is, ProgressSource pi, long expected)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        super(is);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        this.pi = pi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        this.expected = expected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (pi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            pi.updateProgress(0, expected);
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
    private final void justRead(long n) throws IOException   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (n == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
             * don't close automatically when mark is set and is valid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
             * cannot reset() after close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            if (!isMarked()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        count += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
         * If read beyond the markLimit, invalidate the mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if (count - markedCount > markLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            markLimit = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (pi != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            pi.updateProgress(count, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (isMarked()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        // if expected length is known, we could determine if
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // read overrun.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (expected > 0)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            if (count >= expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * Returns true if the mark is valid, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private boolean isMarked() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (markLimit < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // mark is set, but is not valid anymore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (count - markedCount > markLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
           return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        // mark still holds
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public synchronized int read() throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        int c = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (c != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            justRead(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            justRead(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public synchronized int read(byte b[], int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                throws java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        int n = in.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        justRead(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public synchronized long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        // REMIND: what does skip do on EOF????
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (in instanceof ChunkedInputStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            n = in.skip(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            // just skip min(n, num_bytes_left)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            long min = (n > expected - count) ? expected - count: n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            n = in.skip(min);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        justRead(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (pi != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            pi.finishTracking();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public synchronized int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return closed ? 0: in.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public synchronized void mark(int readLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        super.mark(readLimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         * mark the count to restore upon reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        markedCount = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        markLimit = readLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public synchronized void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (!isMarked()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new IOException ("Resetting to an invalid mark");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        count = markedCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        super.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return super.markSupported();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    protected void finalize() throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (pi != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                pi.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            // Call super class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            super.finalize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
}