/* * Copyright (c) 2000, 2010, Oracle and/or its affiliates. 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. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package sun.awt.motif; import java.nio.CharBuffer; import java.nio.charset.CoderResult; /** * Surrogate parsing support. Charset implementations may use instances of * this class to handle the details of parsing UTF-16 surrogate pairs. */ class SurrogateParser { public SurrogateParser() { } private int character; // UCS-4 private CoderResult error = CoderResult.UNDERFLOW; private boolean isPair; /** * Returns the UCS-4 character previously parsed. */ public int character() { assert (error == null); return character; } /** * Tells whether or not the previously-parsed UCS-4 character was * originally represented by a surrogate pair. */ public boolean isPair() { assert (error == null); return isPair; } /** * Returns the number of UTF-16 characters consumed by the previous * parse. */ public int increment() { assert (error == null); return isPair ? 2 : 1; } /** * If the previous parse operation detected an error, return the object * describing that error. */ public CoderResult error() { assert (error != null); return error; } /** * Returns an unmappable-input result object, with the appropriate * input length, for the previously-parsed character. */ public CoderResult unmappableResult() { assert (error == null); return CoderResult.unmappableForLength(isPair ? 2 : 1); } /** * Parses a UCS-4 character from the given source buffer, handling * surrogates. * * @param c The first character * @param in The source buffer, from which one more character * will be consumed if c is a high surrogate * * @returns Either a parsed UCS-4 character, in which case the isPair() * and increment() methods will return meaningful values, or * -1, in which case error() will return a descriptive result * object */ public int parse(char c, CharBuffer in) { if (Character.isHighSurrogate(c)) { if (!in.hasRemaining()) { error = CoderResult.UNDERFLOW; return -1; } char d = in.get(); if (Character.isLowSurrogate(d)) { character = Character.toCodePoint(c, d); isPair = true; error = null; return character; } error = CoderResult.malformedForLength(1); return -1; } if (Character.isLowSurrogate(c)) { error = CoderResult.malformedForLength(1); return -1; } character = c; isPair = false; error = null; return character; } /** * Parses a UCS-4 character from the given source buffer, handling * surrogates. * * @param c The first character * @param ia The input array, from which one more character * will be consumed if c is a high surrogate * @param ip The input index * @param il The input limit * * @returns Either a parsed UCS-4 character, in which case the isPair() * and increment() methods will return meaningful values, or * -1, in which case error() will return a descriptive result * object */ public int parse(char c, char[] ia, int ip, int il) { assert (ia[ip] == c); if (Character.isHighSurrogate(c)) { if (il - ip < 2) { error = CoderResult.UNDERFLOW; return -1; } char d = ia[ip + 1]; if (Character.isLowSurrogate(d)) { character = Character.toCodePoint(c, d); isPair = true; error = null; return character; } error = CoderResult.malformedForLength(1); return -1; } if (Character.isLowSurrogate(c)) { error = CoderResult.malformedForLength(1); return -1; } character = c; isPair = false; error = null; return character; } }