1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
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.
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.
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/>.
19 package eu
.alefzero
.webdav
;
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
.nio
.channels
.FileLock
;
28 import java
.util
.Collection
;
29 import java
.util
.HashSet
;
30 import java
.util
.Iterator
;
33 import org
.apache
.commons
.httpclient
.methods
.RequestEntity
;
35 import eu
.alefzero
.webdav
.OnDatatransferProgressListener
;
37 import android
.util
.Log
;
41 * A RequestEntity that represents a File.
44 public class FileRequestEntity
implements RequestEntity
{
47 final String mContentType
;
48 Set
<OnDatatransferProgressListener
> mDataTransferListeners
= new HashSet
<OnDatatransferProgressListener
>();
50 public FileRequestEntity(final File file
, final String contentType
) {
53 this.mContentType
= contentType
;
55 throw new IllegalArgumentException("File may not be null");
60 public long getContentLength() {
61 return mFile
.length();
65 public String
getContentType() {
70 public boolean isRepeatable() {
74 public void addOnDatatransferProgressListener(OnDatatransferProgressListener listener
) {
75 mDataTransferListeners
.add(listener
);
78 public void addOnDatatransferProgressListeners(Collection
<OnDatatransferProgressListener
> listeners
) {
79 mDataTransferListeners
.addAll(listeners
);
82 public void removeOnDatatransferProgressListener(OnDatatransferProgressListener listener
) {
83 mDataTransferListeners
.remove(listener
);
88 public void writeRequest(final OutputStream out
) throws IOException
{
89 //byte[] tmp = new byte[4096];
90 ByteBuffer tmp
= ByteBuffer
.allocate(4096);
93 // TODO(bprzybylski): each mem allocation can throw OutOfMemoryError we need to handle it
94 // globally in some fashionable manner
95 RandomAccessFile raf
= new RandomAccessFile(mFile
, "r");
96 FileChannel channel
= raf
.getChannel();
97 FileLock lock
= channel
.tryLock();
98 Iterator
<OnDatatransferProgressListener
> it
= null
;
100 long size
= mFile
.length();
101 if (size
== 0) size
= -1;
103 while ((readResult
= channel
.read(tmp
)) >= 0) {
104 out
.write(tmp
.array(), 0, readResult
);
106 transferred
+= readResult
;
107 it
= mDataTransferListeners
.iterator();
108 while (it
.hasNext()) {
109 it
.next().onTransferProgress(readResult
, transferred
, size
, mFile
.getName());
113 } catch (IOException io
) {
114 Log
.e("FileRequestException", io
.getMessage());
115 throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io
);