|
@@ -0,0 +1,110 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2014 DMDirc Developers
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+ * SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+package com.dmdirc.util.io;
|
|
24
|
+
|
|
25
|
+import java.io.FilterInputStream;
|
|
26
|
+import java.io.IOException;
|
|
27
|
+import java.io.InputStream;
|
|
28
|
+
|
|
29
|
+import javax.annotation.Nonnull;
|
|
30
|
+
|
|
31
|
+/**
|
|
32
|
+ *
|
|
33
|
+ */
|
|
34
|
+public class ListenerInputStream extends FilterInputStream {
|
|
35
|
+
|
|
36
|
+ private final DownloadListener listener;
|
|
37
|
+ private final int length;
|
|
38
|
+ private int count;
|
|
39
|
+ private int mark;
|
|
40
|
+
|
|
41
|
+ public ListenerInputStream(@Nonnull final InputStream in, final DownloadListener listener,
|
|
42
|
+ final int length) {
|
|
43
|
+ super(in);
|
|
44
|
+ this.listener = listener;
|
|
45
|
+ this.length = length;
|
|
46
|
+ count = 0;
|
|
47
|
+ mark = count;
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ @Override
|
|
51
|
+ public int read() throws IOException {
|
|
52
|
+ final int read = super.read();
|
|
53
|
+ return update(read);
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ @Override
|
|
57
|
+ public int read(@Nonnull final byte[] b) throws IOException {
|
|
58
|
+ final int read = super.read(b);
|
|
59
|
+ return update(read);
|
|
60
|
+ }
|
|
61
|
+
|
|
62
|
+ @Override
|
|
63
|
+ public int read(@Nonnull final byte[] b, final int off, final int len) throws IOException {
|
|
64
|
+ final int read = super.read(b, off, len);
|
|
65
|
+ return update(read);
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+ @Override
|
|
69
|
+ public long skip(final long n) throws IOException {
|
|
70
|
+ final long read = super.skip(n);
|
|
71
|
+ return update((int) read);
|
|
72
|
+ }
|
|
73
|
+
|
|
74
|
+ @Override
|
|
75
|
+ public int available() throws IOException {
|
|
76
|
+ return super.available();
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ @Override
|
|
80
|
+ public void close() throws IOException {
|
|
81
|
+ super.close();
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ @Override
|
|
85
|
+ public synchronized void mark(final int readlimit) {
|
|
86
|
+ mark = count;
|
|
87
|
+ super.mark(readlimit);
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ @Override
|
|
91
|
+ public synchronized void reset() throws IOException {
|
|
92
|
+ update(mark - count);
|
|
93
|
+ super.reset();
|
|
94
|
+ }
|
|
95
|
+
|
|
96
|
+ @Override
|
|
97
|
+ public boolean markSupported() {
|
|
98
|
+ return super.markSupported();
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ private int update(final int read) {
|
|
102
|
+ if (read > 0) {
|
|
103
|
+ count += read;
|
|
104
|
+ if (listener != null && length != -1) {
|
|
105
|
+ listener.downloadProgress(100 * (float) count / length);
|
|
106
|
+ }
|
|
107
|
+ }
|
|
108
|
+ return read;
|
|
109
|
+ }
|
|
110
|
+}
|