Escapes the given byte array.

@param valueBytes the UTF-8 bytes
@param ri The reader index, pointed at the first byte that needs escaping.
18158
private static String doEscape(byte[] valueBytes, int ri) {
        final byte[] escapedBytes = new byte[ri + (valueBytes.length - ri) * 3];
        // copy over the good bytes
        if (ri != 0) {
            System.arraycopy(valueBytes, 0, escapedBytes, 0, ri);
        }
        int wi = ri;
        for (; ri < valueBytes.length; ri++) {
            final byte b = valueBytes[ri];
            // Manually implement URL encoding, per the gRPC spec.
            if (isEscapingChar(b)) {
                escapedBytes[wi] = '%';
                escapedBytes[wi + 1] = HEX[(b >> 4) & 0xF];
                escapedBytes[wi + 2] = HEX[b & 0xF];
                wi += 3;
                continue;
            }
            escapedBytes[wi++] = b;
        }
        final byte[] dest = new byte[wi];
        System.arraycopy(escapedBytes, 0, dest, 0, wi);

        return new String(dest, StandardCharsets.US_ASCII);
    }
1198390
static void writeRawVarint32(ByteBuf out, int value) {
        while (true) {
            if ((value & ~0x7F) == 0) {
                out.writeByte(value);
                return;
            } else {
                out.writeByte((value & 0x7F) | 0x80);
                value >>>= 7;
            }
        }
    }
856822
static void writeString(final String value, final DataOutput out)
		throws IOException
	{
		final byte[] bytes = value.getBytes("UTF-8");
		writeInt(bytes.length, out);
		out.write(bytes);
	}
214954
private static int scanUriEscape(ByteToChar converter,
                                   byte []rawUri, int i, int len)
    throws IOException
  {
    int ch1 = i < len ? (rawUri[i++] & 0xff) : -1;

    if (ch1 == 'u') {
      ch1 = i < len ? (rawUri[i++] & 0xff) : -1;
      int ch2 = i < len ? (rawUri[i++] & 0xff) : -1;
      int ch3 = i < len ? (rawUri[i++] & 0xff) : -1;
      int ch4 = i < len ? (rawUri[i++] & 0xff) : -1;

      converter.addChar((char) ((toHex(ch1) << 12) +
                                (toHex(ch2) << 8) + 
                                (toHex(ch3) << 4) + 
                                (toHex(ch4))));
    }
    else {
      int ch2 = i < len ? (rawUri[i++] & 0xff) : -1;

      int b = (toHex(ch1) << 4) + toHex(ch2);;

      converter.addByte(b);
    }

    return i;
  }
1000422
public static UTF8String fromBytes(byte[] bytes) {
    if (bytes != null) {
      return new UTF8String(bytes, BYTE_ARRAY_OFFSET, bytes.length);
    } else {
      return null;
    }
  }
1062167
protected void setByteArrayValue(byte[] input) {
        this.sValue = null;
        this.bValue = input;
        this.offset = 0;
        this.valueLength = input.length;
        if (ELEM_ADDED != this.status) {
            this.status = ELEM_CHANGED;
        }
    }
383964
private void writeByte(int value) throws IOException {
    if (this.msb0) {
      value = JBBPUtils.reverseBitsInByte((byte) value) & 0xFF;
    }
    this.out.write(value);
    this.byteCounter++;
  }
205674
public BEValue bdecodeBytes() throws IOException {
    int c = this.getNextIndicator();
    int num = c - '0';
    if (num < 0 || num > 9)
      throw new InvalidBEncodingException("Number expected, not '"
              + (char) c + "'");
    this.indicator = 0;

    c = this.read();
    int i = c - '0';
    while (i >= 0 && i <= 9) {
      // This can overflow!
      num = num * 10 + i;
      c = this.read();
      i = c - '0';
    }

    if (c != ':') {
      throw new InvalidBEncodingException("Colon expected, not '" +
              (char) c + "'");
    }

    return new BEValue(read(num));
  }
393667
public static void writeBytes(byte b[], int offset, byte[] value) {
    System.arraycopy(value, 0, b, offset, value.length);
  }
