@@ -251,6 +251,7 @@ public AsyncServer getServer() {
251
251
}
252
252
253
253
public static class CacheData implements Parcelable {
254
+ DiskLruCache .Snapshot snapshot ;
254
255
CacheResponse candidate ;
255
256
long contentLength ;
256
257
ResponseHeaders cachedResponseHeaders ;
@@ -278,7 +279,7 @@ public Cancellable getSocket(final GetSocketData data) {
278
279
}
279
280
280
281
String key = uriToKey (data .request .getUri ());
281
- DiskLruCache .Snapshot snapshot ;
282
+ DiskLruCache .Snapshot snapshot = null ;
282
283
Entry entry ;
283
284
try {
284
285
snapshot = cache .get (key );
@@ -299,7 +300,7 @@ public Cancellable getSocket(final GetSocketData data) {
299
300
snapshot .close ();
300
301
return null ;
301
302
}
302
-
303
+
303
304
CacheResponse candidate = entry .isHttps () ? new EntrySecureCacheResponse (entry , snapshot ) : new EntryCacheResponse (entry , snapshot );
304
305
305
306
Map <String , List <String >> responseHeadersMap ;
@@ -310,6 +311,7 @@ public Cancellable getSocket(final GetSocketData data) {
310
311
}
311
312
catch (Exception e ) {
312
313
networkCount ++;
314
+ snapshot .close ();
313
315
return null ;
314
316
}
315
317
if (responseHeadersMap == null || cachedResponseBody == null ) {
@@ -319,6 +321,7 @@ public Cancellable getSocket(final GetSocketData data) {
319
321
catch (Exception e ) {
320
322
}
321
323
networkCount ++;
324
+ snapshot .close ();
322
325
return null ;
323
326
}
324
327
@@ -351,6 +354,7 @@ public void run() {
351
354
else if (responseSource == ResponseSource .CONDITIONAL_CACHE ) {
352
355
data .request .logi ("Response may be served from conditional cache" );
353
356
CacheData cacheData = new CacheData ();
357
+ cacheData .snapshot = snapshot ;
354
358
cacheData .contentLength = contentLength ;
355
359
cacheData .cachedResponseHeaders = cachedResponseHeaders ;
356
360
cacheData .candidate = candidate ;
@@ -367,6 +371,7 @@ else if (responseSource == ResponseSource.CONDITIONAL_CACHE) {
367
371
catch (Exception e ) {
368
372
}
369
373
networkCount ++;
374
+ snapshot .close ();
370
375
return null ;
371
376
}
372
377
}
@@ -567,9 +572,10 @@ public void onBodyDecoder(OnBodyData data) {
567
572
bodySpewer .spew ();
568
573
return ;
569
574
}
570
-
575
+
571
576
// did not validate, so fall through and cache the response
572
577
data .state .remove ("cache-data" );
578
+ cacheData .snapshot .close ();
573
579
}
574
580
575
581
if (!caching )
@@ -623,17 +629,24 @@ public void onBodyDecoder(OnBodyData data) {
623
629
624
630
@ Override
625
631
public void onRequestComplete (OnRequestCompleteData data ) {
626
- BodyCacher cacher = data .state .getParcelable ("body-cacher " );
627
- if (cacher = = null )
628
- return ;
632
+ CacheData cacheData = data .state .getParcelable ("cache-data " );
633
+ if (cacheData != null && cacheData . snapshot ! = null )
634
+ cacheData . snapshot . close () ;
629
635
630
- try {
631
- if (data .exception != null )
632
- cacher .abort ();
633
- else
634
- cacher .commit ();
635
- }
636
- catch (Exception e ) {
636
+ CachedSocket cachedSocket = Util .getWrappedSocket (data .socket , CachedSocket .class );
637
+ if (cachedSocket != null )
638
+ ((SnapshotCacheResponse )cachedSocket .cacheResponse ).getSnapshot ().close ();
639
+
640
+ BodyCacher cacher = data .state .getParcelable ("body-cacher" );
641
+ if (cacher != null ) {
642
+ try {
643
+ if (data .exception != null )
644
+ cacher .abort ();
645
+ else
646
+ cacher .commit ();
647
+ }
648
+ catch (Exception e ) {
649
+ }
637
650
}
638
651
}
639
652
@@ -901,11 +914,20 @@ private static InputStream newBodyInputStream(final DiskLruCache.Snapshot snapsh
901
914
};
902
915
}
903
916
904
- static class EntryCacheResponse extends CacheResponse {
917
+ static interface SnapshotCacheResponse {
918
+ public DiskLruCache .Snapshot getSnapshot ();
919
+ }
920
+
921
+ static class EntryCacheResponse extends CacheResponse implements SnapshotCacheResponse {
905
922
private final Entry entry ;
906
923
private final DiskLruCache .Snapshot snapshot ;
907
924
private final InputStream in ;
908
925
926
+ @ Override
927
+ public DiskLruCache .Snapshot getSnapshot () {
928
+ return snapshot ;
929
+ }
930
+
909
931
public EntryCacheResponse (Entry entry , DiskLruCache .Snapshot snapshot ) {
910
932
this .entry = entry ;
911
933
this .snapshot = snapshot ;
@@ -921,11 +943,17 @@ public EntryCacheResponse(Entry entry, DiskLruCache.Snapshot snapshot) {
921
943
}
922
944
}
923
945
924
- static class EntrySecureCacheResponse extends SecureCacheResponse {
946
+ static class EntrySecureCacheResponse extends SecureCacheResponse implements SnapshotCacheResponse {
925
947
private final Entry entry ;
926
948
private final DiskLruCache .Snapshot snapshot ;
927
949
private final InputStream in ;
928
950
951
+ @ Override
952
+ public DiskLruCache .Snapshot getSnapshot () {
953
+ return snapshot ;
954
+ }
955
+
956
+
929
957
public EntrySecureCacheResponse (Entry entry , DiskLruCache .Snapshot snapshot ) {
930
958
this .entry = entry ;
931
959
this .snapshot = snapshot ;
0 commit comments