package com.integralblue.httpresponsecache.compat;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
public final class Strings {
/**
* The digits for every supported radix.
*/
private static final char[] DIGITS = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z'
};
private static final char[] UPPER_CASE_DIGITS = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z'
};
public static final boolean isEmpty(String string){
return string == null || string.length() == 0;
}
/**
* Converts the byte array to a string using the given charset.
*
* <p>The behavior when the bytes cannot be decoded by the given charset
* is to replace malformed input and unmappable characters with the charset's default
* replacement string. Use {@link java.nio.charset.CharsetDecoder} for more control.
*
* @throws IndexOutOfBoundsException
* if {@code byteCount < 0 || offset < 0 || offset + byteCount > data.length}
* @throws NullPointerException
* if {@code data == null}
*/
public static final String construct(byte[] data, int offset, int byteCount, Charset charset) {
try {
return new String(data, offset, byteCount, charset.name());
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException(e);
}
}
/**
* Returns a new byte array containing the characters of this string encoded using the
* given charset.
*
* <p>The behavior when this string cannot be represented in the given charset
* is to replace malformed input and unmappable characters with the charset's default
* replacement byte array. Use {@link java.nio.charset.CharsetEncoder} for more control.
*/
public static byte[] getBytes(String string, Charset charset) {
try {
return string.getBytes(charset.name());
} catch (UnsupportedEncodingException e) {
throw new IllegalArgumentException(e);
}
}
public static String bytesToHexString(byte[] bytes, boolean upperCase) {
char[] digits = upperCase ? UPPER_CASE_DIGITS : DIGITS;
char[] buf = new char[bytes.length * 2];
int c = 0;
for (byte b : bytes) {
buf[c++] = digits[(b >> 4) & 0xf];
buf[c++] = digits[b & 0xf];
}
return new String(buf);
}
}