mirror of
https://github.com/stleary/JSON-java.git
synced 2025-08-05 12:25:36 -04:00
JSONTokener implemented java.io.Closeable
This commit is contained in:
parent
45bcba518f
commit
e1eabc9c27
@ -1,11 +1,6 @@
|
|||||||
package org.json;
|
package org.json;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.Reader;
|
|
||||||
import java.io.StringReader;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Public Domain.
|
Public Domain.
|
||||||
@ -18,7 +13,7 @@ Public Domain.
|
|||||||
* @author JSON.org
|
* @author JSON.org
|
||||||
* @version 2014-05-03
|
* @version 2014-05-03
|
||||||
*/
|
*/
|
||||||
public class JSONTokener {
|
public class JSONTokener implements Closeable {
|
||||||
/** current read character position on the current line. */
|
/** current read character position on the current line. */
|
||||||
private long character;
|
private long character;
|
||||||
/** flag to indicate if the end of the input has been found. */
|
/** flag to indicate if the end of the input has been found. */
|
||||||
@ -522,4 +517,11 @@ public class JSONTokener {
|
|||||||
return " at " + this.index + " [character " + this.character + " line " +
|
return " at " + this.index + " [character " + this.character + " line " +
|
||||||
this.line + "]";
|
this.line + "]";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
if(reader!=null){
|
||||||
|
reader.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -313,4 +313,16 @@ public class JSONTokenerTest {
|
|||||||
assertEquals(0, t2.next());
|
assertEquals(0, t2.next());
|
||||||
assertFalse(t2.more());
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user