Class CharsetHelper

java.lang.Object
com.helger.base.charset.CharsetHelper

@Immutable public final class CharsetHelper extends Object
Whole lotta charset management routines.
Author:
Philip Helger
  • Method Details

    • getCharsetFromName

      public static @NonNull Charset getCharsetFromName(@Nonempty @NonNull @Nonempty String sCharsetName)
      Resolve the charset by the specified name. The difference to Charset.forName(String) is, that this method has no checked exceptions but only unchecked exceptions.
      Parameters:
      sCharsetName - The charset to be resolved. May neither be null nor empty.
      Returns:
      The Charset object
      Throws:
      IllegalArgumentException - If the charset could not be resolved.
    • getCharsetFromNameOrNull

      public static @Nullable Charset getCharsetFromNameOrNull(@Nullable String sCharsetName)
      Resolve the charset by the specified name. The difference to Charset.forName(String) is, that this method throws no exceptions.
      Parameters:
      sCharsetName - The charset to be resolved. May be null or empty.
      Returns:
      The Charset object or null if no such charset was found.
    • getCharsetFromNameOrDefault

      public static @Nullable Charset getCharsetFromNameOrDefault(@Nullable String sCharsetName, @Nullable Charset aDefault)
      Resolve the charset by the specified name. The difference to Charset.forName(String) is, that this method throws no exceptions.
      Parameters:
      sCharsetName - The charset to be resolved. May be null or empty.
      aDefault - the default charset to be returned if none is provided. May be null.
      Returns:
      The Charset object or the provided default if no such charset was found.
    • getAllCharsets

      @ReturnsMutableCopy public static @NonNull Map<String,Charset> getAllCharsets()
      Returns:
      An immutable collection of all available charsets from the standard charset provider.
    • getAsStringInOtherCharset

      public static @Nullable String getAsStringInOtherCharset(@Nullable String sText, @NonNull Charset aCurrentCharset, @NonNull Charset aNewCharset)
      Convert the passed string from one charset to another.
      Parameters:
      sText - The text to convert. May be null.
      aCurrentCharset - The current charset of the text. May not be null.
      aNewCharset - The target charset. May not be null.
      Returns:
      null if the input text is null, the converted string otherwise.
    • 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 be null or empty.
      Returns:
      A non-negative value.
    • getUTF8ByteCount

      @Nonnegative public static int getUTF8ByteCount(char @Nullable [] 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 be null or empty.
      Returns:
      A non-negative value.
    • getUTF8ByteCount

      @Nonnegative public static int getUTF8ByteCount(char c)
      Get the number of bytes necessary to represent the passed character as UTF-8.
      Parameters:
      c - The character to be evaluated.
      Returns:
      A non-negative value.
    • 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

      public static @NonNull CharsetHelper.InputStreamAndCharset getInputStreamAndCharsetFromBOM(@WillNotClose @NonNull InputStream aIS)
      If a BOM is present in the InputStream it is read and if possible the charset is automatically determined from the BOM.
      Parameters:
      aIS - The input stream to use. May not be null.
      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

      public static @NonNull InputStreamReader getReaderByBOM(@NonNull InputStream aIS, @NonNull Charset aFallbackCharset)
      Create an InputStreamReader that detects the charset from a potential Unicode BOM in the input stream.
      Parameters:
      aIS - The input stream to read from. May not be null.
      aFallbackCharset - The charset to use if no BOM is detected. May not be null.
      Returns:
      A non-null InputStreamReader using the detected or fallback charset.