3e7977c4ef535cf2a01d1b617bcb99e2f7e66963
[pub/Android/ownCloud.git] / src / eu / alefzero / webdav / FileRequestEntity.java
1 package eu.alefzero.webdav;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.OutputStream;
8
9 import org.apache.commons.httpclient.methods.RequestEntity;
10
11 import android.util.Log;
12
13 import eu.alefzero.owncloud.files.interfaces.OnDatatransferProgressListener;
14
15 /**
16 * A RequestEntity that represents a File.
17 *
18 */
19 public class FileRequestEntity implements RequestEntity {
20
21 final File file;
22 final String contentType;
23 OnDatatransferProgressListener listener;
24
25 public FileRequestEntity(final File file, final String contentType) {
26 super();
27 if (file == null) {
28 throw new IllegalArgumentException("File may not be null");
29 }
30 this.file = file;
31 this.contentType = contentType;
32 }
33
34 public long getContentLength() {
35 return this.file.length();
36 }
37
38 public String getContentType() {
39 return this.contentType;
40 }
41
42 public boolean isRepeatable() {
43 return true;
44 }
45
46 public void setOnDatatransferProgressListener(OnDatatransferProgressListener listener) {
47 this.listener = listener;
48 }
49
50 public void writeRequest(final OutputStream out) throws IOException {
51 byte[] tmp = new byte[4096];
52 int i = 0;
53 InputStream instream = new FileInputStream(this.file);
54 try {
55 while ((i = instream.read(tmp)) >= 0) {
56 out.write(tmp, 0, i);
57 if (listener != null)
58 listener.transferProgress(i);
59 }
60 } catch (IOException io) {
61 Log.e("FileRequestException", io.getMessage());
62 throw new RuntimeException("Ugly solution to workaround the default policy of retries when the server falls while uploading ; temporal fix; really", io);
63
64 } finally {
65 instream.close();
66 }
67 }
68
69 }