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