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.util;
29
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.InputStreamReader;
33 import java.io.Reader;
34 import java.io.UnsupportedEncodingException;
35 import java.nio.charset.Charset;
36 import java.nio.charset.UnsupportedCharsetException;
37
38 import org.apache.http.HeaderElement;
39 import org.apache.http.HttpEntity;
40 import org.apache.http.HttpResponse;
41 import org.apache.http.NameValuePair;
42 import org.apache.http.ParseException;
43 import org.apache.http.entity.ContentType;
44 import org.apache.http.protocol.HTTP;
45
46
47
48
49
50
51 public final class EntityUtils {
52
53 private static final int DEFAULT_BUFFER_SIZE = 4096;
54
55 private EntityUtils() {
56 }
57
58
59
60
61
62
63
64
65
66
67 public static void consumeQuietly(final HttpEntity entity) {
68 try {
69 consume(entity);
70 } catch (final IOException ignore) {
71 }
72 }
73
74
75
76
77
78
79
80
81
82
83 public static void consume(final HttpEntity entity) throws IOException {
84 if (entity == null) {
85 return;
86 }
87 if (entity.isStreaming()) {
88 final InputStream inStream = entity.getContent();
89 if (inStream != null) {
90 inStream.close();
91 }
92 }
93 }
94
95
96
97
98
99
100
101
102
103
104
105
106 public static void updateEntity(
107 final HttpResponse response, final HttpEntity entity) throws IOException {
108 Args.notNull(response, "Response");
109 consume(response.getEntity());
110 response.setEntity(entity);
111 }
112
113
114
115
116
117
118
119
120
121
122 public static byte[] toByteArray(final HttpEntity entity) throws IOException {
123 Args.notNull(entity, "Entity");
124 final InputStream inStream = entity.getContent();
125 if (inStream == null) {
126 return null;
127 }
128 try {
129 Args.check(entity.getContentLength() <= Integer.MAX_VALUE,
130 "HTTP entity too large to be buffered in memory");
131 int capacity = (int)entity.getContentLength();
132 if (capacity < 0) {
133 capacity = DEFAULT_BUFFER_SIZE;
134 }
135 final ByteArrayBufferhtml#ByteArrayBuffer">ByteArrayBuffer buffer = new ByteArrayBuffer(capacity);
136 final byte[] tmp = new byte[DEFAULT_BUFFER_SIZE];
137 int l;
138 while((l = inStream.read(tmp)) != -1) {
139 buffer.append(tmp, 0, l);
140 }
141 return buffer.toByteArray();
142 } finally {
143 inStream.close();
144 }
145 }
146
147
148
149
150
151
152
153
154
155
156
157 @Deprecated
158 public static String getContentCharSet(final HttpEntity entity) throws ParseException {
159 Args.notNull(entity, "Entity");
160 String charset = null;
161 if (entity.getContentType() != null) {
162 final HeaderElement values[] = entity.getContentType().getElements();
163 if (values.length > 0) {
164 final NameValuePair param = values[0].getParameterByName("charset");
165 if (param != null) {
166 charset = param.getValue();
167 }
168 }
169 }
170 return charset;
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184
185 @Deprecated
186 public static String getContentMimeType(final HttpEntity entity) throws ParseException {
187 Args.notNull(entity, "Entity");
188 String mimeType = null;
189 if (entity.getContentType() != null) {
190 final HeaderElement values[] = entity.getContentType().getElements();
191 if (values.length > 0) {
192 mimeType = values[0].getName();
193 }
194 }
195 return mimeType;
196 }
197
198 private static String toString(
199 final HttpEntity entity,
200 final ContentType contentType) throws IOException {
201 final InputStream inStream = entity.getContent();
202 if (inStream == null) {
203 return null;
204 }
205 try {
206 Args.check(entity.getContentLength() <= Integer.MAX_VALUE,
207 "HTTP entity too large to be buffered in memory");
208 int capacity = (int)entity.getContentLength();
209 if (capacity < 0) {
210 capacity = DEFAULT_BUFFER_SIZE;
211 }
212 Charset charset = null;
213 if (contentType != null) {
214 charset = contentType.getCharset();
215 if (charset == null) {
216 final ContentType defaultContentType = ContentType.getByMimeType(contentType.getMimeType());
217 charset = defaultContentType != null ? defaultContentType.getCharset() : null;
218 }
219 }
220 if (charset == null) {
221 charset = HTTP.DEF_CONTENT_CHARSET;
222 }
223 final Reader reader = new InputStreamReader(inStream, charset);
224 final CharArrayBufferhtml#CharArrayBuffer">CharArrayBuffer buffer = new CharArrayBuffer(capacity);
225 final char[] tmp = new char[1024];
226 int l;
227 while((l = reader.read(tmp)) != -1) {
228 buffer.append(tmp, 0, l);
229 }
230 return buffer.toString();
231 } finally {
232 inStream.close();
233 }
234 }
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252 public static String toString(
253 final HttpEntity entity, final Charset defaultCharset) throws IOException, ParseException {
254 Args.notNull(entity, "Entity");
255 ContentType contentType = null;
256 try {
257 contentType = ContentType.get(entity);
258 } catch (final UnsupportedCharsetException ex) {
259 if (defaultCharset == null) {
260 throw new UnsupportedEncodingException(ex.getMessage());
261 }
262 }
263 if (contentType != null) {
264 if (contentType.getCharset() == null) {
265 contentType = contentType.withCharset(defaultCharset);
266 }
267 } else {
268 contentType = ContentType.DEFAULT_TEXT.withCharset(defaultCharset);
269 }
270 return toString(entity, contentType);
271 }
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288 public static String toString(
289 final HttpEntity entity, final String defaultCharset) throws IOException, ParseException {
290 return toString(entity, defaultCharset != null ? Charset.forName(defaultCharset) : null);
291 }
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306 public static String toString(final HttpEntity entity) throws IOException, ParseException {
307 Args.notNull(entity, "Entity");
308 return toString(entity, ContentType.get(entity));
309 }
310
311 }