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