|
@@ -4,6 +4,7 @@ import java.nio.ByteBuffer;
|
|
import java.nio.CharBuffer;
|
|
import java.nio.CharBuffer;
|
|
import java.nio.charset.CharacterCodingException;
|
|
import java.nio.charset.CharacterCodingException;
|
|
import java.nio.charset.Charset;
|
|
import java.nio.charset.Charset;
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
/**
|
|
* UTF-8 Charset encoder/decoder.
|
|
* UTF-8 Charset encoder/decoder.
|
|
@@ -20,7 +21,9 @@ public final class Utf8 {
|
|
*/
|
|
*/
|
|
public static byte[] encode(CharSequence string) {
|
|
public static byte[] encode(CharSequence string) {
|
|
try {
|
|
try {
|
|
- return CHARSET.newEncoder().encode(CharBuffer.wrap(string)).array();
|
|
|
|
|
|
+ ByteBuffer bytes = CHARSET.newEncoder().encode(CharBuffer.wrap(string));
|
|
|
|
+
|
|
|
|
+ return Arrays.copyOfRange(bytes.array(), 0, bytes.limit());
|
|
} catch (CharacterCodingException e) {
|
|
} catch (CharacterCodingException e) {
|
|
throw new IllegalArgumentException("Encoding failed", e);
|
|
throw new IllegalArgumentException("Encoding failed", e);
|
|
}
|
|
}
|
|
@@ -31,9 +34,9 @@ public final class Utf8 {
|
|
*/
|
|
*/
|
|
public static String decode(byte[] bytes) {
|
|
public static String decode(byte[] bytes) {
|
|
try {
|
|
try {
|
|
- return new String(CHARSET.newDecoder().decode(ByteBuffer.wrap(bytes)).array());
|
|
|
|
|
|
+ return CHARSET.newDecoder().decode(ByteBuffer.wrap(bytes)).toString();
|
|
} catch (CharacterCodingException e) {
|
|
} catch (CharacterCodingException e) {
|
|
- throw new IllegalArgumentException("Encoding failed", e);
|
|
|
|
|
|
+ throw new IllegalArgumentException("Decoding failed", e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|