public class StringUtils extends Object
| Constructor and Description |
|---|
StringUtils() |
| Modifier and Type | Method and Description |
|---|---|
static String |
fromASCII(byte[] bytes) |
static String |
fromASCII(byte[] bytes,
int offset,
int len) |
static CharBuffer |
fromASCII(ByteBuffer bytes) |
static String |
fromUTF8(byte[] bytes) |
static String |
fromUTF8(byte[] bytes,
int offset,
int len) |
static CharBuffer |
fromUTF8(ByteBuffer bytes) |
static ByteBuffer |
toASCII(CharBuffer chars) |
static byte[] |
toASCII(String str) |
static ByteBuffer |
toUTF8(CharBuffer chars) |
static byte[] |
toUTF8(String str)
In all src and test code, the String(byte[], ...) constructor and
String.getBytes method must always be passed a Charset, to avoid
portability issues.
|
public static byte[] toUTF8(String str)
In most cases, the UTF8 or ASCII charset should be used for portability. UTF8 should be used when any character may be represented. ASCII can be used when all characters are in the ASCII range. The default charset should only be used when handling user-input data directly, e.g., console input/output or user-visible files.
Rather than passing the charset as a string (getBytes("UTF-8")), the Charset objects defined here should be passed (getBytes(UTF8)). Not only is using a Charset object slightly more efficient because it avoids a lookup, even more importantly it avoids having to clutter code with a catch for java.io.UnsupportedEncodingException, which should never be thrown for the "UTF-8" or "US-ASCII" charsets.
public static ByteBuffer toUTF8(CharBuffer chars)
public static String fromUTF8(byte[] bytes)
public static String fromUTF8(byte[] bytes, int offset, int len)
public static CharBuffer fromUTF8(ByteBuffer bytes)
public static byte[] toASCII(String str)
public static ByteBuffer toASCII(CharBuffer chars)
public static String fromASCII(byte[] bytes)
public static String fromASCII(byte[] bytes, int offset, int len)
public static CharBuffer fromASCII(ByteBuffer bytes)
Copyright © 2024. All rights reserved.