9bce93a7fd53de083308ec20fe6d9f1fa574361e
[pub/Android/ownCloud.git] / oc_framework / src / com / owncloud / android / lib / network / FileRequestEntity.java
1 /* ownCloud Android Library is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
3 * Copyright (C) 2012 Bartek Przybylski
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 *
24 */
25
26 package com.owncloud.android.lib.network;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.OutputStream;
31 import java.io.RandomAccessFile;
32 import java.nio.ByteBuffer;
33 import java.nio.channels.FileChannel;
34 import java.util.Collection;
35 import java.util.HashSet;
36 import java.util.Iterator;
37 import java.util.Set;
38
39 import org.apache.commons.httpclient.methods.RequestEntity;
40
41 import android.util.Log;
42
43
44
45 /**
46 * A RequestEntity that represents a File.
47 *
48 */
49 public class FileRequestEntity implements RequestEntity, ProgressiveDataTransferer {
50
51 final File mFile;
52 final String mContentType;
53 Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
54
55 public FileRequestEntity(final File file, final String contentType) {
56 super();
57 this.mFile = file;
58 this.mContentType = contentType;
59 if (file == null) {
60 throw new IllegalArgumentException("File may not be null");
61 }
62 }
63
64 @Override
65 public long getContentLength() {
66 return mFile.length();
67 }
68
69 @Override
70 public String getContentType() {
71 return mContentType;
72 }
73
74 @Override
75 public boolean isRepeatable() {
76 return true;
77 }
78
79 @Override
80 public void addDatatransferProgressListener(OnDatatransferProgressListener listener) {
81 synchronized (mDataTransferListeners) {
82 mDataTransferListeners.add(listener);
83 }
84 }
85
86 @Override
87 public void addDatatransferProgressListeners(Collection<OnDatatransferProgressListener> listeners) {
88 synchronized (mDataTransferListeners) {
89 mDataTransferListeners.addAll(listeners);
90 }
91 }
92
93 @Override
94 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
95 synchronized (mDataTransferListeners) {
96 mDataTransferListeners.remove(listener);
97 }
98 }
99
100
101 @Override
102 public void writeRequest(final OutputStream out) throws IOException {
103 //byte[] tmp = new byte[4096];
104 ByteBuffer tmp = ByteBuffer.allocate(4096);
105 int readResult = 0;
106
107 // TODO(bprzybylski): each mem allocation can throw OutOfMemoryError we need to handle it
108 // globally in some fashionable manner
109 RandomAccessFile raf = new RandomAccessFile(mFile, "r");
110 FileChannel channel = raf.getChannel();
111 Iterator<OnDatatransferProgressListener> it = null;
112 long transferred = 0;
113 long size = mFile.length();
114 if (size == 0) size = -1;
115 try {
116 while ((readResult = channel.read(tmp)) >= 0) {
117 out.write(tmp.array(), 0, readResult);
118 tmp.clear();
119 transferred += readResult;
120 synchronized (mDataTransferListeners) {
121 it = mDataTransferListeners.iterator();
122 while (it.hasNext()) {
123 it.next().onTransferProgress(readResult, transferred, size, mFile.getAbsolutePath());
124 }
125 }
126 }
127
128 } catch (IOException io) {
129 Log.e("FileRequestException", io.getMessage());
130 throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io);
131
132 } finally {
133 channel.close();
134 raf.close();
135 }
136 }
137
138 }