FileDocCategorySizeDatePackage
CoderResult.javaAPI DocJava SE 5 API8545Fri Aug 26 14:57:10 BST 2005java.nio.charset

CoderResult

public class CoderResult extends Object
A description of the result state of a coder.

A charset coder, that is, either a decoder or an encoder, consumes bytes (or characters) from an input buffer, translates them, and writes the resulting characters (or bytes) to an output buffer. A coding process terminates for one of four categories of reasons, which are described by instances of this class:

  • Underflow is reported when there is no more input to be processed, or there is insufficient input and additional input is required. This condition is represented by the unique result object {@link #UNDERFLOW}, whose {@link #isUnderflow() isUnderflow} method returns true.

  • Overflow is reported when there is insufficient room remaining in the output buffer. This condition is represented by the unique result object {@link #OVERFLOW}, whose {@link #isOverflow() isOverflow} method returns true.

  • A malformed-input error is reported when a sequence of input units is not well-formed. Such errors are described by instances of this class whose {@link #isMalformed() isMalformed} method returns true and whose {@link #length() length} method returns the length of the malformed sequence. There is one unique instance of this class for all malformed-input errors of a given length.

  • An unmappable-character error is reported when a sequence of input units denotes a character that cannot be represented in the output charset. Such errors are described by instances of this class whose {@link #isUnmappable() isUnmappable} method returns true and whose {@link #length() length} method returns the length of the input sequence denoting the unmappable character. There is one unique instance of this class for all unmappable-character errors of a given length.

For convenience, the {@link #isError() isError} method returns true for result objects that describe malformed-input and unmappable-character errors but false for those that describe underflow or overflow conditions.

author
Mark Reinhold
author
JSR-51 Expert Group
version
1.8, 03/12/19
since
1.4

Fields Summary
private static final int
CR_UNDERFLOW
private static final int
CR_OVERFLOW
private static final int
CR_ERROR_MIN
private static final int
CR_MALFORMED
private static final int
CR_UNMAPPABLE
private static final String[]
names
private final int
type
private final int
length
public static final CoderResult
UNDERFLOW
Result object indicating underflow, meaning that either the input buffer has been completely consumed or, if the input buffer is not yet empty, that additional input is required.

public static final CoderResult
OVERFLOW
Result object indicating overflow, meaning that there is insufficient room in the output buffer.

private static Cache
malformedCache
private static Cache
unmappableCache
Constructors Summary
private CoderResult(int type, int length)


         
	this.type = type;
	this.length = length;
    
Methods Summary
public booleanisError()
Tells whether or not this object describes an error condition.

return
true if, and only if, this object denotes either a malformed-input error or an unmappable-character error

	return (type >= CR_ERROR_MIN);
    
public booleanisMalformed()
Tells whether or not this object describes a malformed-input error.

return
true if, and only if, this object denotes a malformed-input error

	return (type == CR_MALFORMED);
    
public booleanisOverflow()
Tells whether or not this object describes an overflow condition.

return
true if, and only if, this object denotes overflow

	return (type == CR_OVERFLOW);
    
public booleanisUnderflow()
Tells whether or not this object describes an underflow condition.

return
true if, and only if, this object denotes underflow

	return (type == CR_UNDERFLOW);
    
public booleanisUnmappable()
Tells whether or not this object describes an unmappable-character error.

return
true if, and only if, this object denotes an unmappable-character error

	return (type == CR_UNMAPPABLE);
    
public intlength()
Returns the length of the erroneous input described by this object  (optional operation).

return
The length of the erroneous input, a positive integer
throws
UnsupportedOperationException If this object does not describe an error condition, that is, if the {@link #isError() isError} does not return true

	if (!isError())
	    throw new UnsupportedOperationException();
	return length;
    
public static java.nio.charset.CoderResultmalformedForLength(int length)
Static factory method that returns the unique object describing a malformed-input error of the given length.

return
The requested coder-result object


                                 
         
	return malformedCache.get(length);
    
public voidthrowException()
Throws an exception appropriate to the result described by this object.

throws
BufferUnderflowException If this object is {@link #UNDERFLOW}
throws
BufferOverflowException If this object is {@link #OVERFLOW}
throws
MalformedInputException If this object represents a malformed-input error; the exception's length value will be that of this object
throws
UnmappableCharacterException If this object represents an unmappable-character error; the exceptions length value will be that of this object

	switch (type) {
	case CR_UNDERFLOW:   throw new BufferUnderflowException();
	case CR_OVERFLOW:    throw new BufferOverflowException();
	case CR_MALFORMED:   throw new MalformedInputException(length);
	case CR_UNMAPPABLE:  throw new UnmappableCharacterException(length);
	default:
	    assert false;
	}
    
public java.lang.StringtoString()
Returns a string describing this coder result.

return
A descriptive string

	String nm = names[type];
	return isError() ? nm + "[" + length + "]" : nm;
    
public static java.nio.charset.CoderResultunmappableForLength(int length)
Static factory method that returns the unique result object describing an unmappable-character error of the given length.

return
The requested coder-result object


                                  
         
	return unmappableCache.get(length);