FileDocCategorySizeDatePackage
UTF7StyleCharsetDecoder.javaAPI DocAndroid 1.5 API6651Wed May 06 22:42:46 BST 2009com.beetstra.jutf7

UTF7StyleCharsetDecoder

public class UTF7StyleCharsetDecoder extends CharsetDecoder

The CharsetDecoder used to decode both variants of the UTF-7 charset and the modified-UTF-7 charset.

author
Jaap Beetstra

Fields Summary
private final Base64Util
base64
private final byte
shift
private final byte
unshift
private final boolean
strict
private boolean
base64mode
private int
bitsRead
private int
tempChar
private boolean
justShifted
private boolean
justUnshifted
Constructors Summary
UTF7StyleCharsetDecoder(UTF7StyleCharset cs, Base64Util base64, boolean strict)

        super(cs, 0.6f, 1.0f);
        this.base64 = base64;
        this.strict = strict;
        this.shift = cs.shift();
        this.unshift = cs.unshift();
    
Methods Summary
private booleanbase64bitsWaiting()

return
True if there are base64 encoded characters waiting to be written

        return tempChar != 0 || bitsRead >= 6;
    
protected java.nio.charset.CoderResultdecodeLoop(java.nio.ByteBuffer in, java.nio.CharBuffer out)

        while (in.hasRemaining()) {
            byte b = in.get();
            if (base64mode) {
                if (b == unshift) {
                    if (base64bitsWaiting())
                        return malformed(in);
                    if (justShifted) {
                        if (!out.hasRemaining())
                            return overflow(in);
                        out.put((char)shift);
                    } else
                        justUnshifted = true;
                    setUnshifted();
                } else {
                    if (!out.hasRemaining())
                        return overflow(in);
                    CoderResult result = handleBase64(in, out, b);
                    if (result != null)
                        return result;
                }
                justShifted = false;
            } else {
                if (b == shift) {
                    base64mode = true;
                    if (justUnshifted && strict)
                        return malformed(in);
                    justShifted = true;
                    continue;
                }
                if (!out.hasRemaining())
                    return overflow(in);
                out.put((char)b);
                justUnshifted = false;
            }
        }
        return CoderResult.UNDERFLOW;
    
private java.nio.charset.CoderResulthandleBase64(java.nio.ByteBuffer in, java.nio.CharBuffer out, byte lastRead)

Decodes a byte in base 64 mode. Will directly write a character to the output buffer if completed.

param
in The input buffer
param
out The output buffer
param
lastRead Last byte read from the input buffer
return
CoderResult.malformed if a non-base 64 character was encountered in strict mode, null otherwise

        CoderResult result = null;
        int sextet = base64.getSextet(lastRead);
        if (sextet >= 0) {
            bitsRead += 6;
            if (bitsRead < 16) {
                tempChar += sextet << (16 - bitsRead);
            } else {
                bitsRead -= 16;
                tempChar += sextet >> (bitsRead);
                out.put((char)tempChar);
                tempChar = (sextet << (16 - bitsRead)) & 0xFFFF;
            }
        } else {
            if (strict)
                return malformed(in);
            out.put((char)lastRead);
            if (base64bitsWaiting())
                result = malformed(in);
            setUnshifted();
        }
        return result;
    
protected java.nio.charset.CoderResultimplFlush(java.nio.CharBuffer out)

        if ((base64mode && strict) || base64bitsWaiting())
            return CoderResult.malformedForLength(1);
        return CoderResult.UNDERFLOW;
    
protected voidimplReset()

        setUnshifted();
        justUnshifted = false;
    
private java.nio.charset.CoderResultmalformed(java.nio.ByteBuffer in)

Resets the input buffer position to just before the last byte read, and returns a result indicating to skip the last byte.

param
in The input buffer
return
CoderResult.malformedForLength(1);

        in.position(in.position() - 1);
        return CoderResult.malformedForLength(1);
    
private java.nio.charset.CoderResultoverflow(java.nio.ByteBuffer in)

        in.position(in.position() - 1);
        return CoderResult.OVERFLOW;
    
private voidsetUnshifted()

Updates internal state to reflect the decoder is no longer in base 64 mode

        base64mode = false;
        bitsRead = 0;
        tempChar = 0;