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 contentType
;
26 OnDatatransferProgressListener listener
;
28 public FileRequestEntity(final File file
, final String contentType
) {
31 throw new IllegalArgumentException("File may not be null");
34 this.contentType
= contentType
;
37 public long getContentLength() {
38 return this.file
.length();
41 public String
getContentType() {
42 return this.contentType
;
45 public boolean isRepeatable() {
49 public void setOnDatatransferProgressListener(OnDatatransferProgressListener listener
) {
50 this.listener
= listener
;
53 public void writeRequest(final OutputStream out
) throws IOException
{
54 //byte[] tmp = new byte[4096];
55 ByteBuffer tmp
= ByteBuffer
.allocate(4096);
58 FileChannel channel
= new RandomAccessFile(this.file
, "rw").getChannel();
59 FileLock lock
= channel
.tryLock();
60 //InputStream instream = new FileInputStream(this.file);
63 //while ((i = instream.read(tmp)) >= 0) {
64 while ((i
= channel
.read(tmp
)) >= 0) {
65 out
.write(tmp
.array(), 0, i
);
68 listener
.transferProgress(i
);
70 } catch (IOException io
) {
71 Log
.e("FileRequestException", io
.getMessage());
72 throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io
);