Skip to content

Commit fbbc76c

Browse files
author
Gabriel Einsdorf
committed
DefaultDownloadService: switch to the copy method provided by DataHandles
1 parent 3d18d7b commit fbbc76c

File tree

1 file changed

+5
-29
lines changed

1 file changed

+5
-29
lines changed

src/main/java/org/scijava/download/DefaultDownloadService.java

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
import org.scijava.io.handle.DataHandle;
3939
import org.scijava.io.handle.DataHandleService;
40+
import org.scijava.io.handle.DataHandles;
4041
import org.scijava.io.location.Location;
4142
import org.scijava.plugin.Parameter;
4243
import org.scijava.plugin.Plugin;
@@ -70,7 +71,7 @@ public Download download(final Location source, final Location destination) {
7071
destination))
7172
{
7273
task.setStatusMessage("Downloading " + source.getURI());
73-
copy(task, in, out);
74+
DataHandles.copy(in, out, task);
7475
}
7576
catch (final IOException exc) {
7677
// TODO: Improve error handling:
@@ -102,13 +103,13 @@ public Download download(final Location source, final Location destination,
102103
if (isCachedHandleValid(source, cache, sourceHandle, cachedHandle)) {
103104
// The data is cached; download from the cached source instead.
104105
task.setStatusMessage("Retrieving " + source.getURI());
105-
copy(task, cachedHandle, destHandle);
106+
DataHandles.copy(cachedHandle, destHandle, task);
106107
}
107108
else {
108109
// Data is not yet cached; write to the destination _and_ the cache.
109110
task.setStatusMessage("Downloading + caching " + source.getURI());
110-
copy(task, sourceHandle, //
111-
new MultiWriteHandle(cachedHandle, destHandle));
111+
DataHandles.copy(sourceHandle, //
112+
new MultiWriteHandle(cachedHandle, destHandle), task);
112113
}
113114
}
114115
catch (final IOException exc) {
@@ -122,31 +123,6 @@ public Download download(final Location source, final Location destination,
122123

123124
// -- Helper methods --
124125

125-
private void copy(final Task task, final DataHandle<Location> in,
126-
final DataHandle<Location> out) throws IOException
127-
{
128-
long length;
129-
try {
130-
length = in.length();
131-
}
132-
catch (final IOException exc) {
133-
// Assume unknown length.
134-
length = 0;
135-
}
136-
if (length > 0) task.setProgressMaximum(length);
137-
138-
final int chunkSize = 64 * 1024; // TODO: Make size configurable.
139-
final byte[] buf = new byte[chunkSize];
140-
while (true) {
141-
if (task.isCanceled()) return;
142-
final int r = in.read(buf);
143-
if (r <= 0) break; // EOF
144-
if (task.isCanceled()) return;
145-
out.write(buf, 0, r);
146-
if (length > 0) task.setProgressValue(task.getProgressValue() + r);
147-
}
148-
}
149-
150126
private boolean isCachedHandleValid(final Location source,
151127
final LocationCache cache, final DataHandle<Location> sourceHandle,
152128
final DataHandle<Location> cachedHandle) throws IOException

0 commit comments

Comments
 (0)