You need to sign in or sign up before continuing.
UnicodeDecoder.java 4.6 KB
Newer Older
D
duke 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
/*
 * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

package sun.nio.cs;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CoderResult;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.MalformedInputException;


abstract class UnicodeDecoder extends CharsetDecoder {

    protected static final char BYTE_ORDER_MARK = (char) 0xfeff;
    protected static final char REVERSED_MARK = (char) 0xfffe;

    protected static final int NONE = 0;
    protected static final int BIG = 1;
    protected static final int LITTLE = 2;

    private final int expectedByteOrder;
    private int currentByteOrder;
    private int defaultByteOrder = BIG;

    public UnicodeDecoder(Charset cs, int bo) {
        super(cs, 0.5f, 1.0f);
        expectedByteOrder = currentByteOrder = bo;
    }

    public UnicodeDecoder(Charset cs, int bo, int defaultBO) {
        this(cs, bo);
        defaultByteOrder = defaultBO;
    }

    private char decode(int b1, int b2) {
        if (currentByteOrder == BIG)
            return (char)((b1 << 8) | b2);
        else
            return (char)((b2 << 8) | b1);
    }

    protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
        int mark = src.position();

        try {
            while (src.remaining() > 1) {
                int b1 = src.get() & 0xff;
                int b2 = src.get() & 0xff;

                // Byte Order Mark interpretation
                if (currentByteOrder == NONE) {
                    char c = (char)((b1 << 8) | b2);
                    if (c == BYTE_ORDER_MARK) {
                        currentByteOrder = BIG;
                        mark += 2;
                        continue;
                    } else if (c == REVERSED_MARK) {
                        currentByteOrder = LITTLE;
                        mark += 2;
                        continue;
                    } else {
                        currentByteOrder = defaultByteOrder;
                        // FALL THROUGH to process b1, b2 normally
                    }
                }

                char c = decode(b1, b2);

                if (c == REVERSED_MARK) {
                    // A reversed BOM cannot occur within middle of stream
                    return CoderResult.malformedForLength(2);
                }

                // Surrogates
M
martin 已提交
100 101
                if (Character.isSurrogate(c)) {
                    if (Character.isHighSurrogate(c)) {
D
duke 已提交
102 103 104
                        if (src.remaining() < 2)
                            return CoderResult.UNDERFLOW;
                        char c2 = decode(src.get() & 0xff, src.get() & 0xff);
M
martin 已提交
105
                        if (!Character.isLowSurrogate(c2))
D
duke 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
                            return CoderResult.malformedForLength(4);
                        if (dst.remaining() < 2)
                            return CoderResult.OVERFLOW;
                        mark += 4;
                        dst.put(c);
                        dst.put(c2);
                        continue;
                    }
                    // Unpaired low surrogate
                    return CoderResult.malformedForLength(2);
                }

                if (!dst.hasRemaining())
                    return CoderResult.OVERFLOW;
                mark += 2;
                dst.put(c);

            }
            return CoderResult.UNDERFLOW;

        } finally {
            src.position(mark);
        }
    }

    protected void implReset() {
        currentByteOrder = expectedByteOrder;
    }

}