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