|
@@ -82,11 +82,21 @@ public class DFSUtilClient {
|
|
|
public static final byte[] EMPTY_BYTES = {};
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(
|
|
|
DFSUtilClient.class);
|
|
|
+
|
|
|
+ // Using the charset canonical name for String/byte[] conversions is much
|
|
|
+ // more efficient due to use of cached encoders/decoders.
|
|
|
+ private static final String UTF8_CSN = StandardCharsets.UTF_8.name();
|
|
|
+
|
|
|
/**
|
|
|
* Converts a string to a byte array using UTF8 encoding.
|
|
|
*/
|
|
|
public static byte[] string2Bytes(String str) {
|
|
|
- return str.getBytes(StandardCharsets.UTF_8);
|
|
|
+ try {
|
|
|
+ return str.getBytes(UTF8_CSN);
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ // should never happen!
|
|
|
+ throw new IllegalArgumentException("UTF8 decoding is not supported", e);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -272,13 +282,13 @@ public class DFSUtilClient {
|
|
|
* @param length The number of bytes to decode
|
|
|
* @return The decoded string
|
|
|
*/
|
|
|
- private static String bytes2String(byte[] bytes, int offset, int length) {
|
|
|
+ static String bytes2String(byte[] bytes, int offset, int length) {
|
|
|
try {
|
|
|
- return new String(bytes, offset, length, "UTF8");
|
|
|
- } catch(UnsupportedEncodingException e) {
|
|
|
- assert false : "UTF8 encoding is not supported ";
|
|
|
+ return new String(bytes, offset, length, UTF8_CSN);
|
|
|
+ } catch (UnsupportedEncodingException e) {
|
|
|
+ // should never happen!
|
|
|
+ throw new IllegalArgumentException("UTF8 encoding is not supported", e);
|
|
|
}
|
|
|
- return null;
|
|
|
}
|
|
|
|
|
|
/**
|