public class CharsetUtils extends Object
byte[], String charset
issues, especially since Java 5 is less cool than Java 6 in this respect.
This code is mainly from the Trifork fork of the client and was written by
Krestan Krab and/or Erik Søe Sørensen.| Modifier and Type | Field and Description |
|---|---|
static Charset |
ASCII |
static Charset |
ISO_8859_1 |
static Charset |
UTF_8 |
| Constructor and Description |
|---|
CharsetUtils() |
| Modifier and Type | Method and Description |
|---|---|
static String |
addUtf8Charset(String contentType)
Adds the utf-8 charset to a content type.
|
static byte[] |
asBytes(String string,
Charset charset)
Turn a string into an array of bytes using the passed
Charset |
static String |
asString(byte[] bytes,
Charset charset)
Turns a byte[] array into a string in the provided
Charset |
static String |
asUTF8String(byte[] bytes)
Turns a byte[] array into a UTF8 string
|
static Charset |
getCharset(Map<String,String> headers)
Attempt to extract a charset from a
Map of HTTP
headers. |
static Charset |
getCharset(String contentType)
Attempts to parse the
Charset from a contentType string. |
static String |
getDeclaredCharset(String contentType)
Get the actual string value declared as the charset in a content-type
string, regardless of its validity.
|
static boolean |
hasCharset(String ctype)
Check if a content-type string has a charset field appended.
|
static byte[] |
utf8StringToBytes(String string)
Turn a UTF-8 encoded string into an array of bytes
|
public static Charset ASCII
public static Charset ISO_8859_1
public static Charset UTF_8
public static Charset getCharset(Map<String,String> headers)
Map of HTTP
headers. Really just pulls a the entry
Constants.HDR_CONTENT_LENGTH from the map and passes it to
getCharset(String)headers - a Map of HTTP headers (or anything, really).ISO_8859_1 if headers is null,
or result of calling getCharset(String) with
the content-type header from headersgetCharset(String)public static Charset getCharset(String contentType)
Charset from a contentType string.
If contentType is null or no charset declaration found, then UTF-8 is
returned. If the found Charset declaration is unknown on this platform
then a runtime exception is thrown.contentType - Charset parsed from a charset declaration in a
contentType String.public static String getDeclaredCharset(String contentType)
NOTE: this is different from getCharset, which will always return a default value.
contentType - the content-type stringpublic static String addUtf8Charset(String contentType)
contentType - contentType with ;charset=utf-8 appended.public static String asString(byte[] bytes, Charset charset)
Charsetbytes - charset - public static String asUTF8String(byte[] bytes)
bytes - charset - public static byte[] asBytes(String string, Charset charset)
Charsetstring - charset - public static byte[] utf8StringToBytes(String string)
string - public static boolean hasCharset(String ctype)
ctype - the content-type stringctype has a charset, false otherwiseCopyright © 2014. All Rights Reserved.