1 /* ownCloud Android client application
2 * Copyright (C) 2012-2013 ownCloud Inc.
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
.nio
.ByteBuffer
;
25 import java
.nio
.channels
.FileChannel
;
26 import java
.util
.Collection
;
27 import java
.util
.HashSet
;
28 import java
.util
.Iterator
;
31 import org
.apache
.commons
.httpclient
.methods
.RequestEntity
;
33 import eu
.alefzero
.webdav
.OnDatatransferProgressListener
;
35 import android
.util
.Log
;
39 * A RequestEntity that represents a PIECE of a file.
41 * @author David A. Velasco
43 public class ChunkFromFileChannelRequestEntity
implements RequestEntity
{
45 private static final String TAG
= ChunkFromFileChannelRequestEntity
.class.getSimpleName();
47 //private final File mFile;
48 private final FileChannel mChannel
;
49 private final String mContentType
;
50 private final long mChunkSize
;
51 private final File mFile
;
53 private long mTransferred
;
54 Set
<OnDatatransferProgressListener
> mDataTransferListeners
= new HashSet
<OnDatatransferProgressListener
>();
55 private ByteBuffer mBuffer
= ByteBuffer
.allocate(4096);
57 public ChunkFromFileChannelRequestEntity(final FileChannel channel
, final String contentType
, long chunkSize
, final File file
) {
59 if (channel
== null
) {
60 throw new IllegalArgumentException("File may not be null");
63 throw new IllegalArgumentException("Chunk size must be greater than zero");
66 mContentType
= contentType
;
67 mChunkSize
= chunkSize
;
73 public void setOffset(long offset
) {
77 public long getContentLength() {
79 return Math
.min(mChunkSize
, mChannel
.size() - mChannel
.position());
80 } catch (IOException e
) {
85 public String
getContentType() {
89 public boolean isRepeatable() {
93 public void addOnDatatransferProgressListener(OnDatatransferProgressListener listener
) {
94 mDataTransferListeners
.add(listener
);
97 public void addOnDatatransferProgressListeners(Collection
<OnDatatransferProgressListener
> listeners
) {
98 mDataTransferListeners
.addAll(listeners
);
101 public void removeOnDatatransferProgressListener(OnDatatransferProgressListener listener
) {
102 mDataTransferListeners
.remove(listener
);
106 public void writeRequest(final OutputStream out
) throws IOException
{
108 Iterator
<OnDatatransferProgressListener
> it
= null
;
111 mChannel
.position(mOffset
);
112 long size
= mFile
.length();
113 if (size
== 0) size
= -1;
114 while (mChannel
.position() < mOffset
+ mChunkSize
&& mChannel
.position() < mChannel
.size()) {
115 readCount
= mChannel
.read(mBuffer
);
116 out
.write(mBuffer
.array(), 0, readCount
);
118 mTransferred
+= readCount
;
119 it
= mDataTransferListeners
.iterator();
120 while (it
.hasNext()) {
121 it
.next().onTransferProgress(readCount
, mTransferred
, size
, mFile
.getName());
125 } catch (IOException io
) {
126 Log
.e(TAG
, io
.getMessage());
127 throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io
);