summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--core/java/android/util/JsonReader.java8
-rw-r--r--core/tests/coretests/src/android/util/JsonReaderTest.java21
2 files changed, 29 insertions, 0 deletions
diff --git a/core/java/android/util/JsonReader.java b/core/java/android/util/JsonReader.java
index 09ce8e4..f139372 100644
--- a/core/java/android/util/JsonReader.java
+++ b/core/java/android/util/JsonReader.java
@@ -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;
}
diff --git a/core/tests/coretests/src/android/util/JsonReaderTest.java b/core/tests/coretests/src/android/util/JsonReaderTest.java
index 0b50af3..42b7640 100644
--- a/core/tests/coretests/src/android/util/JsonReaderTest.java
+++ b/core/tests/coretests/src/android/util/JsonReaderTest.java
@@ -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",