Always return null once a json parser has closed

This commit is contained in:
Jeremy Lakeman 2018-06-26 14:32:00 +09:30
parent 2f3b8cadc4
commit 3d2eb1c260
2 changed files with 19 additions and 9 deletions

View File

@ -111,7 +111,7 @@ public class JsonParser {
if (current != ch) { if (current != ch) {
return false; return false;
} }
read(); current=0;
return true; return true;
} }
@ -122,14 +122,8 @@ public class JsonParser {
private void requireConstString(String str) throws IOException, JsonParseException{ private void requireConstString(String str) throws IOException, JsonParseException{
// Check for every character, without attempting to read past the end // Check for every character, without attempting to read past the end
for(int i=0;i<str.length();i++) { for(int i=0;i<str.length();i++)
if (current == 0) requireChar(str.charAt(i));
read();
char ch = str.charAt(i);
if (current != ch)
expected("'"+ch+"'");
current = 0;
}
} }
private void skipWhiteSpace() throws IOException { private void skipWhiteSpace() throws IOException {
@ -404,6 +398,8 @@ public class JsonParser {
public String readString() throws IOException, JsonParseException { public String readString() throws IOException, JsonParseException {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
requireChar('"'); requireChar('"');
if (current == 0)
read();
while(current != '"'){ while(current != '"'){
if (current == '\\'){ if (current == '\\'){
switch (read()) { switch (read()) {

View File

@ -67,6 +67,20 @@ public abstract class HttpJsonSerialiser<T, E extends Exception> extends JsonTab
} }
} }
@Override
public T next() throws IOException, JsonParser.JsonParseException, E {
if (closed)
return null;
try {
return super.next();
}catch (IOException | JsonParser.JsonParseException e){
// don't throw if a close was triggered from another thread
if (closed)
return null;
throw e;
}
}
public void close() throws IOException public void close() throws IOException
{ {
if (closed) if (closed)