Methods Summary |
---|
protected java.nio.charset.CoderResult | decodeLoop(java.nio.ByteBuffer in, java.nio.CharBuffer out)Decodes one or more bytes. The default behaviour of the converter
is stop and report if an error in input stream is encountered.
To set different behaviour use @see CharsetDecoder.onMalformedInput()
This method allows a buffer by buffer conversion of a data stream.
The state of the conversion is saved between calls to convert.
Among other things, this means multibyte input sequences can be
split between calls. If a call to convert results in an Error, the
conversion may be continued by calling convert again with suitably
modified parameters.All conversions should be finished with a call to
the flush method.
if(!in.hasRemaining()){
return CoderResult.UNDERFLOW;
}
data[INPUT_OFFSET] = getArray(in);
data[OUTPUT_OFFSET]= getArray(out);
data[INPUT_HELD] = 0;
try{
/* do the conversion */
ec=NativeConverter.decode(
converterHandle, /* Handle to ICU Converter */
input, /* input array of bytes */
inEnd, /* last index+1 to be converted */
output, /* input array of chars */
outEnd, /* input index+1 to be written */
data, /* contains data, inOff,outOff */
false /* donot flush the data */
);
/* return an error*/
if(ec == ErrorCode.U_BUFFER_OVERFLOW_ERROR){
return CoderResult.OVERFLOW;
}else if(ec==ErrorCode.U_INVALID_CHAR_FOUND){
return CoderResult.malformedForLength(data[INVALID_BYTES]);
}else if(ec==ErrorCode.U_ILLEGAL_CHAR_FOUND){
return CoderResult.malformedForLength(data[INVALID_BYTES]);
}
/* decoding action succeded */
return CoderResult.UNDERFLOW;
}finally{
setPosition(in);
setPosition(out);
}
|
protected void | finalize()Releases the system resources by cleanly closing ICU converter opened
NativeConverter.closeConverter(converterHandle);
super.finalize();
converterHandle = 0;
|
private final int | getArray(java.nio.ByteBuffer in)
if(in.hasArray()){
input = in.array();
inEnd = in.limit();
return in.position()+savedInputHeldLen;/*exclude the number fo bytes held in previous conversion*/
}else{
inEnd = in.remaining();
if(input==null|| (inEnd > input.length)){
input = new byte[inEnd];
}
// save the current position
int pos = in.position();
in.get(input,0,inEnd);
// reset the position
in.position(pos);
// the start position
// of the new buffer
// is whatever is savedInputLen
return savedInputHeldLen;
}
|
private final int | getArray(java.nio.CharBuffer out)
if(out.hasArray()){
output = out.array();
outEnd = out.limit();
return out.position();
}else{
outEnd = out.remaining();
if(output==null || (outEnd > output.length)){
output = new char[outEnd];
}
//since the new
// buffer start position
// is 0
return 0;
}
|
protected final java.nio.charset.CoderResult | implFlush(java.nio.CharBuffer out)Flushes any characters saved in the converter's internal buffer and
resets the converter.
try{
data[OUTPUT_OFFSET] = getArray(out);
ec=NativeConverter.flushByteToChar(
converterHandle, /* Handle to ICU Converter */
output, /* input array of chars */
outEnd, /* input index+1 to be written */
data /* contains data, inOff,outOff */
);
/* If we don't have room for the output, throw an exception*/
if (ErrorCode.isFailure(ec)) {
if (ec == ErrorCode.U_BUFFER_OVERFLOW_ERROR) {
return CoderResult.OVERFLOW;
}else if (ec == ErrorCode.U_TRUNCATED_CHAR_FOUND ) {//CSDL: add this truncated character error handling
if(data[INPUT_OFFSET]>0){
return CoderResult.malformedForLength(data[INPUT_OFFSET]);
}
}else {
ErrorCode.getException(ec);
}
}
return CoderResult.UNDERFLOW;
}finally{
/* save the flushed data */
setPosition(out);
implReset();
}
|
protected final void | implOnMalformedInput(java.nio.charset.CodingErrorAction newAction)Sets the action to be taken if an illegal sequence is encountered
if(newAction.equals(CodingErrorAction.IGNORE)){
onMalformedInput = NativeConverter.SKIP_CALLBACK;
}else if(newAction.equals(CodingErrorAction.REPLACE)){
onMalformedInput = NativeConverter.SUBSTITUTE_CALLBACK;
}else if(newAction.equals(CodingErrorAction.REPORT)){
onMalformedInput = NativeConverter.STOP_CALLBACK;
}
char[] sub = replacement().toCharArray();
//System.out.println(" setting callbacks mfi " + onMalformedInput +" umi " + onUnmappableInput);
ec = NativeConverter.setCallbackDecode(converterHandle, onMalformedInput, onUnmappableInput, sub, sub.length);
if(ErrorCode.isFailure(ec)){
throw ErrorCode.getException(ec);
}
|
protected final void | implOnUnmappableCharacter(java.nio.charset.CodingErrorAction newAction)Sets the action to be taken if an illegal sequence is encountered
if(newAction.equals(CodingErrorAction.IGNORE)){
onUnmappableInput = NativeConverter.SKIP_CALLBACK;
}else if(newAction.equals(CodingErrorAction.REPLACE)){
onUnmappableInput = NativeConverter.SUBSTITUTE_CALLBACK;
}else if(newAction.equals(CodingErrorAction.REPORT)){
onUnmappableInput = NativeConverter.STOP_CALLBACK;
}
char[] sub = replacement().toCharArray();
ec = NativeConverter.setCallbackDecode(converterHandle,onMalformedInput, onUnmappableInput, sub, sub.length);
if(ErrorCode.isFailure(ec)){
throw ErrorCode.getException(ec);
}
|
protected void | implReplaceWith(java.lang.String newReplacement)Sets this decoders replacement string. Substitutes the string in input if an
umappable or illegal sequence is encountered
if(converterHandle > 0){
if( newReplacement.length() > NativeConverter.getMaxBytesPerChar(converterHandle)) {
throw new IllegalArgumentException();
}
ec =NativeConverter.setSubstitutionChars(converterHandle,
newReplacement.toCharArray(),
newReplacement.length()
);
if(ErrorCode.isFailure(ec)){
throw ErrorCode.getException(ec);
}
}
|
protected void | implReset()Resets the to Unicode mode of converter
NativeConverter.resetByteToChar(converterHandle);
data[INPUT_OFFSET] = 0;
data[OUTPUT_OFFSET] = 0;
data[INVALID_BYTES] = 0;
data[INPUT_HELD] = 0;
savedInputHeldLen = 0;
output = null;
input = null;
|
private final void | setPosition(java.nio.CharBuffer out)
if(out.hasArray()){
out.position(out.position() + data[OUTPUT_OFFSET]);
}else{
out.put(output,0,data[OUTPUT_OFFSET]);
}
|
private final void | setPosition(java.nio.ByteBuffer in)
// ok was there input held in the previous invocation of decodeLoop
// that resulted in output in this invocation?
if(data[OUTPUT_OFFSET]>0 && savedInputHeldLen >0){
int len = in.position() + data[INPUT_OFFSET] + savedInputHeldLen;
in.position(len);
savedInputHeldLen = data[INPUT_HELD];
}else{
in.position(in.position() + data[INPUT_OFFSET] + savedInputHeldLen);
savedInputHeldLen = data[INPUT_HELD];
in.position(in.position() - savedInputHeldLen);
}
|