Class Utf8StringEncoder

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class Utf8StringEncoder
    extends java.lang.Object
    Encodes Strings to UTF-8. Instances of this class are reusable but are NOT threadsafe. Instances are vended by Pool.getOrCreate(). encode(String) can be called any number of times. Users are expected to call Closeable.close() when the encoder is no longer needed.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Attempts to return this instance to the pool with which it is associated, if any.
      Utf8StringEncoder.Result encode​(java.lang.String text)
      Encodes the provided String's text to UTF-8.
      • Methods inherited from class java.lang.Object

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

      • encode

        public Utf8StringEncoder.Result encode​(java.lang.String text)
        Encodes the provided String's text to UTF-8. Unlike String.getBytes(Charset), this method will not silently replace characters that cannot be encoded with a substitute character. Instead, it will throw an IllegalArgumentException. Some resources in the returned Utf8StringEncoder.Result may be reused across calls to this method. Consequently, callers should use the Result and discard it immediately.
        Parameters:
        text - A Java String to encode as UTF8 bytes.
        Returns:
        A Utf8StringEncoder.Result containing a byte array of UTF-8 bytes and encoded length.
        Throws:
        java.lang.IllegalArgumentException - if the String cannot be encoded as UTF-8.
      • close

        public void close()
        Attempts to return this instance to the pool with which it is associated, if any. Do not continue to use this instance after calling this method.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable