1010 *******************************************************************************/
1111package org .springframework .ide .vscode .boot .index .cache ;
1212
13+ import java .io .BufferedReader ;
14+ import java .io .BufferedWriter ;
1315import java .io .File ;
1416import java .io .FileReader ;
1517import java .io .FileWriter ;
1618import java .io .IOException ;
19+ import java .io .Writer ;
1720import java .lang .reflect .Array ;
1821import java .lang .reflect .Type ;
1922import java .nio .file .Files ;
5962import com .google .gson .JsonPrimitive ;
6063import com .google .gson .JsonSerializationContext ;
6164import com .google .gson .JsonSerializer ;
65+ import com .google .gson .Strictness ;
6266import com .google .gson .reflect .TypeToken ;
6367import com .google .gson .stream .JsonReader ;
68+ import com .google .gson .stream .JsonToken ;
6469
6570/**
6671 * @author Martin Lippert
@@ -119,45 +124,40 @@ public <T extends IndexCacheable> Pair<T[], Multimap<String, String>> retrieve(I
119124 File cacheStore = new File (cacheDirectory , cacheKey .toString () + ".json" );
120125 if (cacheStore .exists ()) {
121126
122- try (JsonReader reader = new JsonReader (new FileReader (cacheStore ))) {
123- IndexCacheStore <T > store = retrieveStoreFromIncrementalStorage (cacheKey , type );
127+ IndexCacheStore <T > store = retrieveStoreFromIncrementalStorage (cacheKey , type );
124128
125- SortedMap <String , Long > timestampedFiles = Arrays .stream (files )
126- .filter (file -> new File (file ).exists ())
127- .collect (Collectors .toMap (file -> file , file -> {
128- try {
129- return Files .getLastModifiedTime (new File (file ).toPath ()).toMillis ();
130- } catch (IOException e ) {
131- throw new RuntimeException (e );
132- }
133- }, (v1 ,v2 ) -> { throw new RuntimeException (String .format ("Duplicate key for values %s and %s" , v1 , v2 ));}, TreeMap ::new ));
129+ SortedMap <String , Long > timestampedFiles = Arrays .stream (files )
130+ .filter (file -> new File (file ).exists ())
131+ .collect (Collectors .toMap (file -> file , file -> {
132+ try {
133+ return Files .getLastModifiedTime (new File (file ).toPath ()).toMillis ();
134+ } catch (IOException e ) {
135+ throw new RuntimeException (e );
136+ }
137+ }, (v1 ,v2 ) -> { throw new RuntimeException (String .format ("Duplicate key for values %s and %s" , v1 , v2 ));}, TreeMap ::new ));
134138
135- if (isFileMatch (timestampedFiles , store .getTimestampedFiles ())) {
139+ if (isFileMatch (timestampedFiles , store .getTimestampedFiles ())) {
136140
137- List <T > symbols = store .getSymbols ();
141+ List <T > symbols = store .getSymbols ();
138142
139- Map <String , Collection <String >> storedDependencies = store .getDependencies ();
140- Multimap <String , String > dependencies = MultimapBuilder .hashKeys ().hashSetValues ().build ();
143+ Map <String , Collection <String >> storedDependencies = store .getDependencies ();
144+ Multimap <String , String > dependencies = MultimapBuilder .hashKeys ().hashSetValues ().build ();
141145
142- if (storedDependencies !=null && !storedDependencies .isEmpty ()) {
143- for (Entry <String , Collection <String >> entry : storedDependencies .entrySet ()) {
144- dependencies .replaceValues (entry .getKey (), entry .getValue ());
145- }
146+ if (storedDependencies !=null && !storedDependencies .isEmpty ()) {
147+ for (Entry <String , Collection <String >> entry : storedDependencies .entrySet ()) {
148+ dependencies .replaceValues (entry .getKey (), entry .getValue ());
146149 }
147-
148- // update local timestamp cache
149- ConcurrentMap <InternalFileIdentifier , Long > timestampMap = timestampedFiles .entrySet ().stream ()
150- .collect (Collectors .toConcurrentMap (e -> InternalFileIdentifier .fromPath (e .getKey ()), e -> e .getValue ()));
151- this .timestamps .put (cacheKey , timestampMap );
152-
153- return Pair .of (
154- (T []) symbols .toArray ((T []) Array .newInstance (type , symbols .size ())),
155- MultimapBuilder .hashKeys ().hashSetValues ().build (dependencies )
156- );
157150 }
158- }
159- catch (Exception e ) {
160- log .error ("error reading cached symbols" , e );
151+
152+ // update local timestamp cache
153+ ConcurrentMap <InternalFileIdentifier , Long > timestampMap = timestampedFiles .entrySet ().stream ()
154+ .collect (Collectors .toConcurrentMap (e -> InternalFileIdentifier .fromPath (e .getKey ()), e -> e .getValue ()));
155+ this .timestamps .put (cacheKey , timestampMap );
156+
157+ return Pair .of (
158+ (T []) symbols .toArray ((T []) Array .newInstance (type , symbols .size ())),
159+ MultimapBuilder .hashKeys ().hashSetValues ().build (dependencies )
160+ );
161161 }
162162 }
163163 return null ;
@@ -292,7 +292,7 @@ else if (key != null && !key.equals(cacheKey)
292292 private <T extends IndexCacheable > void persist (IndexCacheKey cacheKey , DeltaElement <T > delta , boolean append ) {
293293 DeltaStorage <T > deltaStorage = new DeltaStorage <T >(delta );
294294
295- try (FileWriter writer = new FileWriter (new File (cacheDirectory , cacheKey .toString () + ".json" ), append ))
295+ try (Writer writer = new BufferedWriter ( new FileWriter (new File (cacheDirectory , cacheKey .toString () + ".json" ), append ) ))
296296 {
297297 Gson gson = createGson ();
298298 gson .toJson (deltaStorage , writer );
@@ -313,15 +313,14 @@ private <T extends IndexCacheable> IndexCacheStore<T> retrieveStoreFromIncrement
313313 if (cacheStore .exists ()) {
314314
315315 Gson gson = createGson ();
316-
317316
318- try (JsonReader reader = new JsonReader (new FileReader (cacheStore ))) {
319-
320- DeltaStorage <T > readElement ;
321- while ((readElement = gson .fromJson (reader , DeltaStorage .class )) != null ) {
322- DeltaElement <T > delta = readElement .storedElement ;
323- store = delta .apply (store );
317+ try (JsonReader reader = new JsonReader (new BufferedReader (new FileReader (cacheStore )))) {
318+ reader .setStrictness (Strictness .LENIENT );
319+ while (reader .peek () != JsonToken .END_DOCUMENT ) {
320+ DeltaStorage <T > delta = gson .fromJson (reader , DeltaStorage .class );
321+ store = delta .storedElement .apply (store );
324322 }
323+
325324 }
326325 catch (Exception e ) {
327326 log .error ("error reading cached symbols" , e );
0 commit comments