1 package eu
.alefzero
.webdav
;
4 import java
.io
.FileInputStream
;
5 import java
.io
.IOException
;
6 import java
.io
.InputStream
;
7 import java
.io
.OutputStream
;
8 import java
.io
.RandomAccessFile
;
9 import java
.nio
.ByteBuffer
;
10 import java
.nio
.channels
.FileChannel
;
11 import java
.nio
.channels
.FileLock
;
12 import java
.nio
.channels
.OverlappingFileLockException
;
14 import org
.apache
.commons
.httpclient
.methods
.RequestEntity
;
16 import eu
.alefzero
.webdav
.OnDatatransferProgressListener
;
18 import android
.util
.Log
;
22 * A RequestEntity that represents a File.
25 public class FileRequestEntity
implements RequestEntity
{
28 final String contentType
;
29 OnDatatransferProgressListener listener
;
31 public FileRequestEntity(final File file
, final String contentType
) {
34 throw new IllegalArgumentException("File may not be null");
37 this.contentType
= contentType
;
40 public long getContentLength() {
41 return this.file
.length();
44 public String
getContentType() {
45 return this.contentType
;
48 public boolean isRepeatable() {
52 public void setOnDatatransferProgressListener(OnDatatransferProgressListener listener
) {
53 this.listener
= listener
;
56 public void writeRequest(final OutputStream out
) throws IOException
{
57 //byte[] tmp = new byte[4096];
58 ByteBuffer tmp
= ByteBuffer
.allocate(4096);
61 FileChannel channel
= new RandomAccessFile(this.file
, "rw").getChannel();
62 FileLock lock
= channel
.tryLock();
63 //InputStream instream = new FileInputStream(this.file);
66 //while ((i = instream.read(tmp)) >= 0) {
67 while ((i
= channel
.read(tmp
)) >= 0) {
68 out
.write(tmp
.array(), 0, i
);
71 listener
.transferProgress(i
);
73 } catch (IOException io
) {
74 Log
.e("FileRequestException", io
.getMessage());
75 throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io
);