Class Utf8StringDecoder

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

    public class Utf8StringDecoder
    extends java.lang.Object
    Decodes Strings from UTF-8. Instances of this class are reusable but are NOT threadsafe. Instances are vended by Pool.getOrCreate(). Users are expected to call Closeable.close() when the decoder is no longer needed. There are two ways of using this class:
    1. Use decode(ByteBuffer, int) to decode the requested number of bytes from the given ByteBuffer in a single step. Or,
    2. Use the following sequence of method calls:
      1. prepareDecode(int) to prepare the decoder to decode the requested number of bytes.
      2. partialDecode(ByteBuffer, boolean) to decode the available bytes from the byte buffer. This may be repeated as more bytes are made available in the ByteBuffer, which is the caller's responsibility.
      3. finishDecode() to finish decoding and return the resulting String.
      Note: decode(ByteBuffer, int) must not be called between calls to prepareDecode(int) and finishDecode().
    • 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.
      java.lang.String decode​(java.nio.ByteBuffer utf8InputBuffer, int numberOfBytes)
      Decodes the given number of UTF-8 bytes from the given ByteBuffer into a Java String.
      java.lang.String finishDecode()
      Finishes decoding and returns the resulting String.
      void partialDecode​(java.nio.ByteBuffer utf8InputBuffer, boolean endOfInput)
      Decodes the available bytes from the given ByteBuffer.
      void prepareDecode​(int numberOfBytes)
      Prepares the decoder to decode the given number of UTF-8 bytes.
      • Methods inherited from class java.lang.Object

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

      • prepareDecode

        public void prepareDecode​(int numberOfBytes)
        Prepares the decoder to decode the given number of UTF-8 bytes.
        Parameters:
        numberOfBytes - the number of bytes to decode.
      • partialDecode

        public void partialDecode​(java.nio.ByteBuffer utf8InputBuffer,
                                  boolean endOfInput)
        Decodes the available bytes from the given ByteBuffer.
        Parameters:
        utf8InputBuffer - a ByteBuffer containing UTF-8 bytes.
        endOfInput - true if the end of the UTF-8 sequence is expected to occur in the buffer; otherwise, false.
      • finishDecode

        public java.lang.String finishDecode()
        Finishes decoding and returns the resulting String.
        Returns:
        the decoded Java String.
      • decode

        public java.lang.String decode​(java.nio.ByteBuffer utf8InputBuffer,
                                       int numberOfBytes)
        Decodes the given number of UTF-8 bytes from the given ByteBuffer into a Java String.
        Parameters:
        utf8InputBuffer - a ByteBuffer containing UTF-8 bytes.
        numberOfBytes - the number of bytes from the utf8InputBuffer to decode.
        Returns:
        the decoded Java String.
      • 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