Fixed undesired restart of finished audio previews when the device orientation changes
[pub/Android/ownCloud.git] / src / eu / alefzero / webdav / FileRequestEntity.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
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.network.ProgressiveDataTransferer;
35
36 import eu.alefzero.webdav.OnDatatransferProgressListener;
37
38 import android.util.Log;
39
40
41 /**
42 * A RequestEntity that represents a File.
43 *
44 */
45 public class FileRequestEntity implements RequestEntity, ProgressiveDataTransferer {
46
47 final File mFile;
48 final String mContentType;
49 Set<OnDatatransferProgressListener> mDataTransferListeners = new HashSet<OnDatatransferProgressListener>();
50
51 public FileRequestEntity(final File file, final String contentType) {
52 super();
53 this.mFile = file;
54 this.mContentType = contentType;
55 if (file == null) {
56 throw new IllegalArgumentException("File may not be null");
57 }
58 }
59
60 @Override
61 public long getContentLength() {
62 return mFile.length();
63 }
64
65 @Override
66 public String getContentType() {
67 return mContentType;
68 }
69
70 @Override
71 public boolean isRepeatable() {
72 return true;
73 }
74
75 @Override
76 public void addDatatransferProgressListener(OnDatatransferProgressListener listener) {
77 synchronized (mDataTransferListeners) {
78 mDataTransferListeners.add(listener);
79 }
80 }
81
82 @Override
83 public void addDatatransferProgressListeners(Collection<OnDatatransferProgressListener> listeners) {
84 synchronized (mDataTransferListeners) {
85 mDataTransferListeners.addAll(listeners);
86 }
87 }
88
89 @Override
90 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener) {
91 synchronized (mDataTransferListeners) {
92 mDataTransferListeners.remove(listener);
93 }
94 }
95
96
97 @Override
98 public void writeRequest(final OutputStream out) throws IOException {
99 //byte[] tmp = new byte[4096];
100 ByteBuffer tmp = ByteBuffer.allocate(4096);
101 int readResult = 0;
102
103 // TODO(bprzybylski): each mem allocation can throw OutOfMemoryError we need to handle it
104 // globally in some fashionable manner
105 RandomAccessFile raf = new RandomAccessFile(mFile, "r");
106 FileChannel channel = raf.getChannel();
107 Iterator<OnDatatransferProgressListener> it = null;
108 long transferred = 0;
109 long size = mFile.length();
110 if (size == 0) size = -1;
111 try {
112 while ((readResult = channel.read(tmp)) >= 0) {
113 out.write(tmp.array(), 0, readResult);
114 tmp.clear();
115 transferred += readResult;
116 synchronized (mDataTransferListeners) {
117 it = mDataTransferListeners.iterator();
118 while (it.hasNext()) {
119 it.next().onTransferProgress(readResult, transferred, size, mFile.getName());
120 }
121 }
122 }
123
124 } catch (IOException io) {
125 Log.e("FileRequestException", io.getMessage());
126 throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io);
127
128 } finally {
129 channel.close();
130 raf.close();
131 }
132 }
133
134 }