com.basho.riak.client.util
Class CharsetUtils

java.lang.Object
  extended by com.basho.riak.client.util.CharsetUtils

public class CharsetUtils
extends Object

Utils for dealing with 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.

Author:
russell

Field Summary
static Charset ASCII
           
static Charset ISO_8859_1
           
static Charset UTF_8
           
 
Constructor Summary
CharsetUtils()
           
 
Method Summary
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
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ASCII

public static Charset ASCII

ISO_8859_1

public static Charset ISO_8859_1

UTF_8

public static Charset UTF_8
Constructor Detail

CharsetUtils

public CharsetUtils()
Method Detail

getCharset

public static Charset getCharset(Map<String,String> headers)
Attempt to extract a charset from a Map of HTTP headers. Really just pulls a the entry Constants.HDR_CONTENT_LENGTH from the map and passes it to getCharset(String)

Parameters:
headers - a Map of HTTP headers (or anything, really).
Returns:
ISO_8859_1 if headers is null, or result of calling getCharset(String) with the content-type header from headers
See Also:
getCharset(String)

getCharset

public static Charset getCharset(String contentType)
Attempts to parse the 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.

Parameters:
contentType -
Returns:
a Charset parsed from a charset declaration in a contentType String.

getDeclaredCharset

public static String getDeclaredCharset(String contentType)
Get the actual string value declared as the charset in a content-type string, regardless of its validity.

NOTE: this is different from getCharset, which will always return a default value.

Parameters:
contentType - the content-type string
Returns:
the verbatim charset declared or null if non-exists

addUtf8Charset

public static String addUtf8Charset(String contentType)
Adds the utf-8 charset to a content type.

Parameters:
contentType -
Returns:
the contentType with ;charset=utf-8 appended.

asString

public static String asString(byte[] bytes,
                              Charset charset)
Turns a byte[] array into a string in the provided Charset

Parameters:
bytes -
charset -
Returns:
a String

asUTF8String

public static String asUTF8String(byte[] bytes)
Turns a byte[] array into a UTF8 string

Parameters:
bytes -
charset -
Returns:
a String

asBytes

public static byte[] asBytes(String string,
                             Charset charset)
Turn a string into an array of bytes using the passed Charset

Parameters:
string -
charset -
Returns:
a byte[] array

utf8StringToBytes

public static byte[] utf8StringToBytes(String string)
Turn a UTF-8 encoded string into an array of bytes

Parameters:
string -
Returns:

hasCharset

public static boolean hasCharset(String ctype)
Check if a content-type string has a charset field appended.

Parameters:
ctype - the content-type string
Returns:
true if ctype has a charset, false otherwise


Copyright © 2012. All Rights Reserved.