1 /* ownCloud Android Library is available under MIT license
2 * Copyright (C) 2014 ownCloud (http://www.owncloud.org/)
3 * Copyright (C) 2012 Bartek Przybylski
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
26 package com
.owncloud
.android
.oc_framework
.network
.webdav
;
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
;
39 import org
.apache
.commons
.httpclient
.methods
.RequestEntity
;
41 import android
.util
.Log
;
43 import com
.owncloud
.android
.oc_framework
.network
.ProgressiveDataTransferer
;
47 * A RequestEntity that represents a File.
50 public class FileRequestEntity
implements RequestEntity
, ProgressiveDataTransferer
{
53 final String mContentType
;
54 Set
<OnDatatransferProgressListener
> mDataTransferListeners
= new HashSet
<OnDatatransferProgressListener
>();
56 public FileRequestEntity(final File file
, final String contentType
) {
59 this.mContentType
= contentType
;
61 throw new IllegalArgumentException("File may not be null");
66 public long getContentLength() {
67 return mFile
.length();
71 public String
getContentType() {
76 public boolean isRepeatable() {
81 public void addDatatransferProgressListener(OnDatatransferProgressListener listener
) {
82 synchronized (mDataTransferListeners
) {
83 mDataTransferListeners
.add(listener
);
88 public void addDatatransferProgressListeners(Collection
<OnDatatransferProgressListener
> listeners
) {
89 synchronized (mDataTransferListeners
) {
90 mDataTransferListeners
.addAll(listeners
);
95 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener
) {
96 synchronized (mDataTransferListeners
) {
97 mDataTransferListeners
.remove(listener
);
103 public void writeRequest(final OutputStream out
) throws IOException
{
104 //byte[] tmp = new byte[4096];
105 ByteBuffer tmp
= ByteBuffer
.allocate(4096);
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;
117 while ((readResult
= channel
.read(tmp
)) >= 0) {
118 out
.write(tmp
.array(), 0, readResult
);
120 transferred
+= readResult
;
121 synchronized (mDataTransferListeners
) {
122 it
= mDataTransferListeners
.iterator();
123 while (it
.hasNext()) {
124 it
.next().onTransferProgress(readResult
, transferred
, size
, mFile
.getAbsolutePath());
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
);