Merge pull request #64 in ITERATE/cyberduck from feature/windows/9074 to master
[cyberduck.git] / source / ch / cyberduck / core / openstack / SwiftReadFeature.java
blob54daadcced14d48af7fb95df84b71ed5a400be5f
1 package ch.cyberduck.core.openstack;
3 /*
4 * Copyright (c) 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:
18 * feedback@cyberduck.ch
21 import ch.cyberduck.core.DefaultIOExceptionMappingService;
22 import ch.cyberduck.core.Path;
23 import ch.cyberduck.core.PathContainerService;
24 import ch.cyberduck.core.exception.BackgroundException;
25 import ch.cyberduck.core.features.Read;
26 import ch.cyberduck.core.transfer.TransferStatus;
28 import org.apache.log4j.Logger;
30 import java.io.IOException;
31 import java.io.InputStream;
33 import ch.iterate.openstack.swift.exception.GenericException;
34 import ch.iterate.openstack.swift.io.ContentLengthInputStream;
36 /**
37 * @version $Id$
39 public class SwiftReadFeature implements Read {
40 private static final Logger log = Logger.getLogger(SwiftReadFeature.class);
42 private PathContainerService containerService
43 = new SwiftPathContainerService();
45 private SwiftSession session;
47 private SwiftRegionService regionService;
49 public SwiftReadFeature(final SwiftSession session, final SwiftRegionService regionService) {
50 this.session = session;
51 this.regionService = regionService;
54 @Override
55 public InputStream read(final Path file, final TransferStatus status) throws BackgroundException {
56 try {
57 final ContentLengthInputStream stream;
58 if(status.isAppend()) {
59 if(status.getLength() > 0) {
60 stream = session.getClient().getObject(regionService.lookup(file),
61 containerService.getContainer(file).getName(), containerService.getKey(file),
62 status.getOffset(), status.getLength());
64 else {
65 stream = session.getClient().getObject(regionService.lookup(file),
66 containerService.getContainer(file).getName(), containerService.getKey(file),
67 status.getOffset());
70 else {
71 stream = session.getClient().getObject(regionService.lookup(file),
72 containerService.getContainer(file).getName(), containerService.getKey(file));
74 if(log.isDebugEnabled()) {
75 log.debug(String.format("Reading stream with content length %d", stream.getLength()));
77 return stream;
79 catch(GenericException e) {
80 throw new SwiftExceptionMappingService().map("Download {0} failed", e, file);
82 catch(IOException e) {
83 throw new DefaultIOExceptionMappingService().map(e, file);
87 @Override
88 public boolean offset(final Path file) {
89 return true;