|
73 | 73 | import java.io.InputStream;
|
74 | 74 | import java.io.OutputStream;
|
75 | 75 | import java.io.PushbackInputStream;
|
| 76 | +import java.lang.reflect.Field; |
76 | 77 | import java.net.URI;
|
77 | 78 | import java.util.Collections;
|
78 | 79 | import java.util.HashMap;
|
@@ -1262,27 +1263,72 @@ private HttpEntityEnclosingRequestBase addEntityToRequestBase(HttpEntityEnclosin
|
1262 | 1263 | return requestBase;
|
1263 | 1264 | }
|
1264 | 1265 |
|
| 1266 | + /** |
| 1267 | + * This horrible hack is required on Android, due to implementation of BasicManagedEntity, which |
| 1268 | + * doesn't chain call consumeContent on underlying wrapped HttpEntity |
| 1269 | + * |
| 1270 | + * @param entity HttpEntity, may be null |
| 1271 | + */ |
| 1272 | + public static void endEntityViaReflection(HttpEntity entity) { |
| 1273 | + if (entity instanceof HttpEntityWrapper) { |
| 1274 | + try { |
| 1275 | + Field f = null; |
| 1276 | + Field[] fields = HttpEntityWrapper.class.getDeclaredFields(); |
| 1277 | + for (Field ff : fields) { |
| 1278 | + if (ff.getName().equals("wrappedEntity")) { |
| 1279 | + f = ff; |
| 1280 | + break; |
| 1281 | + } |
| 1282 | + } |
| 1283 | + if (f != null) { |
| 1284 | + f.setAccessible(true); |
| 1285 | + HttpEntity wrapped = (HttpEntity) f.get(entity); |
| 1286 | + if (wrapped != null) { |
| 1287 | + wrapped.consumeContent(); |
| 1288 | + } |
| 1289 | + } |
| 1290 | + } catch (Throwable t) { |
| 1291 | + Log.e(LOG_TAG, "wrappedEntity consume", t); |
| 1292 | + } |
| 1293 | + } |
| 1294 | + } |
| 1295 | + |
1265 | 1296 | /**
|
1266 | 1297 | * Enclosing entity to hold stream of gzip decoded data for accessing HttpEntity contents
|
1267 | 1298 | */
|
1268 | 1299 | private static class InflatingEntity extends HttpEntityWrapper {
|
| 1300 | + |
1269 | 1301 | public InflatingEntity(HttpEntity wrapped) {
|
1270 | 1302 | super(wrapped);
|
1271 | 1303 | }
|
1272 | 1304 |
|
| 1305 | + InputStream wrappedStream; |
| 1306 | + PushbackInputStream pushbackStream; |
| 1307 | + GZIPInputStream gzippedStream; |
| 1308 | + |
1273 | 1309 | @Override
|
1274 | 1310 | public InputStream getContent() throws IOException {
|
1275 |
| - PushbackInputStream content = new PushbackInputStream(wrappedEntity.getContent(), 2); |
1276 |
| - if (isInputStreamGZIPCompressed(content)) { |
1277 |
| - return new GZIPInputStream(content); |
| 1311 | + wrappedStream = wrappedEntity.getContent(); |
| 1312 | + pushbackStream = new PushbackInputStream(wrappedStream, 2); |
| 1313 | + if (isInputStreamGZIPCompressed(pushbackStream)) { |
| 1314 | + gzippedStream = new GZIPInputStream(pushbackStream); |
| 1315 | + return gzippedStream; |
1278 | 1316 | } else {
|
1279 |
| - return content; |
| 1317 | + return pushbackStream; |
1280 | 1318 | }
|
1281 | 1319 | }
|
1282 | 1320 |
|
1283 | 1321 | @Override
|
1284 | 1322 | public long getContentLength() {
|
1285 | 1323 | return -1;
|
1286 | 1324 | }
|
| 1325 | + |
| 1326 | + @Override |
| 1327 | + public void consumeContent() throws IOException { |
| 1328 | + AsyncHttpClient.silentCloseInputStream(wrappedStream); |
| 1329 | + AsyncHttpClient.silentCloseInputStream(pushbackStream); |
| 1330 | + AsyncHttpClient.silentCloseInputStream(gzippedStream); |
| 1331 | + super.consumeContent(); |
| 1332 | + } |
1287 | 1333 | }
|
1288 | 1334 | }
|
0 commit comments