Merge pull request #287 from LukeOwncloud/setup_md
[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 version 2,
7 * as published by the Free Software Foundation.
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.io.RandomAccessFile;
25 import java.nio.ByteBuffer;
26 import java.nio.channels.FileChannel;
27 import java.util.Collection;
28 import java.util.HashSet;
29 import java.util.Iterator;
30 import java.util.Set;
31
32 import org.apache.commons.httpclient.methods.RequestEntity;
33
34 import com.owncloud.android.Log_OC;
35 import com.owncloud.android.network.ProgressiveDataTransferer;
36
37 import eu.alefzero.webdav.OnDatatransferProgressListener;
38
39
40 /**
41 * A RequestEntity that represents a File.
42 *
43 */
44 public class FileRequestEntity implements RequestEntity, ProgressiveDataTransferer {
45
46 final File mFile;
47 final String mContentType;
48 Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
49
50 public FileRequestEntity(final File file, final String contentType) {
51 super();
52 this.mFile = file;
53 this.mContentType = contentType;
54 if (file == null) {
55 throw new IllegalArgumentException("File may not be null");
56 }
57 }
58
59 @Override
60 public long getContentLength() {
61 return mFile.length();
62 }
63
64 @Override
65 public String getContentType() {
66 return mContentType;
67 }
68
69 @Override
70 public boolean isRepeatable() {
71 return true;
72 }
73
74 @Override
75 public void addDatatransferProgressListener(OnDatatransferProgressListener listener) {
76 synchronized (mDataTransferListeners) {
77 mDataTransferListeners.add(listener);
78 }
79 }
80
81 @Override
82 public void addDatatransferProgressListeners(Collection<OnDatatransferProgressListener> listeners) {
83 synchronized (mDataTransferListeners) {
84 mDataTransferListeners.addAll(listeners);
85 }
86 }
87
88 @Override
89 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
90 synchronized (mDataTransferListeners) {
91 mDataTransferListeners.remove(listener);
92 }
93 }
94
95
96 @Override
97 public void writeRequest(final OutputStream out) throws IOException {
98 //byte[] tmp = new byte[4096];
99 ByteBuffer tmp = ByteBuffer.allocate(4096);
100 int readResult = 0;
101
102 // TODO(bprzybylski): each mem allocation can throw OutOfMemoryError we need to handle it
103 // globally in some fashionable manner
104 RandomAccessFile raf = new RandomAccessFile(mFile, "r");
105 FileChannel channel = raf.getChannel();
106 Iterator<OnDatatransferProgressListener> it = null;
107 long transferred = 0;
108 long size = mFile.length();
109 if (size == 0) size = -1;
110 try {
111 while ((readResult = channel.read(tmp)) >= 0) {
112 out.write(tmp.array(), 0, readResult);
113 tmp.clear();
114 transferred += readResult;
115 synchronized (mDataTransferListeners) {
116 it = mDataTransferListeners.iterator();
117 while (it.hasNext()) {
118 it.next().onTransferProgress(readResult, transferred, size, mFile.getName());
119 }
120 }
121 }
122
123 } catch (IOException io) {
124 Log_OC.e("FileRequestException", io.getMessage());
125 throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io);
126
127 } finally {
128 channel.close();
129 raf.close();
130 }
131 }
132
133 }