Class Utf8StringBuilder
- All Implemented Interfaces:
 CharsetStringBuilder
This class wraps a standard StringBuilder and provides methods to append
UTF-8 encoded bytes, that are converted into characters.
This class is stateful and up to 4 calls to append(byte) may be needed before
state a character is appended to the string buffer.
The UTF-8 decoding is done by this class and no additional buffers or Readers are used.  The algorithm is
fast fail, in that errors are detected as the bytes are appended.  However, no exceptions are thrown and
only the hasCodingErrors() method indicates the fast failure, otherwise the coding errors
are replaced and may be returned, unless the build() method is used, which may throw
CharacterCodingException. Already decoded characters may also be appended (e.g. append(char)
making this class suitable for decoding % encoded strings of already decoded characters.
- See Also:
 
- 
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic classNested classes/interfaces inherited from interface CharsetStringBuilder
CharsetStringBuilder.DecoderStringBuilder, CharsetStringBuilder.Iso88591StringBuilder, CharsetStringBuilder.UsAsciiStringBuilder - 
Field Summary
Fields - 
Constructor Summary
ConstructorsModifierConstructorDescriptionUtf8StringBuilder(int capacity) protectedUtf8StringBuilder(StringBuilder buffer) protectedUtf8StringBuilder(StringBuilder buffer, CodingErrorAction onMalformedInput, CodingErrorAction onUnmappableCharacter) Utf8StringBuilder(CodingErrorAction onMalformedInput, CodingErrorAction onUnmappableCharacter)  - 
Method Summary
Modifier and TypeMethodDescriptionvoidappend(byte b) voidappend(byte[] b) voidappend(byte[] b, int offset, int length) booleanappend(byte[] b, int offset, int length, int maxChars) voidappend(char c) voidvoidvoidappend(ByteBuffer buf) voidappendByte(byte b) protected voidbufferAppend(char c) protected voidbuild()Build the completed string and reset the buffer.build(boolean allowPartialString) Attempt to build the completed string and reset the buffer, returning a partial string if there are encoding errorsprotected voidvoidcomplete()Complete the appendable, adding a replacement character and coding error if the sequence is not currently complete.booleanTest for if there are and detected encoding errorsbooleanintlength()voidPartially reset the appendable: clear the buffer and clear any errors, but retain the decoding state of any partially decoded sequences.booleanIf there is an incomplete sequence, replace it with the encoding specific replacement character.voidreset()Reset the appendable, clearing the buffer, resetting decoding state and clearing any errors.takeCompleteString(Supplier<X> onCodingError) Take the completely decoded string.takePartialString(Supplier<X> onCodingError) Take the partially decoded string.Get the completely decoded string, which is equivalent to callingcomplete()thentoString().toString()Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface CharsetStringBuilder
append 
- 
Field Details
- 
REPLACEMENT
public static final char REPLACEMENT- See Also:
 
 - 
_state
protected int _state 
 - 
 - 
Constructor Details
- 
Utf8StringBuilder
public Utf8StringBuilder() - 
Utf8StringBuilder
public Utf8StringBuilder(CodingErrorAction onMalformedInput, CodingErrorAction onUnmappableCharacter)  - 
Utf8StringBuilder
public Utf8StringBuilder(int capacity)  - 
Utf8StringBuilder
 - 
Utf8StringBuilder
protected Utf8StringBuilder(StringBuilder buffer, CodingErrorAction onMalformedInput, CodingErrorAction onUnmappableCharacter)  
 - 
 - 
Method Details
- 
length
public int length()- Specified by:
 lengthin interfaceCharsetStringBuilder- Returns:
 - the length in characters
 
 - 
hasCodingErrors
public boolean hasCodingErrors()Description copied from interface:CharsetStringBuilderTest for if there are and detected encoding errors
- Specified by:
 hasCodingErrorsin interfaceCharsetStringBuilder- Returns:
 Trueif the characters decoded have contained UTF8 coding errors.
 - 
reset
public void reset()Reset the appendable, clearing the buffer, resetting decoding state and clearing any errors.- Specified by:
 resetin interfaceCharsetStringBuilder
 - 
partialReset
public void partialReset()Partially reset the appendable: clear the buffer and clear any errors, but retain the decoding state of any partially decoded sequences. - 
checkCharAppend
protected void checkCharAppend() - 
replaceIncomplete
public boolean replaceIncomplete()Description copied from interface:CharsetStringBuilderIf there is an incomplete sequence, replace it with the encoding specific replacement character.
Will set the encoding errors to true for
CharsetStringBuilder.hasCodingErrors()- Specified by:
 replaceIncompletein interfaceCharsetStringBuilder- Returns:
 - true if replacement occurred, false if there was no issue.
 
 - 
append
public void append(char c) - Specified by:
 appendin interfaceCharsetStringBuilder- Parameters:
 c- A decoded character to append
 - 
append
 - 
append
 - 
append
public void append(byte b) - Specified by:
 appendin interfaceCharsetStringBuilder- Parameters:
 b- An encoded byte to append
 - 
append
- Specified by:
 appendin interfaceCharsetStringBuilder- Parameters:
 buf- Buffer of encoded bytes to append. The bytes are consumed from the buffer.
 - 
append
public void append(byte[] b) - Specified by:
 appendin interfaceCharsetStringBuilder- Parameters:
 b- Array of encoded bytes to append
 - 
append
public void append(byte[] b, int offset, int length) - Specified by:
 appendin interfaceCharsetStringBuilder- Parameters:
 b- Array of encoded bytesoffset- offset into the arraylength- the number of bytes to append from the array.
 - 
append
public boolean append(byte[] b, int offset, int length, int maxChars)  - 
bufferAppend
protected void bufferAppend(char c)  - 
bufferReset
protected void bufferReset() - 
appendByte
- Throws:
 IOException
 - 
isComplete
public boolean isComplete()- Returns:
 Trueif the appended sequences are complete UTF-8 sequences.
 - 
complete
public void complete()Complete the appendable, adding a replacement character and coding error if the sequence is not currently complete. - 
toString
 - 
toPartialString
- Returns:
 - The currently decoded string, excluding any partial sequences appended.
 
 - 
toCompleteString
Get the completely decoded string, which is equivalent to callingcomplete()thentoString().- Returns:
 - The completely decoded string.
 
 - 
build
Attempt to build the completed string and reset the buffer, returning a partial string if there are encoding errors
- Specified by:
 buildin interfaceCharsetStringBuilder- Parameters:
 allowPartialString- true if a partial string is allowed to be returned, false means if complete string cannot be returned, an exception is thrown.- Returns:
 - The available string (complete or partial)
 - Throws:
 CharacterCodingException- (only ifallowPartialStringis false) thrown if the bytes cannot be correctly decoded or a multibyte sequence is incomplete.
 - 
takeCompleteString
Take the completely decoded string.- Type Parameters:
 X- The type of the exception thrown- Parameters:
 onCodingError- A supplier of aThrowableto use ifhasCodingErrors()returns true, or null for no error action- Returns:
 - The complete string.
 - Throws:
 X- ifhasCodingErrors()is true aftercomplete().
 - 
takePartialString
Take the partially decoded string.- Type Parameters:
 X- The type of the exception thrown- Parameters:
 onCodingError- A supplier of aThrowableto use ifhasCodingErrors()returns true, or null for no error action- Returns:
 - The complete string.
 - Throws:
 X- ifhasCodingErrors()is true aftercomplete().
 - 
build
Description copied from interface:CharsetStringBuilderBuild the completed string and reset the buffer.
- Specified by:
 buildin interfaceCharsetStringBuilder- Returns:
 - The decoded built string which must be complete in regard to any multibyte sequences.
 - Throws:
 CharacterCodingException- If the bytes cannot be correctly decoded or a multibyte sequence is incomplete.
 
 -