Fix testStringEndingInSlash by fixing nextNonWhitespace to always return the character at buffer[pos-1].

This commit is contained in:
Jesse Wilson 2012-02-16 22:49:53 +00:00
parent 2c8bec27d4
commit 15e7819e9a
2 changed files with 28 additions and 3 deletions

View File

@ -863,6 +863,12 @@ public class JsonReader implements Closeable {
return result;
}
/**
* Returns the next character in the stream that is neither whitespace nor a
* part of a comment. When this returns, the returned character is always at
* {@code buffer[pos-1]}; this means the caller can always push back the
* returned character by decrementing {@code pos}.
*/
private int nextNonWhitespace(boolean throwOnEof) throws IOException {
/*
* This code uses ugly local variables 'p' and 'l' representing the 'pos'
@ -895,8 +901,13 @@ public class JsonReader implements Closeable {
case '/':
pos = p;
if (p == l && !fillBuffer(1)) {
return c;
if (p == l) {
pos--; // push back '/' so it's still in the buffer when this method returns
boolean charsLoaded = fillBuffer(2);
pos++; // consume the '/' again
if (!charsLoaded) {
return c;
}
}
checkLenient();

View File

@ -1092,7 +1092,21 @@ public final class JsonReaderTest extends TestCase {
public void testStringEndingInSlash() throws IOException {
JsonReader reader = new JsonReader(new StringReader("/"));
reader.setLenient(true);
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
try {
reader.peek();
fail();
} catch (MalformedJsonException expected) {
}
}
public void testDocumentWithCommentEndingInSlash() throws IOException {
JsonReader reader = new JsonReader(new StringReader("/* foo *//"));
reader.setLenient(true);
try {
reader.peek();
fail();
} catch (MalformedJsonException expected) {
}
}
public void testStringWithLeadingSlash() throws IOException {