1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package com.owncloud.android.oc_framework.network.webdav;
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.nio.ByteBuffer;
24 import java.nio.channels.FileChannel;
25 import java.util.Collection;
26 import java.util.HashSet;
27 import java.util.Iterator;
30 import org.apache.commons.httpclient.methods.RequestEntity;
32 import com.owncloud.android.oc_framework.network.ProgressiveDataTransferer;
34 import android.util.Log;
38 * A RequestEntity that represents a PIECE of a file.
40 * @author David A. Velasco
42 public class ChunkFromFileChannelRequestEntity implements RequestEntity, ProgressiveDataTransferer {
44 private static final String TAG = ChunkFromFileChannelRequestEntity.class.getSimpleName();
46 //private final File mFile;
47 private final FileChannel mChannel;
48 private final String mContentType;
49 private final long mChunkSize;
50 private final File mFile;
52 private long mTransferred;
53 Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
54 private ByteBuffer mBuffer = ByteBuffer.allocate(4096);
56 public ChunkFromFileChannelRequestEntity(final FileChannel channel, final String contentType, long chunkSize, final File file) {
58 if (channel == null) {
59 throw new IllegalArgumentException("File may not be null");
62 throw new IllegalArgumentException("Chunk size must be greater than zero");
65 mContentType = contentType;
66 mChunkSize = chunkSize;
72 public void setOffset(long offset) {
76 public long getContentLength() {
78 return Math.min(mChunkSize, mChannel.size() - mChannel.position());
79 } catch (IOException e) {
84 public String getContentType() {
88 public boolean isRepeatable() {
93 public void addDatatransferProgressListener(OnDatatransferProgressListener listener) {
94 synchronized (mDataTransferListeners) {
95 mDataTransferListeners.add(listener);
100 public void addDatatransferProgressListeners(Collection<OnDatatransferProgressListener> listeners) {
101 synchronized (mDataTransferListeners) {
102 mDataTransferListeners.addAll(listeners);
107 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
108 synchronized (mDataTransferListeners) {
109 mDataTransferListeners.remove(listener);
114 public void writeRequest(final OutputStream out) throws IOException {
116 Iterator<OnDatatransferProgressListener> it = null;
119 mChannel.position(mOffset);
120 long size = mFile.length();
121 if (size == 0) size = -1;
122 long maxCount = Math.min(mOffset + mChunkSize, mChannel.size());
123 while (mChannel.position() < maxCount) {
124 readCount = mChannel.read(mBuffer);
125 out.write(mBuffer.array(), 0, readCount);
127 if (mTransferred < maxCount) { // condition to avoid accumulate progress for repeated chunks
128 mTransferred += readCount;
130 synchronized (mDataTransferListeners) {
131 it = mDataTransferListeners.iterator();
132 while (it.hasNext()) {
133 it.next().onTransferProgress(readCount, mTransferred, size, mFile.getName());
138 } catch (IOException io) {
139 Log.e(TAG, io.getMessage());
140 throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io);