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.GruntspudUtil;
024: import gruntspud.ui.CommandProgressDialog;
025:
026: import java.io.IOException;
027: import java.io.InputStream;
028:
029: public class MonitorInputStream extends InputStream {
030: private int length, total;
031: private CommandProgressDialog progress;
032: private InputStream in;
033: private StringBuffer buf = new StringBuffer();
034:
035: public MonitorInputStream(InputStream in, int length,
036: CommandProgressDialog progress) {
037: this .length = length;
038: this .progress = progress;
039: this .in = in;
040: if (!progress.isProgress2Visible()) {
041: progress.setProgress2Visible(true);
042: }
043: progress.setProgress2Maximum(length);
044: progress.setProgress2Value(0);
045: progress.setProgress2Name("File progress");
046: }
047:
048: /* (non-Javadoc)
049: * @see java.io.InputStream#read()
050: */
051: public int read() throws IOException {
052: int z = in.read();
053: if (z != -1) {
054: update(1);
055: }
056: return z;
057: }
058:
059: /* (non-Javadoc)
060: * @see java.io.InputStream#read(byte[])
061: */
062: public int read(byte[] b) throws IOException {
063: int z = in.read(b);
064: if (z != -1) {
065: update(z);
066: }
067: return z;
068: }
069:
070: /* (non-Javadoc)
071: * @see java.io.InputStream#read(byte[], int, int)
072: */
073: public int read(byte[] b, int off, int len) throws IOException {
074: int z = in.read(b, off, len);
075: if (z != -1) {
076: update(z);
077: }
078: return z;
079: }
080:
081: private void update(int u) {
082: total += u;
083: progress.setProgress2Value(total);
084: int pc = (int) (((float) total / (float) length) * 100);
085: buf.setLength(0);
086: buf.append(GruntspudUtil.formatFileSize(total));
087: buf.append(" / ");
088: buf.append(GruntspudUtil.formatFileSize(length));
089: buf.append(" - ");
090: buf.append(pc);
091: buf.append("%");
092: progress.setString2(buf.toString());
093: }
094:
095: /* (non-Javadoc)
096: * @see java.io.InputStream#available()
097: */
098: public int available() throws IOException {
099: return in.available();
100: }
101:
102: /* (non-Javadoc)
103: * @see java.io.InputStream#close()
104: */
105: public void close() throws IOException {
106: buf.setLength(0);
107: buf.append("Complete - ");
108: buf.append(GruntspudUtil.formatFileSize(total));
109: progress.setString2(buf.toString());
110: in.close();
111: }
112:
113: /* (non-Javadoc)
114: * @see java.io.InputStream#mark(int)
115: */
116: public synchronized void mark(int readlimit) {
117: in.mark(readlimit);
118: }
119:
120: /* (non-Javadoc)
121: * @see java.io.InputStream#markSupported()
122: */
123: public boolean markSupported() {
124: return in.markSupported();
125: }
126:
127: /* (non-Javadoc)
128: * @see java.io.InputStream#reset()
129: */
130: public synchronized void reset() throws IOException {
131: in.reset();
132: }
133:
134: /* (non-Javadoc)
135: * @see java.io.InputStream#skip(long)
136: */
137: public long skip(long n) throws IOException {
138: return in.skip(n);
139: }
140: }
|