Class StringEncoding

java.lang.Object
com.helger.base.string.StringEncoding

@Immutable public final class StringEncoding extends Object
Helper class for encoding char arrays to byte arrays and decoding byte arrays to char arrays using NIO charset codecs, avoiding intermediate String objects.
Author:
Philip Helger
  • Method Summary

    Modifier and Type
    Method
    Description
    static char @NonNull []
    decodeBytesToChars(byte @NonNull [] aByteArray, int nOfs, int nLen, @NonNull Charset aCharset)
    Decode a byte array to a char array using the provided charset.
    static char @NonNull []
    decodeBytesToChars(byte @NonNull [] aByteArray, @NonNull Charset aCharset)
    Decode a byte array to a char array using the provided charset.
    static byte @NonNull []
    encodeCharToBytes(char @NonNull [] aCharArray, int nOfs, int nLen, @NonNull Charset aCharset)
    Encode a char array to a byte array using the provided charset.
    static byte @NonNull []
    encodeCharToBytes(char @NonNull [] aCharArray, @NonNull Charset aCharset)
    Encode a char array to a byte array using the provided charset.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • encodeCharToBytes

      @ReturnsMutableCopy public static byte @NonNull [] encodeCharToBytes(char @NonNull [] aCharArray, @NonNull Charset aCharset)
      Encode a char array to a byte array using the provided charset. This does the same as new String (aCharArray).getBytes (aCharset) just without the intermediate objects.
      Parameters:
      aCharArray - The char array to be encoded. May not be null.
      aCharset - Charset to be used. May not be null.
      Returns:
      The created byte array. Never null.
      Since:
      8.6.4
    • encodeCharToBytes

      @ReturnsMutableCopy public static byte @NonNull [] encodeCharToBytes(char @NonNull [] aCharArray, @Nonnegative int nOfs, @Nonnegative int nLen, @NonNull Charset aCharset)
      Encode a char array to a byte array using the provided charset. This does the same as new String (aCharArray).getBytes (aCharset) just without the intermediate objects.
      Parameters:
      aCharArray - The char array to be encoded. May not be null.
      nOfs - Offset into char array. Must be ≥ 0.
      nLen - Chars to encode. Must be ≥ 0.
      aCharset - Charset to be used. May not be null.
      Returns:
      The created byte array. Never null.
      Since:
      8.6.4
    • decodeBytesToChars

      public static char @NonNull [] decodeBytesToChars(byte @NonNull [] aByteArray, @NonNull Charset aCharset)
      Decode a byte array to a char array using the provided charset. This does the same as new String (aByteArray, aCharset) just without the intermediate objects.
      Parameters:
      aByteArray - The byte array to be decoded. May not be null.
      aCharset - Charset to be used. May not be null.
      Returns:
      The created char array. Never null.
      Since:
      8.6.4
    • decodeBytesToChars

      public static char @NonNull [] decodeBytesToChars(byte @NonNull [] aByteArray, @Nonnegative int nOfs, @Nonnegative int nLen, @NonNull Charset aCharset)
      Decode a byte array to a char array using the provided charset. This does the same as new String (aByteArray, aCharset) just without the intermediate objects.
      Parameters:
      aByteArray - The byte array to be decoded. May not be null.
      nOfs - Offset into byte array. Must be ≥ 0.
      nLen - Bytes to encode. Must be ≥ 0.
      aCharset - Charset to be used. May not be null.
      Returns:
      The created char array. Never null.
      Since:
      8.6.4