ICU-8612 Remove exception usage in normal code path of CharsetASCII and Charset88591

X-SVN-Rev: 30238
This commit is contained in:
Michael Ow 2011-06-27 18:06:05 +00:00
parent cfe479cd47
commit a2b657a9a3
2 changed files with 76 additions and 68 deletions

View File

@ -1,14 +1,12 @@
/**
*******************************************************************************
* Copyright (C) 2006-2008, International Business Machines Corporation and *
* Copyright (C) 2006-2011, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
package com.ibm.icu.charset;
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharsetDecoder;
@ -40,15 +38,22 @@ class Charset88591 extends CharsetASCII {
return null;
}
protected CoderResult decodeLoopCoreUnoptimized(ByteBuffer source, CharBuffer target)
throws BufferUnderflowException, BufferOverflowException {
protected CoderResult decodeLoopCoreUnoptimized(ByteBuffer source, CharBuffer target) {
byte ch;
/*
* perform 88591 conversion from the source buffer to the target buffer. no range check
* is necessary (an exception will be generated to end the loop).
* is necessary.
*/
while (true)
target.put((char) (source.get() & 0xff));
while (source.hasRemaining()) {
ch = source.get();
if (target.hasRemaining()) {
target.put((char) (ch & 0xff));
} else {
return CoderResult.OVERFLOW;
}
}
return CoderResult.UNDERFLOW;
}
}
@ -88,8 +93,7 @@ class Charset88591 extends CharsetASCII {
return null;
}
protected final CoderResult encodeLoopCoreUnoptimized(CharBuffer source, ByteBuffer target,
boolean flush) throws BufferUnderflowException, BufferOverflowException {
protected final CoderResult encodeLoopCoreUnoptimized(CharBuffer source, ByteBuffer target, boolean flush) {
int ch;
/*
@ -97,20 +101,25 @@ class Charset88591 extends CharsetASCII {
* each char in the source is within the correct range
*/
while (true) {
while (source.hasRemaining()) {
ch = (int) source.get();
if ((ch & 0xff00) == 0) {
if (target.hasRemaining()) {
target.put((byte) ch);
} else {
break;
}
return CoderResult.OVERFLOW;
}
} else {
/*
* if we reach here, it's because a character was not in the correct range, and we need
* to deak with this by calling encodeMalformedOrUnmappable.
* to deal with this by calling encodeMalformedOrUnmappable.
*/
return encodeMalformedOrUnmappable(source, ch, flush);
}
}
return CoderResult.UNDERFLOW;
}
}

View File

@ -1,6 +1,6 @@
/**
*******************************************************************************
* Copyright (C) 2006-2008, International Business Machines Corporation and *
* Copyright (C) 2006-2011, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
@ -8,8 +8,6 @@
*/
package com.ibm.icu.charset;
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.IntBuffer;
@ -90,21 +88,10 @@ class CharsetASCII extends CharsetICU {
}
} else {
/* unoptimized loop */
try {
/*
* perform the core loop... if it throws an exception, it must be due to an
* overflow or underflow
*/
cr = decodeLoopCoreUnoptimized(source, target);
} catch (BufferUnderflowException ex) {
/* all of the source has been read */
cr = CoderResult.UNDERFLOW;
} catch (BufferOverflowException ex) {
if (cr == CoderResult.OVERFLOW) {
/* the target is full */
source.position(source.position() - 1); /* rewind by 1 */
cr = CoderResult.OVERFLOW;
}
}
@ -142,23 +129,33 @@ class CharsetASCII extends CharsetICU {
return null;
}
protected CoderResult decodeLoopCoreUnoptimized(ByteBuffer source, CharBuffer target)
throws BufferUnderflowException, BufferOverflowException {
protected CoderResult decodeLoopCoreUnoptimized(ByteBuffer source, CharBuffer target) {
int ch = 0;
/*
* perform ascii conversion from the source buffer to the target buffer, making sure
* each byte in the source is within the correct range
*/
while (((ch = (source.get() & 0xff)) & 0x80) == 0)
target.put((char) ch);
while (source.hasRemaining()) {
ch = source.get() & 0xff;
if ((ch & 0x80) == 0) {
if (target.hasRemaining()) {
target.put((char)ch);
} else {
return CoderResult.OVERFLOW;
}
} else {
/*
* if we reach here, it's because a character was not in the correct range, and we need
* to deak with this by calling decodeMalformedOrUnmappable
*/
return decodeMalformedOrUnmappable(ch);
}
}
return CoderResult.UNDERFLOW;
}
protected CoderResult decodeMalformedOrUnmappable(int ch) {
/*
@ -248,18 +245,10 @@ class CharsetASCII extends CharsetICU {
} else {
/* unoptimized loop */
try {
/*
* perform the core loop... if it throws an exception, it must be due to an
* overflow or underflow
*/
cr = encodeLoopCoreUnoptimized(source, target, flush);
} catch (BufferUnderflowException ex) {
cr = CoderResult.UNDERFLOW;
} catch (BufferOverflowException ex) {
if (cr == CoderResult.OVERFLOW) {
source.position(source.position() - 1); /* rewind by 1 */
cr = CoderResult.OVERFLOW;
}
}
}
@ -299,23 +288,33 @@ class CharsetASCII extends CharsetICU {
return null;
}
protected CoderResult encodeLoopCoreUnoptimized(CharBuffer source, ByteBuffer target,
boolean flush) throws BufferUnderflowException, BufferOverflowException {
protected CoderResult encodeLoopCoreUnoptimized(CharBuffer source, ByteBuffer target, boolean flush) {
int ch;
/*
* perform ascii conversion from the source buffer to the target buffer, making sure
* each char in the source is within the correct range
*/
while (((ch = (int) source.get()) & 0xff80) == 0)
target.put((byte) ch);
while (source.hasRemaining()) {
ch = (int) source.get();
if ((ch & 0xff80) == 0) {
if (target.hasRemaining()) {
target.put((byte) ch);
} else {
return CoderResult.OVERFLOW;
}
} else {
/*
* if we reach here, it's because a character was not in the correct range, and we need
* to deak with this by calling encodeMalformedOrUnmappable.
*/
return encodeMalformedOrUnmappable(source, ch, flush);
}
}
return CoderResult.UNDERFLOW;
}
protected final CoderResult encodeMalformedOrUnmappable(CharBuffer source, int ch, boolean flush) {
/*