Skip to content

Commit

Permalink
JSONTokener implemented java.io.Closeable
Browse files Browse the repository at this point in the history
  • Loading branch information
haribabu-dev committed Mar 4, 2023
1 parent 45bcba5 commit e1eabc9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/main/java/org/json/JSONTokener.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package org.json;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.*;

/*
Public Domain.
Expand All @@ -18,7 +13,7 @@
* @author JSON.org
* @version 2014-05-03
*/
public class JSONTokener {
public class JSONTokener implements Closeable {
/** current read character position on the current line. */
private long character;
/** flag to indicate if the end of the input has been found. */
Expand Down Expand Up @@ -522,4 +517,11 @@ public String toString() {
return " at " + this.index + " [character " + this.character + " line " +
this.line + "]";
}

@Override
public void close() throws IOException {
if(reader!=null){
reader.close();
}
}
}
12 changes: 12 additions & 0 deletions src/test/java/org/json/junit/JSONTokenerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,16 @@ public void testNextBackComboWithNewLines() {
assertEquals(0, t2.next());
assertFalse(t2.more());
}

@Test
public void testAutoClose(){
Reader reader = new StringReader("some test string");
try {
JSONTokener tokener = new JSONTokener(reader);
tokener.close();
tokener.next();
} catch (Exception exception){
assertEquals("Stream closed", exception.getMessage());
}
}
}

0 comments on commit e1eabc9

Please sign in to comment.