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 2 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 com
.owncloud
.android
.network
.ProgressiveDataTransferer
; 
  35 import eu
.alefzero
.webdav
.OnDatatransferProgressListener
; 
  37 import android
.util
.Log
; 
  41  * A RequestEntity that represents a PIECE of a file. 
  43  * @author David A. Velasco 
  45 public class ChunkFromFileChannelRequestEntity 
implements RequestEntity
, ProgressiveDataTransferer 
{ 
  47     private static final String TAG 
= ChunkFromFileChannelRequestEntity
.class.getSimpleName(); 
  49     //private final File mFile; 
  50     private final FileChannel mChannel
; 
  51     private final String mContentType
; 
  52     private final long mChunkSize
; 
  53     private final File mFile
; 
  55     private long mTransferred
; 
  56     Set
<OnDatatransferProgressListener
> mDataTransferListeners 
= new HashSet
<OnDatatransferProgressListener
>(); 
  57     private ByteBuffer mBuffer 
= ByteBuffer
.allocate(4096); 
  59     public ChunkFromFileChannelRequestEntity(final FileChannel channel
, final String contentType
, long chunkSize
, final File file
) { 
  61         if (channel 
== null
) { 
  62             throw new IllegalArgumentException("File may not be null"); 
  65             throw new IllegalArgumentException("Chunk size must be greater than zero"); 
  68         mContentType 
= contentType
; 
  69         mChunkSize 
= chunkSize
; 
  75     public void setOffset(long offset
) { 
  79     public long getContentLength() { 
  81             return Math
.min(mChunkSize
, mChannel
.size() - mChannel
.position()); 
  82         } catch (IOException e
) { 
  87     public String 
getContentType() { 
  91     public boolean isRepeatable() { 
  96     public void addDatatransferProgressListener(OnDatatransferProgressListener listener
) { 
  97         synchronized (mDataTransferListeners
) { 
  98             mDataTransferListeners
.add(listener
); 
 103     public void addDatatransferProgressListeners(Collection
<OnDatatransferProgressListener
> listeners
) { 
 104         synchronized (mDataTransferListeners
) { 
 105             mDataTransferListeners
.addAll(listeners
); 
 110     public void removeDatatransferProgressListener(OnDatatransferProgressListener listener
) { 
 111         synchronized (mDataTransferListeners
) { 
 112             mDataTransferListeners
.remove(listener
); 
 117     public void writeRequest(final OutputStream out
) throws IOException 
{ 
 119         Iterator
<OnDatatransferProgressListener
> it 
= null
; 
 122             mChannel
.position(mOffset
); 
 123             long size 
= mFile
.length(); 
 124             if (size 
== 0) size 
= -1; 
 125             while (mChannel
.position() < mOffset 
+ mChunkSize 
&& mChannel
.position() < mChannel
.size()) { 
 126                 readCount 
= mChannel
.read(mBuffer
); 
 127                 out
.write(mBuffer
.array(), 0, readCount
); 
 129                 mTransferred 
+= readCount
; 
 130                 synchronized (mDataTransferListeners
) { 
 131                     it 
= mDataTransferListeners
.iterator(); 
 132                     while (it
.hasNext()) { 
 133                         it
.next().onTransferProgress(readCount
, mTransferred
, size
, mFile
.getName()); 
 138         } catch (IOException io
) { 
 139             Log
.e(TAG
, io
.getMessage()); 
 140             throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io
);