1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.http.entity;
29
30 import java.io.File;
31 import java.io.FileInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.OutputStream;
35
36 import org.apache.http.util.Args;
37
38
39
40
41
42
43 public class FileEntity extends AbstractHttpEntity implements Cloneable {
44
45 protected final File file;
46
47
48
49
50
51
52
53
54
55 @Deprecated
56 public FileEntity(final File file, final String contentType) {
57 super();
58 this.file = Args.notNull(file, "File");
59 setContentType(contentType);
60 }
61
62
63
64
65
66
67
68
69
70 public FileEntity(final File file, final ContentType contentType) {
71 super();
72 this.file = Args.notNull(file, "File");
73 if (contentType != null) {
74 setContentType(contentType.toString());
75 }
76 }
77
78
79
80
81
82
83
84
85 public FileEntity(final File file) {
86 super();
87 this.file = Args.notNull(file, "File");
88 }
89
90 @Override
91 public boolean isRepeatable() {
92 return true;
93 }
94
95 @Override
96 public long getContentLength() {
97 return this.file.length();
98 }
99
100 @Override
101 public InputStream getContent() throws IOException {
102 return new FileInputStream(this.file);
103 }
104
105 @Override
106 public void writeTo(final OutputStream outStream) throws IOException {
107 Args.notNull(outStream, "Output stream");
108 final InputStream inStream = new FileInputStream(this.file);
109 try {
110 final byte[] tmp = new byte[OUTPUT_BUFFER_SIZE];
111 int l;
112 while ((l = inStream.read(tmp)) != -1) {
113 outStream.write(tmp, 0, l);
114 }
115 outStream.flush();
116 } finally {
117 inStream.close();
118 }
119 }
120
121
122
123
124
125
126 @Override
127 public boolean isStreaming() {
128 return false;
129 }
130
131 @Override
132 public Object clone() throws CloneNotSupportedException {
133
134
135 return super.clone();
136 }
137
138 }