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.nio.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 import java.nio.channels.FileChannel;
36
37 import org.apache.http.annotation.NotThreadSafe;
38 import org.apache.http.entity.AbstractHttpEntity;
39 import org.apache.http.entity.ContentType;
40 import org.apache.http.nio.ContentEncoder;
41 import org.apache.http.nio.ContentEncoderChannel;
42 import org.apache.http.nio.FileContentEncoder;
43 import org.apache.http.nio.IOControl;
44
45
46
47
48
49
50
51
52
53 @SuppressWarnings("deprecation")
54 @NotThreadSafe
55 public class NFileEntity extends AbstractHttpEntity
56 implements HttpAsyncContentProducer, ProducingNHttpEntity {
57
58 private final File file;
59 private FileChannel fileChannel;
60 private long idx = -1;
61 private boolean useFileChannels;
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 public NFileEntity(final File file, final ContentType contentType, boolean useFileChannels) {
77 if (file == null) {
78 throw new IllegalArgumentException("File may not be null");
79 }
80 this.file = file;
81 this.useFileChannels = useFileChannels;
82 if (contentType != null) {
83 setContentType(contentType.toString());
84 }
85 }
86
87
88
89
90 public NFileEntity(final File file) {
91 if (file == null) {
92 throw new IllegalArgumentException("File may not be null");
93 }
94 this.file = file;
95 }
96
97
98
99
100
101
102
103
104
105 public NFileEntity(final File file, final ContentType contentType) {
106 this(file, contentType, true);
107 }
108
109
110
111
112 @Deprecated
113 public NFileEntity(final File file, final String contentType, boolean useFileChannels) {
114 if (file == null) {
115 throw new IllegalArgumentException("File may not be null");
116 }
117 this.file = file;
118 this.useFileChannels = useFileChannels;
119 setContentType(contentType);
120 }
121
122
123
124
125 @Deprecated
126 public NFileEntity(final File file, final String contentType) {
127 this(file, contentType, true);
128 }
129
130
131
132
133
134
135 public void close() throws IOException {
136 FileChannel local = fileChannel;
137 fileChannel = null;
138 if (local != null) {
139 local.close();
140 }
141 }
142
143
144
145
146
147
148 public void finish() throws IOException {
149 close();
150 }
151
152 public long getContentLength() {
153 return file.length();
154 }
155
156 public boolean isRepeatable() {
157 return true;
158 }
159
160 public void produceContent(ContentEncoder encoder, IOControl ioctrl)
161 throws IOException {
162 if (fileChannel == null) {
163 FileInputStream in = new FileInputStream(file);
164 fileChannel = in.getChannel();
165 idx = 0;
166 }
167
168 long transferred;
169 if (useFileChannels && encoder instanceof FileContentEncoder) {
170 transferred = ((FileContentEncoder)encoder)
171 .transfer(fileChannel, idx, Long.MAX_VALUE);
172 } else {
173 transferred = fileChannel.
174 transferTo(idx, Long.MAX_VALUE, new ContentEncoderChannel(encoder));
175 }
176 if (transferred > 0) {
177 idx += transferred;
178 }
179 if (idx >= fileChannel.size()) {
180 encoder.complete();
181 close();
182 }
183 }
184
185 public boolean isStreaming() {
186 return false;
187 }
188
189 public InputStream getContent() throws IOException {
190 return new FileInputStream(this.file);
191 }
192
193 public void writeTo(final OutputStream outstream) throws IOException {
194 if (outstream == null) {
195 throw new IllegalArgumentException("Output stream may not be null");
196 }
197 InputStream instream = new FileInputStream(this.file);
198 try {
199 byte[] tmp = new byte[4096];
200 int l;
201 while ((l = instream.read(tmp)) != -1) {
202 outstream.write(tmp, 0, l);
203 }
204 outstream.flush();
205 } finally {
206 instream.close();
207 }
208 }
209
210 }