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. * * others. All Rights Reserved. *
******************************************************************************* *******************************************************************************
*/ */
package com.ibm.icu.charset; package com.ibm.icu.charset;
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetDecoder;
@ -40,15 +38,22 @@ class Charset88591 extends CharsetASCII {
return null; return null;
} }
protected CoderResult decodeLoopCoreUnoptimized(ByteBuffer source, CharBuffer target) protected CoderResult decodeLoopCoreUnoptimized(ByteBuffer source, CharBuffer target) {
throws BufferUnderflowException, BufferOverflowException { byte ch;
/* /*
* perform 88591 conversion from the source buffer to the target buffer. no range check * 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) while (source.hasRemaining()) {
target.put((char) (source.get() & 0xff)); 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; return null;
} }
protected final CoderResult encodeLoopCoreUnoptimized(CharBuffer source, ByteBuffer target, protected final CoderResult encodeLoopCoreUnoptimized(CharBuffer source, ByteBuffer target, boolean flush) {
boolean flush) throws BufferUnderflowException, BufferOverflowException {
int ch; int ch;
/* /*
@ -97,20 +101,25 @@ class Charset88591 extends CharsetASCII {
* each char in the source is within the correct range * each char in the source is within the correct range
*/ */
while (true) { while (source.hasRemaining()) {
ch = (int) source.get(); ch = (int) source.get();
if ((ch & 0xff00) == 0) { if ((ch & 0xff00) == 0) {
if (target.hasRemaining()) {
target.put((byte) ch); target.put((byte) ch);
} else { } else {
break; return CoderResult.OVERFLOW;
}
} }
} else {
/* /*
* if we reach here, it's because a character was not in the correct range, and we need * 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 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. * * others. All Rights Reserved. *
******************************************************************************* *******************************************************************************
* *
@ -8,8 +8,6 @@
*/ */
package com.ibm.icu.charset; package com.ibm.icu.charset;
import java.nio.BufferOverflowException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.CharBuffer; import java.nio.CharBuffer;
import java.nio.IntBuffer; import java.nio.IntBuffer;
@ -90,21 +88,10 @@ class CharsetASCII extends CharsetICU {
} }
} else { } else {
/* unoptimized loop */ /* 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); cr = decodeLoopCoreUnoptimized(source, target);
if (cr == CoderResult.OVERFLOW) {
} catch (BufferUnderflowException ex) {
/* all of the source has been read */
cr = CoderResult.UNDERFLOW;
} catch (BufferOverflowException ex) {
/* the target is full */ /* the target is full */
source.position(source.position() - 1); /* rewind by 1 */ source.position(source.position() - 1); /* rewind by 1 */
cr = CoderResult.OVERFLOW;
} }
} }
@ -142,23 +129,33 @@ class CharsetASCII extends CharsetICU {
return null; return null;
} }
protected CoderResult decodeLoopCoreUnoptimized(ByteBuffer source, CharBuffer target) protected CoderResult decodeLoopCoreUnoptimized(ByteBuffer source, CharBuffer target) {
throws BufferUnderflowException, BufferOverflowException {
int ch = 0; int ch = 0;
/* /*
* perform ascii conversion from the source buffer to the target buffer, making sure * perform ascii conversion from the source buffer to the target buffer, making sure
* each byte in the source is within the correct range * each byte in the source is within the correct range
*/ */
while (((ch = (source.get() & 0xff)) & 0x80) == 0) while (source.hasRemaining()) {
target.put((char) ch); 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 * 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 * to deak with this by calling decodeMalformedOrUnmappable
*/ */
return decodeMalformedOrUnmappable(ch); return decodeMalformedOrUnmappable(ch);
} }
}
return CoderResult.UNDERFLOW;
}
protected CoderResult decodeMalformedOrUnmappable(int ch) { protected CoderResult decodeMalformedOrUnmappable(int ch) {
/* /*
@ -248,18 +245,10 @@ class CharsetASCII extends CharsetICU {
} else { } else {
/* unoptimized loop */ /* 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); cr = encodeLoopCoreUnoptimized(source, target, flush);
} catch (BufferUnderflowException ex) { if (cr == CoderResult.OVERFLOW) {
cr = CoderResult.UNDERFLOW;
} catch (BufferOverflowException ex) {
source.position(source.position() - 1); /* rewind by 1 */ source.position(source.position() - 1); /* rewind by 1 */
cr = CoderResult.OVERFLOW;
} }
} }
} }
@ -299,23 +288,33 @@ class CharsetASCII extends CharsetICU {
return null; return null;
} }
protected CoderResult encodeLoopCoreUnoptimized(CharBuffer source, ByteBuffer target, protected CoderResult encodeLoopCoreUnoptimized(CharBuffer source, ByteBuffer target, boolean flush) {
boolean flush) throws BufferUnderflowException, BufferOverflowException {
int ch; int ch;
/* /*
* perform ascii conversion from the source buffer to the target buffer, making sure * perform ascii conversion from the source buffer to the target buffer, making sure
* each char in the source is within the correct range * each char in the source is within the correct range
*/ */
while (((ch = (int) source.get()) & 0xff80) == 0) while (source.hasRemaining()) {
target.put((byte) ch); 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 * 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 deak with this by calling encodeMalformedOrUnmappable.
*/ */
return encodeMalformedOrUnmappable(source, ch, flush); return encodeMalformedOrUnmappable(source, ch, flush);
} }
}
return CoderResult.UNDERFLOW;
}
protected final CoderResult encodeMalformedOrUnmappable(CharBuffer source, int ch, boolean flush) { protected final CoderResult encodeMalformedOrUnmappable(CharBuffer source, int ch, boolean flush) {
/* /*