Merge "Merge "XmlUtils: Fix hex parsing in readThisByteArrayXml" into qt-qpr1-dev am: 2883327fba" into qt-qpr1-dev-plus-aosp
This commit is contained in:
committed by
Android (Google) Code Review
commit
defbe4d33e
@@ -26,6 +26,8 @@ import android.util.ArrayMap;
|
||||
import android.util.Base64;
|
||||
import android.util.Xml;
|
||||
|
||||
import libcore.util.HexEncoding;
|
||||
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
import org.xmlpull.v1.XmlSerializer;
|
||||
@@ -396,16 +398,7 @@ public class XmlUtils {
|
||||
final int N = val.length;
|
||||
out.attribute(null, "num", Integer.toString(N));
|
||||
|
||||
StringBuilder sb = new StringBuilder(val.length*2);
|
||||
for (int i=0; i<N; i++) {
|
||||
int b = val[i];
|
||||
int h = (b >> 4) & 0x0f;
|
||||
sb.append((char)(h >= 10 ? ('a'+h-10) : ('0'+h)));
|
||||
h = b & 0x0f;
|
||||
sb.append((char)(h >= 10 ? ('a'+h-10) : ('0'+h)));
|
||||
}
|
||||
|
||||
out.text(sb.toString());
|
||||
out.text(HexEncoding.encodeToString(val).toLowerCase());
|
||||
|
||||
out.endTag(null, "byte-array");
|
||||
}
|
||||
@@ -1032,7 +1025,9 @@ public class XmlUtils {
|
||||
"Not a number in num attribute in byte-array");
|
||||
}
|
||||
|
||||
byte[] array = new byte[num];
|
||||
// 0 len byte array does not have a text in the XML tag. So, initialize to 0 len array.
|
||||
// For all other array lens, HexEncoding.decode() below overrides the array.
|
||||
byte[] array = new byte[0];
|
||||
|
||||
int eventType = parser.getEventType();
|
||||
do {
|
||||
@@ -1043,16 +1038,7 @@ public class XmlUtils {
|
||||
throw new XmlPullParserException(
|
||||
"Invalid value found in byte-array: " + values);
|
||||
}
|
||||
// This is ugly, but keeping it to mirror the logic in #writeByteArrayXml.
|
||||
for (int i = 0; i < num; i ++) {
|
||||
char nibbleHighChar = values.charAt(2 * i);
|
||||
char nibbleLowChar = values.charAt(2 * i + 1);
|
||||
int nibbleHigh = nibbleHighChar > 'a' ? (nibbleHighChar - 'a' + 10)
|
||||
: (nibbleHighChar - '0');
|
||||
int nibbleLow = nibbleLowChar > 'a' ? (nibbleLowChar - 'a' + 10)
|
||||
: (nibbleLowChar - '0');
|
||||
array[i] = (byte) ((nibbleHigh & 0x0F) << 4 | (nibbleLow & 0x0F));
|
||||
}
|
||||
array = HexEncoding.decode(values);
|
||||
}
|
||||
} else if (eventType == parser.END_TAG) {
|
||||
if (parser.getName().equals(endTag)) {
|
||||
|
||||
@@ -16,13 +16,22 @@
|
||||
|
||||
package com.android.internal.util;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
import android.util.Xml;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlSerializer;
|
||||
|
||||
public class XmlUtilsTest extends TestCase {
|
||||
|
||||
// https://code.google.com/p/android/issues/detail?id=63717
|
||||
@@ -38,4 +47,23 @@ public class XmlUtilsTest extends TestCase {
|
||||
assertEquals("nullValue", deserialized.get(null));
|
||||
assertEquals("fooValue", deserialized.get("foo"));
|
||||
}
|
||||
|
||||
public void testreadWriteXmlByteArrayValue() throws Exception {
|
||||
byte[] testByteArray = {0x1 , 0xa, 0xb, 0x9, 0x34, (byte) 0xaa, (byte) 0xba, (byte) 0x99};
|
||||
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
|
||||
XmlSerializer serializer = new FastXmlSerializer();
|
||||
serializer.setOutput(baos, StandardCharsets.UTF_8.name());
|
||||
serializer.startDocument(null, true);
|
||||
XmlUtils.writeValueXml(testByteArray, "testByteArray", serializer);
|
||||
serializer.endDocument();
|
||||
|
||||
InputStream bais = new ByteArrayInputStream(baos.toByteArray());
|
||||
XmlPullParser pullParser = Xml.newPullParser();
|
||||
pullParser.setInput(bais, StandardCharsets.UTF_8.name());
|
||||
String[] name = new String[1];
|
||||
byte[] testByteArrayDeserialized = (byte[]) XmlUtils.readValueXml(pullParser, name);
|
||||
assertEquals("testByteArray", name[0]);
|
||||
assertArrayEquals(testByteArray, testByteArrayDeserialized);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user