*Now I've created the ultimate single-byte decoder loop.
It's internal work is done by 1 line of code. It's fast, because all exceptional circumstances are processed outside in the catch blocks.
The therein invoked subclassable decode method is customisable in any way.
See the full code of the following snippet here:
https://java-nio-charset-enhanced.dev.java.net/source/browse/java-nio-charset-enhanced/branches/milestone1/src/sun/nio/cs/SingleByteDecoder1.java?rev=93&view=markup <https://java-nio-charset-enhanced.dev.java.net/source/browse/java-nio-charset-enhanced/branches/milestone1/src/sun/nio/cs/SingleByteDecoder1.java?rev=93&view=markup>
*

*package* sun.nio.cs;

*import* java.nio.*;
*import* java.nio.charset.*;

*public* *abstract* *class* SingleByteDecoder1 *extends* CharsetDecoder {

   // cache it, because CoderResult.xxxForLength(int) invokes lame hash map 
access
   *private* *static* *final* CoderResult CODERRESULT_UNMAPPABLE = 
CoderResult.unmappableForLength(1);
   *private* *static* *final* CoderResult CODERRESULT_MALFORMED = 
CoderResult.malformedForLength(1);
   *protected* *static* *final* UnmappableCharacterException 
UNMAPPABLE_EXCEPTION = *new* UnmappableCharacterException(1);
   *protected* *static* *final* MalformedInputException MALFORMED_EXCEPTION = 
*new* MalformedInputException(1);

   *protected* *final* String byteToCharTable;

...

   *private* CoderResult decodeBuffersLoop(ByteBuffer src, CharBuffer dst) {
       *int* mark = src.position();
       *try* {
           *for* (; ; mark++)
               dst.put(decode(src.get()));
       } *catch* (UnmappableCharacterException e) {
           *return* CODERRESULT_UNMAPPABLE;
       } *catch* (MalformedInputException e) {
           *return* CODERRESULT_MALFORMED;
       } *catch* (BufferUnderflowException e) {
           *return* CoderResult.UNDERFLOW;
       } *catch* (BufferOverflowException e) {
           *return* CoderResult.OVERFLOW;
       } *finally* {
           src.position(mark);
       }
   }

   *protected* *char* decode(*byte* inByte) *throws* 
UnmappableCharacterException, MalformedInputException {
       *char* c = byteToCharTable.charAt(inByte + 0x80);
       *if* (c == '\uFFFD')
           *throw* UNMAPPABLE_EXCEPTION;
       *return* c;
   }
   *public* *char* decode(*int* inByte) { // for legacy use
       *try* {
           *if* (inByte >= byteToCharTable.length() - 128 || inByte < -128)
               *throw* MALFORMED_EXCEPTION;
           *return* decode((*char*)inByte);
       } *catch* (CharacterCodingException cce) {
           *return* '\uFFFD';
       }
   }
}


Reply via email to