/** Decode a string into a ByteBuffer */ public static String fromByteBuffer(ByteBuffer bb) { // return BlockUTF8.toString(bb) ; // To be removed (Dec 2011) CharsetDecoder dec = Chars.allocDecoder(); String x = fromByteBuffer(bb, dec); Chars.deallocDecoder(dec); return x; }
/** Encode a string into a ByteBuffer : on return position is the end of the encoding */ public static int toByteBuffer(CharSequence s, ByteBuffer bb) { // BlockUTF8.fromChars(s, bb) ; // To be removed (Dec 2011) CharsetEncoder enc = Chars.allocEncoder(); int x = toByteBuffer(s, bb, enc); Chars.deallocEncoder(enc); return x; }
/** * Encode a string using hex values e.g. %20 * * @param str String to encode * @param marker Marker character * @param escapees Characters to encode (must include the marker) * @return Encoded string (returns input object if no change) */ public static String encodeHex(String str, char marker, char[] escapees) { // We make a first pass to see if there is anything to do. // This is assuming // (1) the string is shortish (e.g. fits in L1) // (2) necessary escaping is not common int N = str.length(); int idx = 0; // Scan stage. for (; idx < N; idx++) { char ch = str.charAt(idx); if (RiotChars.charInArray(ch, escapees)) break; } if (idx == N) return str; // At least one char to convert StringBuilder buff = new StringBuilder(); buff.append(str, 0, idx); // Insert first part. for (; idx < N; idx++) { char ch = str.charAt(idx); if (RiotChars.charInArray(ch, escapees)) { Chars.encodeAsHex(buff, marker, ch); continue; } buff.append(ch); } return buff.toString(); }