1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.opencastproject.util;
23
24 import org.apache.commons.io.input.ProxyInputStream;
25
26 import java.beans.PropertyChangeListener;
27 import java.beans.PropertyChangeSupport;
28 import java.io.IOException;
29 import java.io.InputStream;
30
31
32
33
34 public class ProgressInputStream extends ProxyInputStream {
35
36 private final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
37
38 private volatile long totalNumBytesRead;
39
40 public ProgressInputStream(InputStream in) {
41 super(in);
42 }
43
44 public long getTotalNumBytesRead() {
45 return totalNumBytesRead;
46 }
47
48
49
50
51
52
53
54
55
56 public void addPropertyChangeListener(PropertyChangeListener l) {
57 propertyChangeSupport.addPropertyChangeListener(l);
58 }
59
60
61
62
63
64
65
66
67
68 public void removePropertyChangeListener(PropertyChangeListener l) {
69 propertyChangeSupport.removePropertyChangeListener(l);
70 }
71
72 @Override
73 public int read() throws IOException {
74 return (int) updateProgress(super.read());
75 }
76
77 @Override
78 public int read(byte[] b) throws IOException {
79 return (int) updateProgress(super.read(b));
80 }
81
82 @Override
83 public int read(byte[] b, int off, int len) throws IOException {
84 return (int) updateProgress(super.read(b, off, len));
85 }
86
87 @Override
88 public long skip(long n) throws IOException {
89 return updateProgress(super.skip(n));
90 }
91
92 @Override
93 public void mark(int readlimit) {
94 throw new UnsupportedOperationException();
95 }
96
97 @Override
98 public void reset() throws IOException {
99 throw new UnsupportedOperationException();
100 }
101
102 @Override
103 public boolean markSupported() {
104 return false;
105 }
106
107 private long updateProgress(long numBytesRead) {
108 if (numBytesRead > 0) {
109 long oldTotalNumBytesRead = totalNumBytesRead;
110 totalNumBytesRead += numBytesRead;
111 propertyChangeSupport.firePropertyChange("totalNumBytesRead", oldTotalNumBytesRead, totalNumBytesRead);
112 }
113 return numBytesRead;
114 }
115 }