@@ -356,13 +356,6 @@ public void skip() throws IOException {
356356 IterImplSkip .skip (this );
357357 }
358358
359- private static ThreadLocal <JsonIterator > tlsIter = new ThreadLocal <JsonIterator >() {
360- @ Override
361- protected JsonIterator initialValue () {
362- return new JsonIterator ();
363- }
364- };
365-
366359 public static final <T > T deserialize (Config config , String input , Class <T > clazz ) {
367360 JsoniterSpi .setCurrentConfig (config );
368361 try {
@@ -398,7 +391,7 @@ public static final <T> T deserialize(Config config, byte[] input, Class<T> claz
398391 }
399392 public static final <T > T deserialize (byte [] input , Class <T > clazz ) {
400393 int lastNotSpacePos = findLastNotSpacePos (input );
401- JsonIterator iter = tlsIter . get ();
394+ JsonIterator iter = JsonIteratorPool . borrowJsonIterator ();
402395 iter .reset (input , 0 , lastNotSpacePos );
403396 try {
404397 T val = iter .read (clazz );
@@ -410,6 +403,8 @@ public static final <T> T deserialize(byte[] input, Class<T> clazz) {
410403 throw iter .reportError ("deserialize" , "premature end" );
411404 } catch (IOException e ) {
412405 throw new JsonException (e );
406+ } finally {
407+ JsonIteratorPool .returnJsonIterator (iter );
413408 }
414409 }
415410
@@ -424,7 +419,7 @@ public static final <T> T deserialize(Config config, byte[] input, TypeLiteral<T
424419
425420 public static final <T > T deserialize (byte [] input , TypeLiteral <T > typeLiteral ) {
426421 int lastNotSpacePos = findLastNotSpacePos (input );
427- JsonIterator iter = tlsIter . get ();
422+ JsonIterator iter = JsonIteratorPool . borrowJsonIterator ();
428423 iter .reset (input , 0 , lastNotSpacePos );
429424 try {
430425 T val = iter .read (typeLiteral );
@@ -436,6 +431,8 @@ public static final <T> T deserialize(byte[] input, TypeLiteral<T> typeLiteral)
436431 throw iter .reportError ("deserialize" , "premature end" );
437432 } catch (IOException e ) {
438433 throw new JsonException (e );
434+ } finally {
435+ JsonIteratorPool .returnJsonIterator (iter );
439436 }
440437 }
441438
@@ -463,7 +460,7 @@ public static final Any deserialize(Config config, byte[] input) {
463460
464461 public static final Any deserialize (byte [] input ) {
465462 int lastNotSpacePos = findLastNotSpacePos (input );
466- JsonIterator iter = tlsIter . get ();
463+ JsonIterator iter = JsonIteratorPool . borrowJsonIterator ();
467464 iter .reset (input , 0 , lastNotSpacePos );
468465 try {
469466 Any val = iter .readAny ();
@@ -475,6 +472,8 @@ public static final Any deserialize(byte[] input) {
475472 throw iter .reportError ("deserialize" , "premature end" );
476473 } catch (IOException e ) {
477474 throw new JsonException (e );
475+ } finally {
476+ JsonIteratorPool .returnJsonIterator (iter );
478477 }
479478 }
480479
0 commit comments