Component implements ComponentProvider { public static byte[] parseURI(String name) throws DotDot, URISyntaxException { byte[] decodedName = null; boolean alldots = true; boolean quitEarly = false; boolean hexEncoding = false; int b1, b2; ByteBuffer result = ByteBuffer.allocate(name.length()); for (int i = 0; i < name.length() && !quitEarly; i++) { char ch = name.charAt(i); switch (ch) { case '%': if (name.length()-1 < i+2) { throw new URISyntaxException(name, "malformed %xy byte representation: too short", i); } b1 = Character.digit(name.charAt(++i), 16); b2 = Character.digit(name.charAt(++i), 16); if (b1 < 0 || b2 < 0) throw new URISyntaxException(name, "malformed %xy byte representation: not legal hex number: " + name.substring(i-2, i+1), i-2); result.put((byte)((b1 * 16) + b2)); break; case '/': case '?': case '#': quitEarly = true; break; case '=': if (name.length()-1 < i+2 || ((name.length() - i) & 1) == 0) { throw new URISyntaxException(name, "malformed =xy byte representation: too short", i); } hexEncoding = true; break; case ':': case '[': case ']': case '@': case '!': case '$': case '&': case '\'': case '(': case ')': case '*': case '+': case ',': case ';': result.put((byte)ch); break; default: if (uriReserved(ch)) throw new URISyntaxException(name, "Illegal characters in URI", i); if (hexEncoding) { b1 = Character.digit(ch, 16); b2 = Character.digit(name.charAt(++i), 16); if (b1 < 0 || b2 < 0) throw new URISyntaxException(name, "malformed =xy byte representation: not legal hex number: " + name.substring(i-1, i), i-1); result.put((byte)((b1 * 16) + b2)); } else { result.put((byte)ch); } break; } if (!quitEarly && result.position() > 0 && result.get(result.position()-1) != '.') { alldots = false; } } result.flip(); if (alldots) { if (result.limit() <= 1) { return null; } else if (result.limit() == 2) { throw new DotDot(); } else { result.limit(result.limit()-3); } } decodedName = new byte[result.limit()]; System.arraycopy(result.array(), 0, decodedName, 0, result.limit()); return decodedName; } protected  Component(byte[] comp);  Component(String text); @Override byte[] getComponent(); static byte[] parseNative(String name); static byte[] parseURI(String name); static String hexPrint(byte [] bs); static String printNative(byte[] bs); static String printURI(byte [] bs); static String printURI(byte [] bs, int offset, int length); @Override boolean equals(Object obj); @Override int hashCode(); @Override String toString(); static final ComponentProvider NONCE; static final ComponentProvider EMPTY; }
@Test public void testParseURI() throws DotDot, URISyntaxException { byte[] bytes = Component.parseURI(COMPONENT_HEXIFIED); Assert.assertTrue(Arrays.equals(COMPONENT_NAME_BYTES, bytes)); }