File tree 2 files changed +21
-7
lines changed
2 files changed +21
-7
lines changed Original file line number Diff line number Diff line change 1
1
package org .json ;
2
2
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 .*;
9
4
10
5
/*
11
6
Public Domain.
18
13
* @author JSON.org
19
14
* @version 2014-05-03
20
15
*/
21
- public class JSONTokener {
16
+ public class JSONTokener implements Closeable {
22
17
/** current read character position on the current line. */
23
18
private long character ;
24
19
/** flag to indicate if the end of the input has been found. */
@@ -522,4 +517,11 @@ public String toString() {
522
517
return " at " + this .index + " [character " + this .character + " line " +
523
518
this .line + "]" ;
524
519
}
520
+
521
+ @ Override
522
+ public void close () throws IOException {
523
+ if (reader !=null ){
524
+ reader .close ();
525
+ }
526
+ }
525
527
}
Original file line number Diff line number Diff line change @@ -313,4 +313,16 @@ public void testNextBackComboWithNewLines() {
313
313
assertEquals (0 , t2 .next ());
314
314
assertFalse (t2 .more ());
315
315
}
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
+ }
316
328
}
You can’t perform that action at this time.
0 commit comments