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 version 2, 
   6  *   as published by the Free Software Foundation. 
   8  *   This program is distributed in the hope that it will be useful, 
   9  *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  10  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  11  *   GNU General Public License for more details. 
  13  *   You should have received a copy of the GNU General Public License 
  14  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. 
  18 package eu
.alefzero
.webdav
; 
  21 import java
.io
.IOException
; 
  22 import java
.io
.OutputStream
; 
  23 import java
.nio
.ByteBuffer
; 
  24 import java
.nio
.channels
.FileChannel
; 
  25 import java
.util
.Collection
; 
  26 import java
.util
.HashSet
; 
  27 import java
.util
.Iterator
; 
  30 import org
.apache
.commons
.httpclient
.methods
.RequestEntity
; 
  32 import com
.owncloud
.android
.Log_OC
; 
  33 import com
.owncloud
.android
.network
.ProgressiveDataTransferer
; 
  35 import eu
.alefzero
.webdav
.OnDatatransferProgressListener
; 
  39  * A RequestEntity that represents a PIECE of a file. 
  41  * @author David A. Velasco 
  43 public class ChunkFromFileChannelRequestEntity 
implements RequestEntity
, ProgressiveDataTransferer 
{ 
  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() { 
  94     public void addDatatransferProgressListener(OnDatatransferProgressListener listener
) { 
  95         synchronized (mDataTransferListeners
) { 
  96             mDataTransferListeners
.add(listener
); 
 101     public void addDatatransferProgressListeners(Collection
<OnDatatransferProgressListener
> listeners
) { 
 102         synchronized (mDataTransferListeners
) { 
 103             mDataTransferListeners
.addAll(listeners
); 
 108     public void removeDatatransferProgressListener(OnDatatransferProgressListener listener
) { 
 109         synchronized (mDataTransferListeners
) { 
 110             mDataTransferListeners
.remove(listener
); 
 115     public void writeRequest(final OutputStream out
) throws IOException 
{ 
 117         Iterator
<OnDatatransferProgressListener
> it 
= null
; 
 120             mChannel
.position(mOffset
); 
 121             long size 
= mFile
.length(); 
 122             if (size 
== 0) size 
= -1; 
 123             while (mChannel
.position() < mOffset 
+ mChunkSize 
&& mChannel
.position() < mChannel
.size()) { 
 124                 readCount 
= mChannel
.read(mBuffer
); 
 125                 out
.write(mBuffer
.array(), 0, readCount
); 
 127                 mTransferred 
+= readCount
; 
 128                 synchronized (mDataTransferListeners
) { 
 129                     it 
= mDataTransferListeners
.iterator(); 
 130                     while (it
.hasNext()) { 
 131                         it
.next().onTransferProgress(readCount
, mTransferred
, size
, mFile
.getName()); 
 136         } catch (IOException io
) { 
 137             Log_OC
.e(TAG
, io
.getMessage()); 
 138             throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io
);