Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7a2c813d authored by Jesse Wilson's avatar Jesse Wilson
Browse files

Skip byte order mark (BOM) in JsonReader

Bug: http://code.google.com/p/android/issues/detail?id=18508
Change-Id: I7652080d9ab475c75d0a811a6e2ede4975ebe063
parent 85111ee5
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -738,6 +738,14 @@ public final class JsonReader implements Closeable {
        int total;
        while ((total = in.read(buffer, limit, buffer.length - limit)) != -1) {
            limit += total;

            // if this is the first read, consume an optional byte order mark (BOM) if it exists
            if (bufferStartLine == 1 && bufferStartColumn == 1
                    && limit > 0 && buffer[0] == '\ufeff') {
                pos++;
                bufferStartColumn--;
            }

            if (limit >= minimum) {
                return true;
            }
+21 −0
Original line number Diff line number Diff line
@@ -857,11 +857,32 @@ public final class JsonReaderTest extends TestCase {
        }
    }

    public void testBomIgnoredAsFirstCharacterOfDocument() throws IOException {
        JsonReader reader = new JsonReader(new StringReader("\ufeff[]"));
        reader.beginArray();
        reader.endArray();
    }

    public void testBomForbiddenAsOtherCharacterInDocument() throws IOException {
        JsonReader reader = new JsonReader(new StringReader("[\ufeff]"));
        reader.beginArray();
        try {
            reader.endArray();
            fail();
        } catch (IOException expected) {
        }
    }

    public void testFailWithPosition() throws IOException {
        testFailWithPosition("Expected literal value at line 6 column 3",
                "[\n\n\n\n\n0,}]");
    }

    public void testFailWithPositionIsOffsetByBom() throws IOException {
        testFailWithPosition("Expected literal value at line 1 column 4",
                "\ufeff[0,}]");
    }

    public void testFailWithPositionGreaterThanBufferSize() throws IOException {
        String spaces = repeat(' ', 8192);
        testFailWithPosition("Expected literal value at line 6 column 3",