Ejemplo n.º 1
0
 /**
  * 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();
 }
Ejemplo n.º 2
0
 /**
  * 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();
 }
Ejemplo n.º 3
0
 /**
  * Replaces the specified character and returns the result token.
  *
  * @param token token to be checked
  * @param search the character to be replaced
  * @param replace the new character
  * @return resulting token
  */
 public static byte[] replace(final byte[] token, final int search, final int replace) {
   final TokenBuilder tb = new TokenBuilder(token.length);
   final int tl = token.length;
   for (int i = 0; i < tl; i += cl(token, i)) {
     final int c = cp(token, i);
     tb.add(c == search ? replace : c);
   }
   return tb.finish();
 }
Ejemplo n.º 4
0
 /**
  * Deletes the specified character from the token.
  *
  * @param token token
  * @param ch character to be removed
  * @return resulting token
  */
 public static byte[] delete(final byte[] token, final int ch) {
   // skip step if specified ascii character is not contained
   if (ch < 0x80 && !contains(token, ch)) return token;
   // remove character
   final TokenBuilder tb = new TokenBuilder(token.length);
   final int tl = token.length;
   for (int i = 0; i < tl; i += cl(token, i)) {
     final int c = cp(token, i);
     if (c != ch) tb.add(c);
   }
   return tb.finish();
 }
Ejemplo n.º 5
0
 /**
  * Converts a string to a UTF8 byte array.
  *
  * @param string string to be converted
  * @return byte array
  */
 private static byte[] utf8(final String string) {
   final char[] arr = string.toCharArray();
   final int al = arr.length;
   final TokenBuilder tb = new TokenBuilder(al << 1);
   for (int c = 0; c < al; ++c) {
     final char ch = arr[c];
     tb.add(
         Character.isHighSurrogate(ch) && c < al - 1 && Character.isLowSurrogate(arr[c + 1])
             ? Character.toCodePoint(ch, arr[++c])
             : ch);
   }
   return tb.finish();
 }
Ejemplo n.º 6
0
  /**
   * Splits a token around matches of the given separator.
   *
   * @param token token to be split
   * @param sep separation character
   * @return array
   */
  public static byte[][] split(final byte[] token, final int sep) {
    final int l = token.length;
    final byte[][] split = new byte[l][];

    int s = 0;
    final TokenBuilder tb = new TokenBuilder();
    for (int i = 0; i < l; i += cl(token, i)) {
      final int c = cp(token, i);
      if (c == sep) {
        if (!tb.isEmpty()) {
          split[s++] = tb.finish();
          tb.reset();
        }
      } else {
        tb.add(c);
      }
    }
    if (!tb.isEmpty()) split[s++] = tb.finish();
    return Array.copyOf(split, s);
  }
Ejemplo n.º 7
0
 /**
  * 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]);
 }