jdk/src/share/classes/sun/net/www/http/KeepAliveStream.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 51 6fe31bc95bbc
child 3865 cc2ca3d07bbb
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 1996-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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.http;
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.net.HttpURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import sun.net.ProgressSource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.net.www.MeteredStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * A stream that has the property of being able to be kept alive for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * multiple downloads from the same server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Stephen R. Pietrowicz (NCSA)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Dave Brown
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class KeepAliveStream extends MeteredStream implements Hurryable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // instance variables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    HttpClient hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    boolean hurried;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // has this KeepAliveStream been put on the queue for asynchronous cleanup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    protected boolean queuedForCleanup = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static KeepAliveStreamCleaner queue = new KeepAliveStreamCleaner();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static Thread cleanerThread = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static boolean startCleanupThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public KeepAliveStream(InputStream is, ProgressSource pi, long expected, HttpClient hc)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        super(is, pi, expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        this.hc = hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Attempt to cache this connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public void close() throws IOException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        // If the inputstream is closed already, just return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        // If this stream has already been queued for cleanup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (queuedForCleanup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        // Skip past the data that's left in the Inputstream because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        // some sort of error may have occurred.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // Do this ONLY if the skip won't block. The stream may have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        // been closed at the beginning of a big file and we don't want
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        // to hang around for nothing. So if we can't skip without blocking
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        // we just close the socket and, therefore, terminate the keepAlive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        // NOTE: Don't close super class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (expected > count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                long nskip = (long) (expected - count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                if (nskip <= available()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    long n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    while (n < nskip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        nskip = nskip - n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                        n = skip(nskip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                } else if (expected <= KeepAliveStreamCleaner.MAX_DATA_REMAINING && !hurried) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    //put this KeepAliveStream on the queue so that the data remaining
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    //on the socket can be cleanup asyncronously.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    queueForCleanup(new KeepAliveCleanerEntry(this, hc));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    hc.closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (!closed && !hurried && !queuedForCleanup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                hc.finished();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            if (pi != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                pi.finishTracking();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (!queuedForCleanup) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                // nulling out the underlying inputstream as well as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                // httpClient to let gc collect the memories faster
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                hc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /* we explicitly do not support mark/reset */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public boolean markSupported()  {
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public void mark(int limit) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        throw new IOException("mark/reset not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public synchronized boolean hurry() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            /* CASE 0: we're actually already done */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (closed || count >= expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            } else if (in.available() < (expected - count)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                /* CASE I: can't meet the demand */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                /* CASE II: fill our internal buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                 * Remind: possibly check memory here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                int size = (int) (expected - count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                byte[] buf = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                DataInputStream dis = new DataInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                dis.readFully(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                in = new ByteArrayInputStream(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                hurried = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            // e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private static synchronized void queueForCleanup(KeepAliveCleanerEntry kace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if(queue != null && !kace.getQueuedForCleanup()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if (!queue.offer(kace)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                kace.getHttpClient().closeServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            kace.setQueuedForCleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        startCleanupThread = (cleanerThread == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (!startCleanupThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            if (!cleanerThread.isAlive()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                startCleanupThread = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if (startCleanupThread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            java.security.AccessController.doPrivileged(
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   177
                new java.security.PrivilegedAction<Void>() {
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
   178
                public Void run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    // We want to create the Keep-Alive-SocketCleaner in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    // system threadgroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    ThreadGroup grp = Thread.currentThread().getThreadGroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    ThreadGroup parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    while ((parent = grp.getParent()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                        grp = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    cleanerThread = new Thread(grp, queue, "Keep-Alive-SocketCleaner");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    cleanerThread.setDaemon(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                    cleanerThread.setPriority(Thread.MAX_PRIORITY - 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    cleanerThread.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    protected long remainingToRead() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return expected - count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    protected void setClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        hc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
class KeepAliveCleanerEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    KeepAliveStream kas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    HttpClient hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    public KeepAliveCleanerEntry(KeepAliveStream kas, HttpClient hc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        this.kas = kas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        this.hc = hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    protected KeepAliveStream getKeepAliveStream() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return kas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    protected HttpClient getHttpClient() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return hc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    protected void setQueuedForCleanup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        kas.queuedForCleanup = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    protected boolean getQueuedForCleanup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        return kas.queuedForCleanup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
}