Cancellation in uploads and access to details view pressing the status notification
[pub/Android/ownCloud.git] / src / eu / alefzero / webdav / ChunkFromFileChannelRequestEntity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
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 3 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 eu.alefzero.webdav.OnDatatransferProgressListener;
34
35 import android.util.Log;
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 {
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 public void addOnDatatransferProgressListener(OnDatatransferProgressListener listener) {
94 mDataTransferListeners.add(listener);
95 }
96
97 public void addOnDatatransferProgressListeners(Collection<OnDatatransferProgressListener> listeners) {
98 mDataTransferListeners.addAll(listeners);
99 }
100
101 public void removeOnDatatransferProgressListener(OnDatatransferProgressListener listener) {
102 mDataTransferListeners.remove(listener);
103 }
104
105
106 public void writeRequest(final OutputStream out) throws IOException {
107 int readCount = 0;
108 Iterator<OnDatatransferProgressListener> it = null;
109
110 try {
111 mChannel.position(mOffset);
112 long size = mFile.length();
113 if (size == 0) size = -1;
114 while (mChannel.position() < mOffset + mChunkSize && mChannel.position() < mChannel.size()) {
115 readCount = mChannel.read(mBuffer);
116 out.write(mBuffer.array(), 0, readCount);
117 mBuffer.clear();
118 mTransferred += readCount;
119 it = mDataTransferListeners.iterator();
120 while (it.hasNext()) {
121 it.next().onTransferProgress(readCount, mTransferred, size, mFile.getName());
122 }
123 }
124
125 } catch (IOException io) {
126 Log.e(TAG, io.getMessage());
127 throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io);
128
129 }
130 }
131
132 }