瀏覽代碼

Re-add listener support.

pull/22/head
Greg Holmes 9 年之前
父節點
當前提交
4dde7c5d86

+ 3
- 1
src/com/dmdirc/util/io/Downloader.java 查看文件

@@ -108,7 +108,9 @@ public class Downloader {
108 108
         if (listener != null) {
109 109
             listener.setIndeterminate(true);
110 110
         }
111
-        Files.copy(getConnection(url, "").getInputStream(), file);
111
+        final URLConnection connection = getConnection(url, "");
112
+        Files.copy(new ListenerInputStream(connection.getInputStream(), listener,
113
+                        connection.getContentLength()), file);
112 114
     }
113 115
 
114 116
     /**

+ 110
- 0
src/com/dmdirc/util/io/ListenerInputStream.java 查看文件

@@ -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
+}

+ 3
- 0
test/com/dmdirc/util/io/DownloaderTest.java 查看文件

@@ -47,6 +47,8 @@ import static org.junit.Assert.assertEquals;
47 47
 import static org.junit.Assert.assertFalse;
48 48
 import static org.junit.Assert.assertTrue;
49 49
 import static org.mockito.Matchers.anyBoolean;
50
+import static org.mockito.Matchers.anyInt;
51
+import static org.mockito.Mockito.atLeastOnce;
50 52
 import static org.mockito.Mockito.never;
51 53
 import static org.mockito.Mockito.verify;
52 54
 import static org.mockito.Mockito.when;
@@ -121,6 +123,7 @@ public class DownloaderTest {
121 123
         assertEquals(Lists.newArrayList("OMG IM A FAKE DOWNLOAD"), Files.readAllLines(file,
122 124
                 Charset.forName("UTF-8")));
123 125
         verify(listener).setIndeterminate(anyBoolean());
126
+        verify(listener, atLeastOnce()).downloadProgress(anyInt());
124 127
     }
125 128
 
126 129
     private class TestableDownloader extends Downloader {

Loading…
取消
儲存