/** * Escapes the specified token. * * @param token token * @return escaped token */ public static byte[] escape(final byte[] token) { final TokenBuilder tb = new TokenBuilder(); for (final byte t : token) { if (t >= 0x20 && t <= 0x7e) tb.addByte(t); else hex(tb, t); } return tb.finish(); }
/** * Returns a URI encoded token. * * @param token token * @param iri input * @return encoded token */ public static byte[] uri(final byte[] token, final boolean iri) { final TokenBuilder tb = new TokenBuilder(); for (final byte t : token) { if (letterOrDigit(t) || contains(iri ? IRIRES : RES, t)) tb.addByte(t); else hex(tb, t); } return tb.finish(); }
/** * Adds the specified byte in hex code. * * @param tb token builder * @param b byte to be added */ private static void hex(final TokenBuilder tb, final byte b) { tb.add('%'); tb.addByte(HEX[(b & 0xFF) >> 4]); tb.addByte(HEX[b & 0xFF & 15]); }