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