1133236
public static void writeBytes(byte[] bytes, long value, int offset, int numBytes) {
        int shift = 0;
        for(int i = offset + numBytes - 1; i >= offset; i--) {
            bytes[i] = (byte) (0xFF & (value >> shift));
            shift += 8;
        }
    }
1152220
public static void writeUTF8(OutputStream out, String str) throws IOException {
        byte[] s = str.getBytes(UTF8);
        out.write(s);
    }
841065
public static String convertByteArrayToStringWithoutEscape(byte[] data, int offset, int length) {
    StringBuilder sb = new StringBuilder(length);
    for (int i = offset; i < length && i < data.length; i++) {
      sb.append((char) data[i]);
    }
    return sb.toString();
  }
1193854
public static String encode(byte[] bytes) {
	String retorno = "";
	if (bytes == null || bytes.length == 0) {
	    return retorno;
	}
	for (int i = 0; i < bytes.length; i++) {
	    byte valor = bytes[i];
	    int d1 = valor & 0xF;
	    d1 += (d1 < 10) ? 48 : 55;
	    int d2 = (valor & 0xF0) >> 4;
	    d2 += (d2 < 10) ? 48 : 55;
	    retorno = retorno + (char) d2 + (char) d1;
	}
	return retorno;
    }
1352038
public static String toBinaryString(final byte[] data) {
        if (data == null) {
            return null;
        }
        final StringBuilder buf = new StringBuilder(data.length * 8);
        for (final byte element : data) {
            byte bits = element;
            for (int j = 0; j < 8; j++) {
                if ((bits & 0x80) == 0x80) {
                    buf.append('1');
                } else {
                    buf.append('0');
                }
                bits <<= 1;
            }
        }
        return buf.toString();
    }
108084
public static byte[] decodeBase64(String value) {
        int byteShift = 4;
        int tmp = 0;
        boolean done = false;
        final StringBuilder buffer = new StringBuilder();

        for (int i = 0; i != value.length(); i++) {
            final char c = value.charAt(i);
            final int sixBit = (c < 123) ? EncodingGroovyMethodsSupport.TRANSLATE_TABLE[c] : 66;

            if (sixBit < 64) {
                if (done)
                    throw new RuntimeException("= character not at end of base64 value"); // TODO: change this exception type

                tmp = (tmp << 6) | sixBit;

                if (byteShift-- != 4) {
                    buffer.append((char) ((tmp >> (byteShift * 2)) & 0XFF));
                }

            } else if (sixBit == 64) {

                byteShift--;
                done = true;

            } else if (sixBit == 66) {
                // RFC 2045 says that I'm allowed to take the presence of
                // these characters as evidence of data corruption
                // So I will
                throw new RuntimeException("bad character in base64 value"); // TODO: change this exception type
            }

            if (byteShift == 0) byteShift = 4;
        }

        try {
            return buffer.toString().getBytes("ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Base 64 decode produced byte values > 255"); // TODO: change this exception type
        }
    }
1100585
private int utf8Position(int charPosition) {
    int pos = offset;
    for (int i = 0; i < charPosition; i++) {
      int byteValue = data[pos] & 0xff;
      if (byteValue < 0x80) {
        pos += 1;
      } else if (byteValue < 0xC2) {
        throw new UncheckedIOException("invalid UTF8", new CharacterCodingException());
      } else if (byteValue < 0xE0) {
        pos += 2;
      } else if (byteValue < 0xF0) {
        pos += 3;
      } else if (byteValue < 0xF8) {
        pos += 4;
      } else {
        throw new UncheckedIOException("invalid UTF8", new CharacterCodingException());
      }
    }
    return pos;
  }
1105865
public static byte[] toByteArray(String value) {
        if (value == null) {
            return null;
        }
        try {
            CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder().onMalformedInput(CodingErrorAction.REPORT)
                    .onUnmappableCharacter(CodingErrorAction.REPORT);
            ByteBuffer buf = encoder.encode(CharBuffer.wrap(value));
            // don't use ByteBuffer.array(), as it returns internal, and
            // possibly larger, byte array
            byte[] res = new byte[buf.remaining()];
            buf.get(res);
            return res;
        } catch (CharacterCodingException e) {
            throw new RuntimeException("Unexpected exception", e);
        }
    }