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

Commit a7b6316b authored by Jesse Wilson's avatar Jesse Wilson Committed by Android (Google) Code Review
Browse files

Merge "Skip byte order mark (BOM) in JsonReader"

parents 7d62990b 7a2c813d
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",