/**
*******************************************************************************
- * 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.IntBuffer;
}
} 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) {
+ cr = decodeLoopCoreUnoptimized(source, target);
+ if (cr == CoderResult.OVERFLOW) {
/* the target is full */
source.position(source.position() - 1); /* rewind by 1 */
- cr = CoderResult.OVERFLOW;
}
}
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);
-
- /*
- * 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);
+ 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) {
}
} 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) {
+
+ cr = encodeLoopCoreUnoptimized(source, target, flush);
+
+ if (cr == CoderResult.OVERFLOW) {
source.position(source.position() - 1); /* rewind by 1 */
- cr = CoderResult.OVERFLOW;
}
}
}
* early termination of the loop
*/
if ((ch & 0xff80) != 0) {
- source.position(i + 1);
+ source.position((i + 1) - source.arrayOffset());
target.position(i + offset);
return encodeMalformedOrUnmappable(source, ch, flush);
} else
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);
-
- /*
- * 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);
+ 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) {