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
.lib
.network
;
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
;
46 * A RequestEntity that represents a File.
49 public class FileRequestEntity
implements RequestEntity
, ProgressiveDataTransferer
{
52 final String mContentType
;
53 Set
<OnDatatransferProgressListener
> mDataTransferListeners
= new HashSet
<OnDatatransferProgressListener
>();
55 public FileRequestEntity(final File file
, final String contentType
) {
58 this.mContentType
= contentType
;
60 throw new IllegalArgumentException("File may not be null");
65 public long getContentLength() {
66 return mFile
.length();
70 public String
getContentType() {
75 public boolean isRepeatable() {
80 public void addDatatransferProgressListener(OnDatatransferProgressListener listener
) {
81 synchronized (mDataTransferListeners
) {
82 mDataTransferListeners
.add(listener
);
87 public void addDatatransferProgressListeners(Collection
<OnDatatransferProgressListener
> listeners
) {
88 synchronized (mDataTransferListeners
) {
89 mDataTransferListeners
.addAll(listeners
);
94 public void removeDatatransferProgressListener(OnDatatransferProgressListener listener
) {
95 synchronized (mDataTransferListeners
) {
96 mDataTransferListeners
.remove(listener
);
102 public void writeRequest(final OutputStream out
) throws IOException
{
103 //byte[] tmp = new byte[4096];
104 ByteBuffer tmp
= ByteBuffer
.allocate(4096);
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;
116 while ((readResult
= channel
.read(tmp
)) >= 0) {
117 out
.write(tmp
.array(), 0, readResult
);
119 transferred
+= readResult
;
120 synchronized (mDataTransferListeners
) {
121 it
= mDataTransferListeners
.iterator();
122 while (it
.hasNext()) {
123 it
.next().onTransferProgress(readResult
, transferred
, size
, mFile
.getAbsolutePath());
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
);