OC-1867: Remove package eu.alefzero.webdav
[pub/Android/ownCloud.git] / src / com / owncloud / android / network / 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 com.owncloud.android.network.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
37 /**
38 * A RequestEntity that represents a PIECE of a file.
39 *
40 * @author David A. Velasco
41 */
42 public class ChunkFromFileChannelRequestEntity implements RequestEntity, ProgressiveDataTransferer {
43
44 private static final String TAG = ChunkFromFileChannelRequestEntity.class.getSimpleName();
45
46 //private final File mFile;
47 private final FileChannel mChannel;
48 private final String mContentType;
49 private final long mChunkSize;
50 private final File mFile;
51 private long mOffset;
52 private long mTransferred;
53 Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
54 private ByteBuffer mBuffer = ByteBuffer.allocate(4096);
55
56 public ChunkFromFileChannelRequestEntity(final FileChannel channel, final String contentType, long chunkSize, final File file) {
57 super();
58 if (channel == null) {
59 throw new IllegalArgumentException("File may not be null");
60 }
61 if (chunkSize <= 0) {
62 throw new IllegalArgumentException("Chunk size must be greater than zero");
63 }
64 mChannel = channel;
65 mContentType = contentType;
66 mChunkSize = chunkSize;
67 mFile = file;
68 mOffset = 0;
69 mTransferred = 0;
70 }
71
72 public void setOffset(long offset) {
73 mOffset = offset;
74 }
75
76 public long getContentLength() {
77 try {
78 return Math.min(mChunkSize, mChannel.size() - mChannel.position());
79 } catch (IOException e) {
80 return mChunkSize;
81 }
82 }
83
84 public String getContentType() {
85 return mContentType;
86 }
87
88 public boolean isRepeatable() {
89 return true;
90 }
91
92 @Override
93 public void addDatatransferProgressListener(OnDatatransferProgressListener listener) {
94 synchronized (mDataTransferListeners) {
95 mDataTransferListeners.add(listener);
96 }
97 }
98
99 @Override
100 public void addDatatransferProgressListeners(Collection<OnDatatransferProgressListener> listeners) {
101 synchronized (mDataTransferListeners) {
102 mDataTransferListeners.addAll(listeners);
103 }
104 }
105
106 @Override
107 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
108 synchronized (mDataTransferListeners) {
109 mDataTransferListeners.remove(listener);
110 }
111 }
112
113
114 public void writeRequest(final OutputStream out) throws IOException {
115 int readCount = 0;
116 Iterator<OnDatatransferProgressListener> it = null;
117
118 try {
119 mChannel.position(mOffset);
120 long size = mFile.length();
121 if (size == 0) size = -1;
122 long maxCount = Math.min(mOffset + mChunkSize, mChannel.size());
123 while (mChannel.position() < maxCount) {
124 readCount = mChannel.read(mBuffer);
125 out.write(mBuffer.array(), 0, readCount);
126 mBuffer.clear();
127 if (mTransferred < maxCount) { // condition to avoid accumulate progress for repeated chunks
128 mTransferred += readCount;
129 }
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_OC.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 }