0255446894c3624aad8866be67fd55ee6ffe542c
[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.Log_OC;
34 import com.owncloud.android.network.ProgressiveDataTransferer;
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 while (mChannel.position() < mOffset + mChunkSize && mChannel.position() < mChannel.size()) {
125 readCount = mChannel.read(mBuffer);
126 out.write(mBuffer.array(), 0, readCount);
127 mBuffer.clear();
128 mTransferred += readCount;
129 synchronized (mDataTransferListeners) {
130 it = mDataTransferListeners.iterator();
131 while (it.hasNext()) {
132 it.next().onTransferProgress(readCount, mTransferred, size, mFile.getName());
133 }
134 }
135 }
136
137 } catch (IOException io) {
138 Log_OC.e(TAG, io.getMessage());
139 throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io);
140
141 }
142 }
143
144 }