KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > command > ProgressObservedInputStream


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18

19 package org.columba.core.command;
20
21 import java.io.FilterInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24
25 import org.columba.api.command.IWorkerStatusController;
26
27 public class ProgressObservedInputStream extends FilterInputStream JavaDoc {
28
29     private IWorkerStatusController status;
30
31     private int read;
32
33     /**
34          * Constructs the ProgressObservedInputStream.java.
35          *
36          * @param arg0
37          */

38     public ProgressObservedInputStream(InputStream JavaDoc arg0,
39         IWorkerStatusController theStatusController) {
40     this(arg0, theStatusController, false);
41     }
42
43     /**
44          * Constructs the ProgressObservedInputStream.java.
45          *
46          * @param arg0
47          */

48     public ProgressObservedInputStream(InputStream JavaDoc arg0,
49         IWorkerStatusController theStatusController, boolean relative) {
50     super(arg0);
51     this.status = theStatusController;
52
53     if (!relative) {
54         try {
55         theStatusController.setProgressBarMaximum(arg0.available());
56         } catch (IOException JavaDoc e) {
57         // nothing to do
58
}
59
60         read = 0;
61     } else {
62         read = theStatusController.getProgressBarValue();
63     }
64     }
65
66     /**
67          * @see java.io.InputStream#read()
68          */

69     @Override JavaDoc
70     public int read() throws IOException JavaDoc {
71     int result = super.read();
72     if (result != -1)
73         status.setProgressBarValue(++read);
74     return result;
75     }
76
77     /**
78          * @see java.io.InputStream#read(byte[], int, int)
79          */

80     @Override JavaDoc
81     public int read(byte[] arg0, int arg1, int arg2) throws IOException JavaDoc {
82     int result = super.read(arg0, arg1, arg2);
83     read += result;
84     status.setProgressBarValue(read);
85
86     return result;
87     }
88 }
Popular Tags