e466aa1537ba780301b4f06428f9311f019596b3
[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 /**
12 * A RequestEntity that represents a File.
13 *
14 */
15 public class FileRequestEntity implements RequestEntity {
16
17 final File file;
18 final String contentType;
19 OnUploadProgressListener listener;
20
21 public FileRequestEntity(final File file, final String contentType) {
22 super();
23 if (file == null) {
24 throw new IllegalArgumentException("File may not be null");
25 }
26 this.file = file;
27 this.contentType = contentType;
28 }
29
30 public long getContentLength() {
31 return this.file.length();
32 }
33
34 public String getContentType() {
35 return this.contentType;
36 }
37
38 public boolean isRepeatable() {
39 return true;
40 }
41
42 public void setOnUploadProgressListener(OnUploadProgressListener listener) {
43 this.listener = listener;
44 }
45
46 public void writeRequest(final OutputStream out) throws IOException {
47 byte[] tmp = new byte[4096];
48 int i = 0;
49 InputStream instream = new FileInputStream(this.file);
50 try {
51 while ((i = instream.read(tmp)) >= 0) {
52 out.write(tmp, 0, i);
53 if (listener != null)
54 listener.OnUploadProgress(i);
55 }
56 } finally {
57 instream.close();
58 }
59 }
60
61 }