Merge branch 'develop' into feature_previews
[pub/Android/ownCloud.git] / src / eu / alefzero / webdav / FileRequestEntity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20 package eu.alefzero.webdav;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.OutputStream;
25 import java.io.RandomAccessFile;
26 import java.nio.ByteBuffer;
27 import java.nio.channels.FileChannel;
28 import java.util.Collection;
29 import java.util.HashSet;
30 import java.util.Iterator;
31 import java.util.Set;
32
33 import org.apache.commons.httpclient.methods.RequestEntity;
34
35 import com.owncloud.android.network.ProgressiveDataTransferer;
36
37 import eu.alefzero.webdav.OnDatatransferProgressListener;
38
39 import android.util.Log;
40
41
42 /**
43 * A RequestEntity that represents a File.
44 *
45 */
46 public class FileRequestEntity implements RequestEntity, ProgressiveDataTransferer {
47
48 final File mFile;
49 final String mContentType;
50 Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
51
52 public FileRequestEntity(final File file, final String contentType) {
53 super();
54 this.mFile = file;
55 this.mContentType = contentType;
56 if (file == null) {
57 throw new IllegalArgumentException("File may not be null");
58 }
59 }
60
61 @Override
62 public long getContentLength() {
63 return mFile.length();
64 }
65
66 @Override
67 public String getContentType() {
68 return mContentType;
69 }
70
71 @Override
72 public boolean isRepeatable() {
73 return true;
74 }
75
76 @Override
77 public void addDatatransferProgressListener(OnDatatransferProgressListener listener) {
78 synchronized (mDataTransferListeners) {
79 mDataTransferListeners.add(listener);
80 }
81 }
82
83 @Override
84 public void addDatatransferProgressListeners(Collection<OnDatatransferProgressListener> listeners) {
85 synchronized (mDataTransferListeners) {
86 mDataTransferListeners.addAll(listeners);
87 }
88 }
89
90 @Override
91 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
92 synchronized (mDataTransferListeners) {
93 mDataTransferListeners.remove(listener);
94 }
95 }
96
97
98 @Override
99 public void writeRequest(final OutputStream out) throws IOException {
100 //byte[] tmp = new byte[4096];
101 ByteBuffer tmp = ByteBuffer.allocate(4096);
102 int readResult = 0;
103
104 // TODO(bprzybylski): each mem allocation can throw OutOfMemoryError we need to handle it
105 // globally in some fashionable manner
106 RandomAccessFile raf = new RandomAccessFile(mFile, "r");
107 FileChannel channel = raf.getChannel();
108 Iterator<OnDatatransferProgressListener> it = null;
109 long transferred = 0;
110 long size = mFile.length();
111 if (size == 0) size = -1;
112 try {
113 while ((readResult = channel.read(tmp)) >= 0) {
114 out.write(tmp.array(), 0, readResult);
115 tmp.clear();
116 transferred += readResult;
117 synchronized (mDataTransferListeners) {
118 it = mDataTransferListeners.iterator();
119 while (it.hasNext()) {
120 it.next().onTransferProgress(readResult, transferred, size, mFile.getName());
121 }
122 }
123 }
124
125 } catch (IOException io) {
126 Log.e("FileRequestException", 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 } finally {
130 channel.close();
131 raf.close();
132 }
133 }
134
135 }