1 package eu
.alefzero
.webdav
;
4 import java
.io
.IOException
;
5 import java
.io
.OutputStream
;
6 import java
.io
.RandomAccessFile
;
7 import java
.nio
.ByteBuffer
;
8 import java
.nio
.channels
.FileChannel
;
9 import java
.nio
.channels
.FileLock
;
11 import org
.apache
.commons
.httpclient
.methods
.RequestEntity
;
13 import eu
.alefzero
.webdav
.OnDatatransferProgressListener
;
15 import android
.util
.Log
;
19 * A RequestEntity that represents a File.
22 public class FileRequestEntity
implements RequestEntity
{
25 final String mContentType
;
26 OnDatatransferProgressListener mListener
;
28 public FileRequestEntity(final File file
, final String contentType
) {
31 this.mContentType
= contentType
;
33 throw new IllegalArgumentException("File may not be null");
38 public long getContentLength() {
39 return mFile
.length();
43 public String
getContentType() {
48 public boolean isRepeatable() {
52 public void setOnDatatransferProgressListener(OnDatatransferProgressListener listener
) {
57 public void writeRequest(final OutputStream out
) throws IOException
{
58 //byte[] tmp = new byte[4096];
59 ByteBuffer tmp
= ByteBuffer
.allocate(4096);
62 // TODO(bprzybylski): each mem allocation can throw OutOfMemoryError we need to handle it
63 // globally in some fashionable manner
64 RandomAccessFile raf
= new RandomAccessFile(mFile
, "rw");
65 FileChannel channel
= raf
.getChannel();
66 FileLock lock
= channel
.tryLock();
67 //InputStream instream = new FileInputStream(this.file);
70 //while ((i = instream.read(tmp)) >= 0) {
71 while ((i
= channel
.read(tmp
)) >= 0) {
72 out
.write(tmp
.array(), 0, i
);
74 if (mListener
!= null
)
75 mListener
.transferProgress(i
);
77 } catch (IOException io
) {
78 Log
.e("FileRequestException", io
.getMessage());
79 throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io
);