This wraps the android.util.logging into Log_OC which , if its enabled
[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
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 {
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 public void addOnDatatransferProgressListener(OnDatatransferProgressListener listener) {
96 mDataTransferListeners.add(listener);
97 }
98
99 public void addOnDatatransferProgressListeners(Collection<OnDatatransferProgressListener> listeners) {
100 mDataTransferListeners.addAll(listeners);
101 }
102
103 public void removeOnDatatransferProgressListener(OnDatatransferProgressListener listener) {
104 mDataTransferListeners.remove(listener);
105 }
106
107
108 public void writeRequest(final OutputStream out) throws IOException {
109 int readCount = 0;
110 Iterator<OnDatatransferProgressListener> it = null;
111
112 try {
113 mChannel.position(mOffset);
114 long size = mFile.length();
115 if (size == 0) size = -1;
116 while (mChannel.position() < mOffset + mChunkSize && mChannel.position() < mChannel.size()) {
117 readCount = mChannel.read(mBuffer);
118 out.write(mBuffer.array(), 0, readCount);
119 mBuffer.clear();
120 mTransferred += readCount;
121 it = mDataTransferListeners.iterator();
122 while (it.hasNext()) {
123 it.next().onTransferProgress(readCount, mTransferred, size, mFile.getName());
124 }
125 }
126
127 } catch (IOException io) {
128 Log_OC.e(TAG, io.getMessage());
129 throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io);
130
131 }
132 }
133
134 }