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

Commit 651209b5 authored by Roshan Pius's avatar Roshan Pius
Browse files

XmlUtils: Add missing readThisByteArrayXml method

Add the missing XML to byte array conversion method.

While there,
1. Fix writeByteArrayXml method to store the hex chars of the value.
2. Cleanup couple of error strings in |readThisIntArrayXml| method.

BUG: 29039296
Change-Id: I6386f7df7c5c8b7bc3bc5a268196da617209cea9
TEST: Compiles & manual testing.
parent fa8105c0
Loading
Loading
Loading
Loading
+78 −6
Original line number Original line Diff line number Diff line
@@ -392,10 +392,10 @@ public class XmlUtils {
        StringBuilder sb = new StringBuilder(val.length*2);
        StringBuilder sb = new StringBuilder(val.length*2);
        for (int i=0; i<N; i++) {
        for (int i=0; i<N; i++) {
            int b = val[i];
            int b = val[i];
            int h = b>>4;
            int h = (b >> 4) & 0x0f;
            sb.append(h >= 10 ? ('a'+h-10) : ('0'+h));
            sb.append((char)(h >= 10 ? ('a'+h-10) : ('0'+h)));
            h = b&0xff;
            h = b & 0x0f;
            sb.append(h >= 10 ? ('a'+h-10) : ('0'+h));
            sb.append((char)(h >= 10 ? ('a'+h-10) : ('0'+h)));
        }
        }


        out.text(sb.toString());
        out.text(sb.toString());
@@ -995,6 +995,73 @@ public class XmlUtils {
                "Document ended before " + endTag + " end tag");
                "Document ended before " + endTag + " end tag");
    }
    }


    /**
     * Read a byte[] object from an XmlPullParser.  The XML data could
     * previously have been generated by writeByteArrayXml().  The XmlPullParser
     * must be positioned <em>after</em> the tag that begins the list.
     *
     * @param parser The XmlPullParser from which to read the list data.
     * @param endTag Name of the tag that will end the list, usually "list".
     * @param name An array of one string, used to return the name attribute
     *             of the list's tag.
     *
     * @return Returns a newly generated byte[].
     *
     * @see #writeByteArrayXml
     */
    public static final byte[] readThisByteArrayXml(XmlPullParser parser,
            String endTag, String[] name)
            throws XmlPullParserException, java.io.IOException {

        int num;
        try {
            num = Integer.parseInt(parser.getAttributeValue(null, "num"));
        } catch (NullPointerException e) {
            throw new XmlPullParserException(
                    "Need num attribute in byte-array");
        } catch (NumberFormatException e) {
            throw new XmlPullParserException(
                    "Not a number in num attribute in byte-array");
        }

        byte[] array = new byte[num];

        int eventType = parser.getEventType();
        do {
            if (eventType == parser.TEXT) {
                if (num > 0) {
                    String values = parser.getText();
                    if (values == null || values.length() != num * 2) {
                        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));
                    }
                }
            } else if (eventType == parser.END_TAG) {
                if (parser.getName().equals(endTag)) {
                    return array;
                } else {
                    throw new XmlPullParserException(
                            "Expected " + endTag + " end tag at: "
                                    + parser.getName());
                }
            }
            eventType = parser.next();
        } while (eventType != parser.END_DOCUMENT);

        throw new XmlPullParserException(
                "Document ended before " + endTag + " end tag");
    }

    /**
    /**
     * Read an int[] object from an XmlPullParser.  The XML data could
     * Read an int[] object from an XmlPullParser.  The XML data could
     * previously have been generated by writeIntArrayXml().  The XmlPullParser
     * previously have been generated by writeIntArrayXml().  The XmlPullParser
@@ -1018,10 +1085,10 @@ public class XmlUtils {
            num = Integer.parseInt(parser.getAttributeValue(null, "num"));
            num = Integer.parseInt(parser.getAttributeValue(null, "num"));
        } catch (NullPointerException e) {
        } catch (NullPointerException e) {
            throw new XmlPullParserException(
            throw new XmlPullParserException(
                    "Need num attribute in byte-array");
                    "Need num attribute in int-array");
        } catch (NumberFormatException e) {
        } catch (NumberFormatException e) {
            throw new XmlPullParserException(
            throw new XmlPullParserException(
                    "Not a number in num attribute in byte-array");
                    "Not a number in num attribute in int-array");
        }
        }
        parser.next();
        parser.next();


@@ -1377,6 +1444,11 @@ public class XmlUtils {
                "Unexpected end of document in <string>");
                "Unexpected end of document in <string>");
        } else if ((res = readThisPrimitiveValueXml(parser, tagName)) != null) {
        } else if ((res = readThisPrimitiveValueXml(parser, tagName)) != null) {
            // all work already done by readThisPrimitiveValueXml
            // all work already done by readThisPrimitiveValueXml
        } else if (tagName.equals("byte-array")) {
            res = readThisByteArrayXml(parser, "byte-array", name);
            name[0] = valueName;
            //System.out.println("Returning value for " + valueName + ": " + res);
            return res;
        } else if (tagName.equals("int-array")) {
        } else if (tagName.equals("int-array")) {
            res = readThisIntArrayXml(parser, "int-array", name);
            res = readThisIntArrayXml(parser, "int-array", name);
            name[0] = valueName;
            name[0] = valueName;