public class CharsetUtils extends Object
byte
, String
charset
issues
This code is mainly from the Trifork fork of the original HTTP 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 |
addCharset(Charset charset,
String contentType) |
static String |
addCharset(String charset,
String contentType)
Adds the charset to a content type.
|
static String |
addUtf8Charset(String contentType)
Adds the utf-8 charset to a content type.
|
static String |
asASCIIString(byte[] bytes) |
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(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(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 addCharset(String charset, String contentType)
contentType
- contentType
with charset.name()
appended.public static String asString(byte[] bytes, Charset charset)
Charset
bytes
- charset
- public static String asUTF8String(byte[] bytes)
bytes
- public static String asASCIIString(byte[] bytes)
public static byte[] asBytes(String string, Charset charset)
Charset
string
- 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.