Merge branch 'develop' into feature_previews
[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 as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package eu.alefzero.webdav;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.OutputStream;
24 import java.nio.ByteBuffer;
25 import java.nio.channels.FileChannel;
26 import java.util.Collection;
27 import java.util.HashSet;
28 import java.util.Iterator;
29 import java.util.Set;
30
31 import org.apache.commons.httpclient.methods.RequestEntity;
32
33 import com.owncloud.android.network.ProgressiveDataTransferer;
34
35 import eu.alefzero.webdav.OnDatatransferProgressListener;
36
37 import android.util.Log;
38
39
40 /**
41 * A RequestEntity that represents a PIECE of a file.
42 *
43 * @author David A. Velasco
44 */
45 public class ChunkFromFileChannelRequestEntity implements RequestEntity, ProgressiveDataTransferer {
46
47 private static final String TAG = ChunkFromFileChannelRequestEntity.class.getSimpleName();
48
49 //private final File mFile;
50 private final FileChannel mChannel;
51 private final String mContentType;
52 private final long mChunkSize;
53 private final File mFile;
54 private long mOffset;
55 private long mTransferred;
56 Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
57 private ByteBuffer mBuffer = ByteBuffer.allocate(4096);
58
59 public ChunkFromFileChannelRequestEntity(final FileChannel channel, final String contentType, long chunkSize, final File file) {
60 super();
61 if (channel == null) {
62 throw new IllegalArgumentException("File may not be null");
63 }
64 if (chunkSize <= 0) {
65 throw new IllegalArgumentException("Chunk size must be greater than zero");
66 }
67 mChannel = channel;
68 mContentType = contentType;
69 mChunkSize = chunkSize;
70 mFile = file;
71 mOffset = 0;
72 mTransferred = 0;
73 }
74
75 public void setOffset(long offset) {
76 mOffset = offset;
77 }
78
79 public long getContentLength() {
80 try {
81 return Math.min(mChunkSize, mChannel.size() - mChannel.position());
82 } catch (IOException e) {
83 return mChunkSize;
84 }
85 }
86
87 public String getContentType() {
88 return mContentType;
89 }
90
91 public boolean isRepeatable() {
92 return true;
93 }
94
95 @Override
96 public void addDatatransferProgressListener(OnDatatransferProgressListener listener) {
97 synchronized (mDataTransferListeners) {
98 mDataTransferListeners.add(listener);
99 }
100 }
101
102 @Override
103 public void addDatatransferProgressListeners(Collection<OnDatatransferProgressListener> listeners) {
104 synchronized (mDataTransferListeners) {
105 mDataTransferListeners.addAll(listeners);
106 }
107 }
108
109 @Override
110 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
111 synchronized (mDataTransferListeners) {
112 mDataTransferListeners.remove(listener);
113 }
114 }
115
116
117 public void writeRequest(final OutputStream out) throws IOException {
118 int readCount = 0;
119 Iterator<OnDatatransferProgressListener> it = null;
120
121 try {
122 mChannel.position(mOffset);
123 long size = mFile.length();
124 if (size == 0) size = -1;
125 while (mChannel.position() < mOffset + mChunkSize && mChannel.position() < mChannel.size()) {
126 readCount = mChannel.read(mBuffer);
127 out.write(mBuffer.array(), 0, readCount);
128 mBuffer.clear();
129 mTransferred += readCount;
130 synchronized (mDataTransferListeners) {
131 it = mDataTransferListeners.iterator();
132 while (it.hasNext()) {
133 it.next().onTransferProgress(readCount, mTransferred, size, mFile.getName());
134 }
135 }
136 }
137
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);
141
142 }
143 }
144
145 }