|
1 /* |
|
2 * ==================================================================== |
|
3 * Licensed to the Apache Software Foundation (ASF) under one |
|
4 * or more contributor license agreements. See the NOTICE file |
|
5 * distributed with this work for additional information |
|
6 * regarding copyright ownership. The ASF licenses this file |
|
7 * to you under the Apache License, Version 2.0 (the |
|
8 * "License"); you may not use this file except in compliance |
|
9 * with the License. You may obtain a copy of the License at |
|
10 * |
|
11 * http://www.apache.org/licenses/LICENSE-2.0 |
|
12 * |
|
13 * Unless required by applicable law or agreed to in writing, |
|
14 * software distributed under the License is distributed on an |
|
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
|
16 * KIND, either express or implied. See the License for the |
|
17 * specific language governing permissions and limitations |
|
18 * under the License. |
|
19 * ==================================================================== |
|
20 * |
|
21 * This software consists of voluntary contributions made by many |
|
22 * individuals on behalf of the Apache Software Foundation. For more |
|
23 * information on the Apache Software Foundation, please see |
|
24 * <http://www.apache.org/>. |
|
25 * |
|
26 */ |
|
27 |
|
28 package ch.boye.httpclientandroidlib.util; |
|
29 |
|
30 import java.io.IOException; |
|
31 import java.io.InputStream; |
|
32 import java.io.InputStreamReader; |
|
33 import java.io.Reader; |
|
34 |
|
35 import ch.boye.httpclientandroidlib.HeaderElement; |
|
36 import ch.boye.httpclientandroidlib.HttpEntity; |
|
37 import ch.boye.httpclientandroidlib.NameValuePair; |
|
38 import ch.boye.httpclientandroidlib.ParseException; |
|
39 import ch.boye.httpclientandroidlib.protocol.HTTP; |
|
40 |
|
41 /** |
|
42 * Static helpers for dealing with {@link HttpEntity}s. |
|
43 * |
|
44 * @since 4.0 |
|
45 */ |
|
46 public final class EntityUtils { |
|
47 |
|
48 private EntityUtils() { |
|
49 } |
|
50 |
|
51 /** |
|
52 * Ensures that the entity content is fully consumed and the content stream, if exists, |
|
53 * is closed. |
|
54 * |
|
55 * @param entity |
|
56 * @throws IOException if an error occurs reading the input stream |
|
57 * |
|
58 * @since 4.1 |
|
59 */ |
|
60 public static void consume(final HttpEntity entity) throws IOException { |
|
61 if (entity == null) { |
|
62 return; |
|
63 } |
|
64 if (entity.isStreaming()) { |
|
65 InputStream instream = entity.getContent(); |
|
66 if (instream != null) { |
|
67 instream.close(); |
|
68 } |
|
69 } |
|
70 } |
|
71 |
|
72 /** |
|
73 * Read the contents of an entity and return it as a byte array. |
|
74 * |
|
75 * @param entity |
|
76 * @return byte array containing the entity content. May be null if |
|
77 * {@link HttpEntity#getContent()} is null. |
|
78 * @throws IOException if an error occurs reading the input stream |
|
79 * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE |
|
80 */ |
|
81 public static byte[] toByteArray(final HttpEntity entity) throws IOException { |
|
82 if (entity == null) { |
|
83 throw new IllegalArgumentException("HTTP entity may not be null"); |
|
84 } |
|
85 InputStream instream = entity.getContent(); |
|
86 if (instream == null) { |
|
87 return null; |
|
88 } |
|
89 try { |
|
90 if (entity.getContentLength() > Integer.MAX_VALUE) { |
|
91 throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); |
|
92 } |
|
93 int i = (int)entity.getContentLength(); |
|
94 if (i < 0) { |
|
95 i = 4096; |
|
96 } |
|
97 ByteArrayBuffer buffer = new ByteArrayBuffer(i); |
|
98 byte[] tmp = new byte[4096]; |
|
99 int l; |
|
100 while((l = instream.read(tmp)) != -1) { |
|
101 buffer.append(tmp, 0, l); |
|
102 } |
|
103 return buffer.toByteArray(); |
|
104 } finally { |
|
105 instream.close(); |
|
106 } |
|
107 } |
|
108 |
|
109 /** |
|
110 * Obtains character set of the entity, if known. |
|
111 * |
|
112 * @param entity must not be null |
|
113 * @return the character set, or null if not found |
|
114 * @throws ParseException if header elements cannot be parsed |
|
115 * @throws IllegalArgumentException if entity is null |
|
116 */ |
|
117 public static String getContentCharSet(final HttpEntity entity) throws ParseException { |
|
118 if (entity == null) { |
|
119 throw new IllegalArgumentException("HTTP entity may not be null"); |
|
120 } |
|
121 String charset = null; |
|
122 if (entity.getContentType() != null) { |
|
123 HeaderElement values[] = entity.getContentType().getElements(); |
|
124 if (values.length > 0) { |
|
125 NameValuePair param = values[0].getParameterByName("charset"); |
|
126 if (param != null) { |
|
127 charset = param.getValue(); |
|
128 } |
|
129 } |
|
130 } |
|
131 return charset; |
|
132 } |
|
133 |
|
134 /** |
|
135 * Obtains mime type of the entity, if known. |
|
136 * |
|
137 * @param entity must not be null |
|
138 * @return the character set, or null if not found |
|
139 * @throws ParseException if header elements cannot be parsed |
|
140 * @throws IllegalArgumentException if entity is null |
|
141 * |
|
142 * @since 4.1 |
|
143 */ |
|
144 public static String getContentMimeType(final HttpEntity entity) throws ParseException { |
|
145 if (entity == null) { |
|
146 throw new IllegalArgumentException("HTTP entity may not be null"); |
|
147 } |
|
148 String mimeType = null; |
|
149 if (entity.getContentType() != null) { |
|
150 HeaderElement values[] = entity.getContentType().getElements(); |
|
151 if (values.length > 0) { |
|
152 mimeType = values[0].getName(); |
|
153 } |
|
154 } |
|
155 return mimeType; |
|
156 } |
|
157 |
|
158 /** |
|
159 * Get the entity content as a String, using the provided default character set |
|
160 * if none is found in the entity. |
|
161 * If defaultCharset is null, the default "ISO-8859-1" is used. |
|
162 * |
|
163 * @param entity must not be null |
|
164 * @param defaultCharset character set to be applied if none found in the entity |
|
165 * @return the entity content as a String. May be null if |
|
166 * {@link HttpEntity#getContent()} is null. |
|
167 * @throws ParseException if header elements cannot be parsed |
|
168 * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE |
|
169 * @throws IOException if an error occurs reading the input stream |
|
170 */ |
|
171 public static String toString( |
|
172 final HttpEntity entity, final String defaultCharset) throws IOException, ParseException { |
|
173 if (entity == null) { |
|
174 throw new IllegalArgumentException("HTTP entity may not be null"); |
|
175 } |
|
176 InputStream instream = entity.getContent(); |
|
177 if (instream == null) { |
|
178 return null; |
|
179 } |
|
180 try { |
|
181 if (entity.getContentLength() > Integer.MAX_VALUE) { |
|
182 throw new IllegalArgumentException("HTTP entity too large to be buffered in memory"); |
|
183 } |
|
184 int i = (int)entity.getContentLength(); |
|
185 if (i < 0) { |
|
186 i = 4096; |
|
187 } |
|
188 String charset = getContentCharSet(entity); |
|
189 if (charset == null) { |
|
190 charset = defaultCharset; |
|
191 } |
|
192 if (charset == null) { |
|
193 charset = HTTP.DEFAULT_CONTENT_CHARSET; |
|
194 } |
|
195 Reader reader = new InputStreamReader(instream, charset); |
|
196 CharArrayBuffer buffer = new CharArrayBuffer(i); |
|
197 char[] tmp = new char[1024]; |
|
198 int l; |
|
199 while((l = reader.read(tmp)) != -1) { |
|
200 buffer.append(tmp, 0, l); |
|
201 } |
|
202 return buffer.toString(); |
|
203 } finally { |
|
204 instream.close(); |
|
205 } |
|
206 } |
|
207 |
|
208 /** |
|
209 * Read the contents of an entity and return it as a String. |
|
210 * The content is converted using the character set from the entity (if any), |
|
211 * failing that, "ISO-8859-1" is used. |
|
212 * |
|
213 * @param entity |
|
214 * @return String containing the content. |
|
215 * @throws ParseException if header elements cannot be parsed |
|
216 * @throws IllegalArgumentException if entity is null or if content length > Integer.MAX_VALUE |
|
217 * @throws IOException if an error occurs reading the input stream |
|
218 */ |
|
219 public static String toString(final HttpEntity entity) |
|
220 throws IOException, ParseException { |
|
221 return toString(entity, null); |
|
222 } |
|
223 |
|
224 } |