Path: blob/master/src/java.base/share/classes/sun/net/ProgressSource.java
41152 views
/*1* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package sun.net;2526import java.net.URL;2728/**29* ProgressSource represents the source of progress changes.30*31* @author Stanley Man-Kit Ho32*/33public class ProgressSource34{35public enum State { NEW, CONNECTED, UPDATE, DELETE };3637// URL38private URL url;39// URL method40private String method;41// Content type42private String contentType;43// bytes read44private long progress = 0;45// last bytes read46private long lastProgress = 0;47//bytes expected48private long expected = -1;49// the last thing to happen with this source50private State state;51// connect flag52private boolean connected = false;53// threshold for notification54private int threshold = 8192;55// progress monitor56private ProgressMonitor progressMonitor;5758/**59* Construct progress source object.60*/61public ProgressSource(URL url, String method) {62this(url, method, -1);63}6465/**66* Construct progress source object.67*/68public ProgressSource(URL url, String method, long expected) {69this.url = url;70this.method = method;71this.contentType = "content/unknown";72this.progress = 0;73this.lastProgress = 0;74this.expected = expected;75this.state = State.NEW;76this.progressMonitor = ProgressMonitor.getDefault();77this.threshold = progressMonitor.getProgressUpdateThreshold();78}7980public boolean connected() {81if (!connected) {82connected = true;83state = State.CONNECTED;84return false;85}86return true;87}8889/**90* Close progress source.91*/92public void close() {93state = State.DELETE;94}9596/**97* Return URL of progress source.98*/99public URL getURL() {100return url;101}102103/**104* Return method of URL.105*/106public String getMethod() {107return method;108}109110/**111* Return content type of URL.112*/113public String getContentType() {114return contentType;115}116117// Change content type118public void setContentType(String ct) {119contentType = ct;120}121122/**123* Return current progress.124*/125public long getProgress() {126return progress;127}128129/**130* Return expected maximum progress; -1 if expected is unknown.131*/132public long getExpected() {133return expected;134}135136/**137* Return state.138*/139public State getState() {140return state;141}142143/**144* Begin progress tracking.145*/146public void beginTracking() {147progressMonitor.registerSource(this);148}149150/**151* Finish progress tracking.152*/153public void finishTracking() {154progressMonitor.unregisterSource(this);155}156157/**158* Update progress.159*/160public void updateProgress(long latestProgress, long expectedProgress) {161lastProgress = progress;162progress = latestProgress;163expected = expectedProgress;164165if (connected() == false)166state = State.CONNECTED;167else168state = State.UPDATE;169170// The threshold effectively divides the progress into171// different set of ranges:172//173// Range 0: 0..threshold-1,174// Range 1: threshold .. 2*threshold-1175// ....176// Range n: n*threshold .. (n+1)*threshold-1177//178// To determine which range the progress belongs to, it179// would be calculated as follow:180//181// range number = progress / threshold182//183// Notification should only be triggered when the current184// progress and the last progress are in different ranges,185// i.e. they have different range numbers.186//187// Using this range scheme, notification will be generated188// only once when the progress reaches each range.189//190if (lastProgress / threshold != progress / threshold) {191progressMonitor.updateProgress(this);192}193194// Detect read overrun195if (expected != -1) {196if (progress >= expected && progress != 0)197close();198}199}200201public Object clone() throws CloneNotSupportedException {202return super.clone();203}204205public String toString() {206return getClass().getName() + "[url=" + url + ", method=" + method + ", state=" + state207+ ", content-type=" + contentType + ", progress=" + progress + ", expected=" + expected + "]";208}209}210211212