{"id":1756,"date":"2010-08-16T12:16:33","date_gmt":"2010-08-16T17:16:33","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=1756"},"modified":"2010-08-18T10:08:28","modified_gmt":"2010-08-18T15:08:28","slug":"a-simple-java-tlv-parser","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=1756","title":{"rendered":"A Simple Java TLV Parser"},"content":{"rendered":"<p>* Read <a href=\"http:\/\/en.wikipedia.org\/wiki\/Type-length-value\">here <\/a>for an explanation of TLV.<br \/>\n* In the following sample code, all found tag values are returned as a byte array containing individual tag values, each of which is itself a byte array.<\/p>\n<pre lang=\"java\">\r\n    \/**\r\n     * Reads TLV values for a given hex string.\r\n     *\/\r\n    public static byte[][] readTLV(String tlvHexString, int tag) {\r\n        return readTLV(hexStringToByteArray(tlvHexString), tag);\r\n    }\r\n\r\n    \/**\r\n     * Reads TLV values for a given byte array.\r\n     *\/\r\n    public static byte[][] readTLV(byte[] tlv, int tag) {\r\n        if (tlv == null || tlv.length < 1) {\r\n            throw new IllegalArgumentException(\"Invalid TLV\");\r\n        }\r\n        \r\n        int c = 0;\r\n        ArrayList al = new ArrayList();\r\n        \r\n        ByteArrayInputStream is = null;\r\n        try {\r\n             is = new ByteArrayInputStream(tlv);\r\n            \r\n             while ((c = is.read()) != -1) {\r\n                if (c == tag){\r\n                    log.debug(\"Got tag\");\r\n                    if ((c = is.read()) != -1){\r\n                        byte[] value = new byte[c];\r\n                        is.read(value,0,c);\r\n                        al.add(value);\r\n                    }\r\n                }\r\n            }\r\n        } finally {\r\n            if (is != null) {\r\n                try{\r\n                    is.close();\r\n                }catch (IOException e){\r\n                    log.error(e);\r\n                }\r\n            }\r\n        }\r\n        log.debug(\"Got \" + al.size() + \" values for tag \" \r\n            + Integer.toHexString(tag));\r\n        byte[][] vals = new byte[al.size()][];\r\n        al.toArray(vals);\r\n        return vals;\r\n    }\r\n\r\n    \/**\r\n     * Converts a hex string to byte array.\r\n     *\/\r\n    public static byte[] hexStringToByteArray(String s) { \r\n        int len = s.length(); \r\n        byte[] data = new byte[len \/ 2]; \r\n        for (int i = 0; i < len; i += 2) { \r\n            data[i \/ 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) \r\n                                 + Character.digit(s.charAt(i+1), 16)); \r\n        } \r\n        return data; \r\n    } \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>* Read here for an explanation of TLV. * In the following sample code, all found tag values are returned as a byte array containing individual tag values, each of which is itself a byte array. \/** * Reads TLV &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=1756\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[14],"tags":[],"class_list":["post-1756","post","type-post","status-publish","format-standard","hentry","category-java"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-sk","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1756"}],"version-history":[{"count":5,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1756\/revisions"}],"predecessor-version":[{"id":1761,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1756\/revisions\/1761"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}