Add session to cache with both hostname and address.
[cyberduck.git] / source / ch / cyberduck / core / shared / DefaultDownloadFeature.java
blobfa062fcb1317fe48c118ed882774ea105284c11d
1 package ch.cyberduck.core.shared;
3 /*
4 * Copyright (c) 2002-2013 David Kocher. All rights reserved.
5 * http://cyberduck.ch/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to feedback@cyberduck.ch
20 import ch.cyberduck.core.ConnectionCallback;
21 import ch.cyberduck.core.DefaultIOExceptionMappingService;
22 import ch.cyberduck.core.Local;
23 import ch.cyberduck.core.Path;
24 import ch.cyberduck.core.Session;
25 import ch.cyberduck.core.exception.BackgroundException;
26 import ch.cyberduck.core.features.Download;
27 import ch.cyberduck.core.features.Read;
28 import ch.cyberduck.core.io.BandwidthThrottle;
29 import ch.cyberduck.core.io.DefaultStreamCloser;
30 import ch.cyberduck.core.io.StreamCloser;
31 import ch.cyberduck.core.io.StreamCopier;
32 import ch.cyberduck.core.io.StreamListener;
33 import ch.cyberduck.core.io.ThrottledInputStream;
34 import ch.cyberduck.core.transfer.TransferStatus;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.io.OutputStream;
40 /**
41 * @version $Id$
43 public class DefaultDownloadFeature implements Download {
45 private final Read reader;
47 public DefaultDownloadFeature(final Session<?> session) {
48 this.reader = session.getFeature(Read.class);
51 public DefaultDownloadFeature(final Read reader) {
52 this.reader = reader;
55 @Override
56 public void download(final Path file, final Local local, final BandwidthThrottle throttle, final StreamListener listener,
57 final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
58 try {
59 InputStream in = null;
60 OutputStream out = null;
61 try {
62 in = reader.read(file, status);
63 out = local.getOutputStream(status.isAppend());
64 new StreamCopier(status, status)
65 .withOffset(0L)
66 .withLimit(status.getLength())
67 .withListener(listener)
68 .transfer(new ThrottledInputStream(in, throttle), out);
70 finally {
71 final StreamCloser c = new DefaultStreamCloser();
72 c.close(in);
73 c.close(out);
76 catch(IOException e) {
77 throw new DefaultIOExceptionMappingService().map("Download {0} failed", e, file);
81 @Override
82 public boolean offset(final Path file) throws BackgroundException {
83 return reader.offset(file);