001: /*
002: * IzPack - Copyright 2001-2008 Julien Ponge, All Rights Reserved.
003: *
004: * http://izpack.org/
005: * http://izpack.codehaus.org/
006: *
007: * Copyright 2007 Vladimir Ralev
008: *
009: * Licensed under the Apache License, Version 2.0 (the "License");
010: * you may not use this file except in compliance with the License.
011: * You may obtain a copy of the License at
012: *
013: * http://www.apache.org/licenses/LICENSE-2.0
014: *
015: * Unless required by applicable law or agreed to in writing, software
016: * distributed under the License is distributed on an "AS IS" BASIS,
017: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018: * See the License for the specific language governing permissions and
019: * limitations under the License.
020: */
021:
022: package com.izforge.izpack.installer;
023:
024: import java.io.IOException;
025: import java.io.InputStream;
026:
027: import com.izforge.izpack.Pack;
028:
029: /**
030: *
031: * Wraps an InputStream in order to track how much bytes are being read, and
032: * then updates the progress dialog. When the stream is opened the progress
033: * dialog shows up. When the stream is closed the dialog is disposed. Make sure
034: * you are closing the streams.
035: *
036: * @author <a href="vralev@redhat.com">Vladimir Ralev</a>
037: * @version $Revision: 1.1 $
038: */
039: public class LoggedInputStream extends InputStream {
040: private long bytesRead = 0;
041: private InputStream is;
042: private DownloadPanel downloader;
043: // private WebAccessor webAccessor; // Unused
044: private boolean cancelled = false;
045: private long lastTime = -1;
046: private long lastBytes = -1;
047:
048: public void setCancelled(boolean cancel) {
049: cancelled = cancel;
050: }
051:
052: public LoggedInputStream(InputStream is, WebAccessor webAccessor) {
053: if (is == null)
054: throw new RuntimeException("Unable to connect");
055: this .is = is;
056: // this.webAccessor = webAccessor;
057:
058: String sizeStr;
059: if (webAccessor.getContentLength() > 0)
060: sizeStr = "("
061: + Pack.toByteUnitsString(webAccessor
062: .getContentLength()) + ")";
063: else
064: sizeStr = "";
065:
066: downloader = new DownloadPanel(this );
067: downloader.setTitle("Downloading");
068: downloader.setFileLabel(webAccessor.getUrl() + " " + sizeStr);
069: downloader.setLocationRelativeTo(null);
070: downloader.setVisible(true);
071: if (webAccessor.getContentLength() > 0) {
072: downloader.setProgressMax(webAccessor.getContentLength());
073: downloader.setProgressCurrent(0);
074: }
075: }
076:
077: public int available() throws IOException {
078: return is.available();
079: }
080:
081: public void close() throws IOException {
082: downloader.setVisible(false);
083: downloader.dispose();
084: is.close();
085: }
086:
087: public synchronized void mark(int readlimit) {
088: is.mark(readlimit);
089: }
090:
091: public boolean markSupported() {
092: return is.markSupported();
093: }
094:
095: public synchronized void reset() throws IOException {
096: is.reset();
097: }
098:
099: public long skip(long n) throws IOException {
100: return is.skip(n);
101: }
102:
103: public int read(byte[] b, int off, int len) throws IOException {
104: int bytes = is.read(b, off, len);
105: if (bytes > 0)
106: bytesRead += bytes;
107: update();
108: return bytes;
109: }
110:
111: public int read(byte[] b) throws IOException {
112: int bytes = is.read(b);
113: if (bytes > 0)
114: bytesRead += bytes;
115: update();
116: return bytes;
117: }
118:
119: public long getBytesRead() {
120: return bytesRead;
121: }
122:
123: public int read() throws IOException {
124: int bytes = is.read();
125: if (bytes > 0)
126: bytesRead += 1;
127: update();
128: return bytes;
129: }
130:
131: private void update() {
132: if (lastTime > 0) {
133: long currTime = System.currentTimeMillis();
134: long diff = currTime - lastTime;
135: if (diff > 800) {
136: double bps = (double) (bytesRead - lastBytes)
137: / ((double) (diff) / 1000.);
138: downloader.setStatusLabel(Pack.toByteUnitsString(Math
139: .round(bps))
140: + "/s");
141: lastTime = currTime;
142: lastBytes = bytesRead;
143: }
144: } else {
145: lastTime = System.currentTimeMillis();
146: lastBytes = bytesRead;
147: }
148: downloader.setProgressCurrent((int) bytesRead);
149: if (cancelled)
150: throw new RuntimeException("Cancelled");
151: }
152: }
|