001: /*
002: * Gruntspud
003: *
004: * Copyright (C) 2002 Brett Smith.
005: *
006: * Written by: Brett Smith <t_magicthize@users.sourceforge.net>
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU Library General Public License
010: * as published by the Free Software Foundation; either version 2 of
011: * the License, or (at your option) any later version.
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Library General Public License for more details.
016: *
017: * You should have received a copy of the GNU Library General Public
018: * License along with this program; if not, write to the Free Software
019: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
020: */
021: package gruntspud.file;
022:
023: import gruntspud.Constants;
024: import gruntspud.GruntspudUtil;
025: import gruntspud.ui.CommandProgressDialog;
026:
027: import java.io.IOException;
028:
029: import org.netbeans.lib.cvsclient.util.LoggedDataInputStream;
030:
031: public class MonitorLoggedDataInputStream extends LoggedDataInputStream {
032: private int length, total;
033: private CommandProgressDialog progress;
034: private StringBuffer buf = new StringBuffer();
035:
036: public MonitorLoggedDataInputStream(LoggedDataInputStream in,
037: int length, CommandProgressDialog progress) {
038: super (in);
039: Constants.IO_LOG.debug("Receiving length=" + length);
040: this .length = length;
041: this .progress = progress;
042: if (!progress.isProgress2Visible()) {
043: progress.setProgress2Visible(true);
044: }
045: progress.setProgress2Maximum(length);
046: progress.setProgress2Value(0);
047: progress.setProgress2Name("File progress");
048: }
049:
050: public String readLine() throws IOException {
051: String s = ((LoggedDataInputStream) getUnderlyingStream())
052: .readLine();
053: if (s != null) {
054: update(s.length());
055: }
056: return s;
057: }
058:
059: public int read() throws IOException {
060: int z = getUnderlyingStream().read();
061: if (z != -1) {
062: update(1);
063: }
064: return z;
065: }
066:
067: public int read(byte[] b) throws IOException {
068: int z = getUnderlyingStream().read(b);
069: if (z != -1) {
070: update(z);
071: }
072: return z;
073: }
074:
075: public int read(byte[] b, int off, int len) throws IOException {
076: int z = getUnderlyingStream().read(b, off, len);
077: if (z != -1) {
078: update(z);
079: }
080: return z;
081: }
082:
083: public byte readByte() throws IOException {
084: byte z = ((LoggedDataInputStream) getUnderlyingStream())
085: .readByte();
086: if (z != -1) {
087: update(z);
088: }
089: return z;
090: }
091:
092: private void update(int u) {
093: total += u;
094: progress.setProgress2Value(total);
095: int pc = (int) (((float) total / (float) length) * 100);
096: buf.setLength(0);
097: buf.append(GruntspudUtil.formatFileSize(total));
098: buf.append(" / ");
099: buf.append(GruntspudUtil.formatFileSize(length));
100: buf.append(" - ");
101: buf.append(pc);
102: buf.append("%");
103: progress.setString2(buf.toString());
104: }
105:
106: public void close() throws IOException {
107: buf.setLength(0);
108: buf.append("Complete - ");
109: buf.append(GruntspudUtil.formatFileSize(total));
110: progress.setString2(buf.toString());
111: super.close();
112:
113: }
114: }
|