private static String encodeChunk(String value, Charset charset, boolean allowReserved) {
    if (isEncoded(value, charset)) {
      return value;
    }

    byte[] data = value.getBytes(charset);
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
      for (byte b : data) {
        if (isUnreserved((char) b)) {
          bos.write(b);
        } else if (isReserved((char) b) && allowReserved) {
          bos.write(b);
        } else {
          pctEncode(b, bos);
        }
      }
      return new String(bos.toByteArray(), charset);
    } catch (IOException ioe) {
      throw new IllegalStateException(
          "Error occurred during encoding of the uri: " + ioe.getMessage(), ioe);
    }
  }
Encode a Uri Chunk, ensuring that all reserved characters are also encoded.

@param value to encode.
@param charset to use.
@return an encoded uri chunk.
core/src/main/java/feign/template/UriUtils.java