1 package ch
.cyberduck
.core
.shared
;
4 * Copyright (c) 2002-2013 David Kocher. All rights reserved.
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
;
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
) {
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
{
59 InputStream in
= null;
60 OutputStream out
= null;
62 in
= reader
.read(file
, status
);
63 out
= local
.getOutputStream(status
.isAppend());
64 new StreamCopier(status
, status
)
66 .withLimit(status
.getLength())
67 .withListener(listener
)
68 .transfer(new ThrottledInputStream(in
, throttle
), out
);
71 final StreamCloser c
= new DefaultStreamCloser();
76 catch(IOException e
) {
77 throw new DefaultIOExceptionMappingService().map("Download {0} failed", e
, file
);
82 public boolean offset(final Path file
) throws BackgroundException
{
83 return reader
.offset(file
);