Merge branch 'develop' into setup_buttons
[pub/Android/ownCloud.git] / src / eu / alefzero / webdav / ChunkFromFileChannelRequestEntity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
3 *
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.
7 *
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.
12 *
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/>.
15 *
16 */
17
18 package eu.alefzero.webdav;
19
20 import java.io.File;
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;
28 import java.util.Set;
29
30 import org.apache.commons.httpclient.methods.RequestEntity;
31
32 import com.owncloud.android.Log_OC;
33 import com.owncloud.android.network.ProgressiveDataTransferer;
34
35
36 import eu.alefzero.webdav.OnDatatransferProgressListener;
37
38
39 /**
40 * A RequestEntity that represents a PIECE of a file.
41 *
42 * @author David A. Velasco
43 */
44 public class ChunkFromFileChannelRequestEntity implements RequestEntity, ProgressiveDataTransferer {
45
46 private static final String TAG = ChunkFromFileChannelRequestEntity.class.getSimpleName();
47
48 //private final File mFile;
49 private final FileChannel mChannel;
50 private final String mContentType;
51 private final long mChunkSize;
52 private final File mFile;
53 private long mOffset;
54 private long mTransferred;
55 Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
56 private ByteBuffer mBuffer = ByteBuffer.allocate(4096);
57
58 public ChunkFromFileChannelRequestEntity(final FileChannel channel, final String contentType, long chunkSize, final File file) {
59 super();
60 if (channel == null) {
61 throw new IllegalArgumentException("File may not be null");
62 }
63 if (chunkSize <= 0) {
64 throw new IllegalArgumentException("Chunk size must be greater than zero");
65 }
66 mChannel = channel;
67 mContentType = contentType;
68 mChunkSize = chunkSize;
69 mFile = file;
70 mOffset = 0;
71 mTransferred = 0;
72 }
73
74 public void setOffset(long offset) {
75 mOffset = offset;
76 }
77
78 public long getContentLength() {
79 try {
80 return Math.min(mChunkSize, mChannel.size() - mChannel.position());
81 } catch (IOException e) {
82 return mChunkSize;
83 }
84 }
85
86 public String getContentType() {
87 return mContentType;
88 }
89
90 public boolean isRepeatable() {
91 return true;
92 }
93
94 @Override
95 public void addDatatransferProgressListener(OnDatatransferProgressListener listener) {
96 synchronized (mDataTransferListeners) {
97 mDataTransferListeners.add(listener);
98 }
99 }
100
101 @Override
102 public void addDatatransferProgressListeners(Collection<OnDatatransferProgressListener> listeners) {
103 synchronized (mDataTransferListeners) {
104 mDataTransferListeners.addAll(listeners);
105 }
106 }
107
108 @Override
109 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
110 synchronized (mDataTransferListeners) {
111 mDataTransferListeners.remove(listener);
112 }
113 }
114
115
116 public void writeRequest(final OutputStream out) throws IOException {
117 int readCount = 0;
118 Iterator<OnDatatransferProgressListener> it = null;
119
120 try {
121 mChannel.position(mOffset);
122 long size = mFile.length();
123 if (size == 0) size = -1;
124 long maxCount = Math.min(mOffset + mChunkSize, mChannel.size());
125 while (mChannel.position() < maxCount) {
126 readCount = mChannel.read(mBuffer);
127 out.write(mBuffer.array(), 0, readCount);
128 mBuffer.clear();
129 if (mTransferred < maxCount) { // condition to avoid accumulate progress for repeated chunks
130 mTransferred += readCount;
131 }
132 synchronized (mDataTransferListeners) {
133 it = mDataTransferListeners.iterator();
134 while (it.hasNext()) {
135 it.next().onTransferProgress(readCount, mTransferred, size, mFile.getName());
136 }
137 }
138 }
139
140 } catch (IOException io) {
141 Log_OC.e(TAG, io.getMessage());
142 throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io);
143
144 }
145 }
146
147 }