src/java.base/share/classes/sun/net/ProgressSource.java
author chegar
Fri, 18 Oct 2019 21:25:01 +0100
branchdatagramsocketimpl-branch
changeset 58697 e3ff12d14d43
parent 47216 71c04702a3d5
permissions -rw-r--r--
datagramsocketimpl-branch: minor refactoring
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) 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
package sun.net;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * ProgressSource represents the source of progress changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @author Stanley Man-Kit Ho
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class ProgressSource
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    public enum State { NEW, CONNECTED, UPDATE, DELETE };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    // URL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private URL url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    // URL method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private String method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    // Content type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private String contentType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    // bytes read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private long progress = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    // last bytes read
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private long lastProgress = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    //bytes expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private long expected = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // the last thing to happen with this source
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private State state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // connect flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private boolean connected = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // threshold for notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private int threshold = 8192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // progress monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private ProgressMonitor progressMonitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Construct progress source object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public ProgressSource(URL url, String method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        this(url, method, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Construct progress source object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public ProgressSource(URL url, String method, long expected)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        this.url = url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this.method = method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        this.contentType = "content/unknown";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        this.progress = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        this.lastProgress = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        this.expected = expected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        this.state = State.NEW;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        this.progressMonitor = ProgressMonitor.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        this.threshold = progressMonitor.getProgressUpdateThreshold();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public boolean connected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (!connected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            connected = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            state = State.CONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Close progress source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        state = State.DELETE;
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
     * Return URL of progress source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public URL getURL() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return url;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Return method of URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public String getMethod()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Return content type of URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public String getContentType()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return contentType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    // Change content type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public void setContentType(String ct)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        contentType = ct;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Return current progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public long getProgress()  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return progress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Return expected maximum progress; -1 if expected is unknown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public long getExpected() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return expected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Return state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public State getState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * Begin progress tracking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public void beginTracking() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        progressMonitor.registerSource(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Finish progress tracking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public void finishTracking() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        progressMonitor.unregisterSource(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * Update progress.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public void updateProgress(long latestProgress, long expectedProgress) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        lastProgress = progress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        progress = latestProgress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        expected = expectedProgress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (connected() == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            state = State.CONNECTED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            state = State.UPDATE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // The threshold effectively divides the progress into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        // different set of ranges:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        //      Range 0: 0..threshold-1,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        //      Range 1: threshold .. 2*threshold-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        //      ....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        //      Range n: n*threshold .. (n+1)*threshold-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        // To determine which range the progress belongs to, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        // would be calculated as follow:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        //      range number = progress / threshold
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        // Notification should only be triggered when the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        // progress and the last progress are in different ranges,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        // i.e. they have different range numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        // Using this range scheme, notification will be generated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        // only once when the progress reaches each range.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (lastProgress / threshold != progress / threshold)   {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            progressMonitor.updateProgress(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        // Detect read overrun
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (expected != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            if (progress >= expected && progress != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public String toString()    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return getClass().getName() + "[url=" + url + ", method=" + method + ", state=" + state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            + ", content-type=" + contentType + ", progress=" + progress + ", expected=" + expected + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
}