Package com.helger.commons.charset
Class CharsetHelper
- java.lang.Object
-
- com.helger.commons.charset.CharsetHelper
-
@Immutable public final class CharsetHelper extends Object
Whole lotta charset management routines.- Author:
- Philip Helger
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCharsetHelper.InputStreamAndCharset
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static ICommonsOrderedMap<String,Charset>getAllCharsets()static StringgetAsStringInOtherCharset(String sText, Charset aCurrentCharset, Charset aNewCharset)static CharsetgetCharsetFromName(String sCharsetName)Resolve the charset by the specified name.static CharsetgetCharsetFromNameOrDefault(String sCharsetName, Charset aDefault)Resolve the charset by the specified name.static CharsetgetCharsetFromNameOrNull(String sCharsetName)Resolve the charset by the specified name.static CharsetHelper.InputStreamAndCharsetgetInputStreamAndCharsetFromBOM(InputStream aIS)If a BOM is present in theInputStreamit is read and if possible the charset is automatically determined from the BOM.static InputStreamReadergetReaderByBOM(InputStream aIS, Charset aFallbackCharset)static intgetUTF8ByteCount(char c)static intgetUTF8ByteCount(char[] aChars)Get the number of bytes necessary to represent the passed char array as an UTF-8 string.static intgetUTF8ByteCount(int c)Get the number of bytes necessary to represent the passed character.static intgetUTF8ByteCount(String s)Get the number of bytes necessary to represent the passed string as an UTF-8 string.
-
-
-
Method Detail
-
getCharsetFromName
@Nonnull public static Charset getCharsetFromName(@Nonnull @Nonempty String sCharsetName)
Resolve the charset by the specified name. The difference toCharset.forName(String)is, that this method has no checked exceptions but only unchecked exceptions.- Parameters:
sCharsetName- The charset to be resolved. May neither benullnor empty.- Returns:
- The Charset object
- Throws:
IllegalArgumentException- If the charset could not be resolved.
-
getCharsetFromNameOrNull
@Nullable public static Charset getCharsetFromNameOrNull(@Nullable String sCharsetName)
Resolve the charset by the specified name. The difference toCharset.forName(String)is, that this method throws no exceptions.- Parameters:
sCharsetName- The charset to be resolved. May benullor empty.- Returns:
- The Charset object or
nullif no such charset was found.
-
getCharsetFromNameOrDefault
@Nullable public static Charset getCharsetFromNameOrDefault(@Nullable String sCharsetName, @Nullable Charset aDefault)
Resolve the charset by the specified name. The difference toCharset.forName(String)is, that this method throws no exceptions.- Parameters:
sCharsetName- The charset to be resolved. May benullor empty.aDefault- the default charset to be returned if none is provided. May benull.- Returns:
- The Charset object or the provided default if no such charset was found.
-
getAllCharsets
@Nonnull @ReturnsMutableCopy public static ICommonsOrderedMap<String,Charset> getAllCharsets()
- Returns:
- An immutable collection of all available charsets from the standard charset provider.
-
getAsStringInOtherCharset
@Nullable public static String getAsStringInOtherCharset(@Nullable String sText, @Nonnull Charset aCurrentCharset, @Nonnull Charset aNewCharset)
-
getUTF8ByteCount
@Nonnegative public static int getUTF8ByteCount(@Nullable String s)
Get the number of bytes necessary to represent the passed string as an UTF-8 string.- Parameters:
s- The string to count the length. May benullor empty.- Returns:
- A non-negative value.
-
getUTF8ByteCount
@Nonnegative public static int getUTF8ByteCount(@Nullable char[] aChars)
Get the number of bytes necessary to represent the passed char array as an UTF-8 string.- Parameters:
aChars- The characters to count the length. May benullor empty.- Returns:
- A non-negative value.
-
getUTF8ByteCount
@Nonnegative public static int getUTF8ByteCount(char c)
-
getUTF8ByteCount
@Nonnegative public static int getUTF8ByteCount(@Nonnegative int c)
Get the number of bytes necessary to represent the passed character.- Parameters:
c- The character to be evaluated.- Returns:
- A non-negative value.
-
getInputStreamAndCharsetFromBOM
@Nonnull public static CharsetHelper.InputStreamAndCharset getInputStreamAndCharsetFromBOM(@Nonnull @WillNotClose InputStream aIS)
If a BOM is present in theInputStreamit is read and if possible the charset is automatically determined from the BOM.- Parameters:
aIS- The input stream to use. May not benull.- Returns:
- Never
null. Always use the input stream contained in the returned object and never the one passed in as a parameter, because the returned IS is a push-back InputStream that has a couple of bytes already buffered!
-
getReaderByBOM
@Nonnull public static InputStreamReader getReaderByBOM(@Nonnull InputStream aIS, @Nonnull Charset aFallbackCharset)
-
-