Class Utf8StringDecoder

java.lang.Object
com.amazon.ion.impl.bin.utf8.Utf8StringDecoder
All Implemented Interfaces:
Closeable, AutoCloseable

public class Utf8StringDecoder extends 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 Details

    • 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(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 String finishDecode()
      Finishes decoding and returns the resulting String.
      Returns:
      the decoded Java String.
    • decode

      public String decode(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 AutoCloseable
      Specified by:
      close in interface Closeable