File tree Expand file tree Collapse file tree 2 files changed +21
-7
lines changed
Expand file tree Collapse file tree 2 files changed +21
-7
lines changed Original file line number Diff line number Diff line change 11package org .json ;
22
3- import java .io .BufferedReader ;
4- import java .io .IOException ;
5- import java .io .InputStream ;
6- import java .io .InputStreamReader ;
7- import java .io .Reader ;
8- import java .io .StringReader ;
3+ import java .io .*;
94
105/*
116Public Domain.
1813 * @author JSON.org
1914 * @version 2014-05-03
2015 */
21- public class JSONTokener {
16+ public class JSONTokener implements Closeable {
2217 /** current read character position on the current line. */
2318 private long character ;
2419 /** flag to indicate if the end of the input has been found. */
@@ -522,4 +517,11 @@ public String toString() {
522517 return " at " + this .index + " [character " + this .character + " line " +
523518 this .line + "]" ;
524519 }
520+
521+ @ Override
522+ public void close () throws IOException {
523+ if (reader !=null ){
524+ reader .close ();
525+ }
526+ }
525527}
Original file line number Diff line number Diff line change @@ -313,4 +313,16 @@ public void testNextBackComboWithNewLines() {
313313 assertEquals (0 , t2 .next ());
314314 assertFalse (t2 .more ());
315315 }
316+
317+ @ Test
318+ public void testAutoClose (){
319+ Reader reader = new StringReader ("some test string" );
320+ try {
321+ JSONTokener tokener = new JSONTokener (reader );
322+ tokener .close ();
323+ tokener .next ();
324+ } catch (Exception exception ){
325+ assertEquals ("Stream closed" , exception .getMessage ());
326+ }
327+ }
316328}
You can’t perform that action at this time.
0 commit comments