Path: blob/master/src/java.desktop/unix/classes/sun/font/DoubleByteEncoder.java
41153 views
/*1* Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26*/2728package sun.font;2930import java.nio.ByteBuffer;31import java.nio.CharBuffer;32import java.nio.charset.Charset;33import java.nio.charset.CharsetEncoder;34import java.nio.charset.CoderResult;35import sun.nio.cs.Surrogate;3637public abstract class DoubleByteEncoder38extends CharsetEncoder39{4041private short[] index1;42private String[] index2;4344private final Surrogate.Parser sgp = new Surrogate.Parser();4546protected DoubleByteEncoder(Charset cs,47short[] index1, String[] index2)48{49super(cs, 2.0f, 2.0f);50this.index1 = index1;51this.index2 = index2;52}5354protected DoubleByteEncoder(Charset cs,55short[] index1, String[] index2,56float avg, float max)57{58super(cs, avg, max);59this.index1 = index1;60this.index2 = index2;61}6263protected DoubleByteEncoder(Charset cs,64short[] index1, String[] index2, byte[] repl)65{66super(cs, 2.0f, 2.0f, repl);67this.index1 = index1;68this.index2 = index2;69}707172protected DoubleByteEncoder(Charset cs,73short[] index1, String[] index2,74byte[] repl, float avg, float max)75{76super(cs, avg, max,repl);77this.index1 = index1;78this.index2 = index2;79}8081public boolean canEncode(char c) {82return (encodeSingle(c) != -1 ||83encodeDouble(c) != 0);84}8586private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {87char[] sa = src.array();88int sp = src.arrayOffset() + src.position();89int sl = src.arrayOffset() + src.limit();90byte[] da = dst.array();91int dp = dst.arrayOffset() + dst.position();92int dl = dst.arrayOffset() + dst.limit();9394try {95while (sp < sl) {96char c = sa[sp];97if (Character.isSurrogate(c)) {98if (sgp.parse(c, sa, sp, sl) < 0)99return sgp.error();100if (sl - sp < 2)101return CoderResult.UNDERFLOW;102char c2 = sa[sp + 1];103104byte[] outputBytes = new byte[2];105outputBytes = encodeSurrogate(c, c2);106107if (outputBytes == null) {108return sgp.unmappableResult();109}110else {111if (dl - dp < 2)112return CoderResult.OVERFLOW;113da[dp++] = outputBytes[0];114da[dp++] = outputBytes[1];115sp += 2;116continue;117}118}119if (c >= '\uFFFE')120return CoderResult.unmappableForLength(1);121122int b = encodeSingle(c);123if (b != -1) { // Single Byte124if (dl - dp < 1)125return CoderResult.OVERFLOW;126da[dp++] = (byte)b;127sp++;128continue;129}130131int ncode = encodeDouble(c);132if (ncode != 0 && c != '\u0000' ) {133if (dl - dp < 2)134return CoderResult.OVERFLOW;135da[dp++] = (byte) ((ncode & 0xff00) >> 8);136da[dp++] = (byte) (ncode & 0xff);137sp++;138continue;139}140return CoderResult.unmappableForLength(1);141}142return CoderResult.UNDERFLOW;143} finally {144src.position(sp - src.arrayOffset());145dst.position(dp - dst.arrayOffset());146}147}148149private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {150int mark = src.position();151152try {153while (src.hasRemaining()) {154char c = src.get();155if (Character.isSurrogate(c)) {156int surr;157if ((surr = sgp.parse(c, src)) < 0)158return sgp.error();159char c2 = Surrogate.low(surr);160byte[] outputBytes = new byte[2];161outputBytes = encodeSurrogate(c, c2);162163if (outputBytes == null) {164return sgp.unmappableResult();165} else {166if (dst.remaining() < 2)167return CoderResult.OVERFLOW;168mark += 2;169dst.put(outputBytes[0]);170dst.put(outputBytes[1]);171continue;172}173}174if (c >= '\uFFFE')175return CoderResult.unmappableForLength(1);176int b = encodeSingle(c);177178if (b != -1) { // Single-byte character179if (dst.remaining() < 1)180return CoderResult.OVERFLOW;181mark++;182dst.put((byte)b);183continue;184}185// Double Byte character186187int ncode = encodeDouble(c);188if (ncode != 0 && c != '\u0000') {189if (dst.remaining() < 2)190return CoderResult.OVERFLOW;191mark++;192dst.put((byte) ((ncode & 0xff00) >> 8));193dst.put((byte) ncode);194continue;195}196return CoderResult.unmappableForLength(1);197}198199return CoderResult.UNDERFLOW;200} finally {201src.position(mark);202}203}204205protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {206if (true && src.hasArray() && dst.hasArray())207return encodeArrayLoop(src, dst);208else209return encodeBufferLoop(src, dst);210}211212/*213* Can be changed by subclass214*/215protected int encodeDouble(char ch) {216int offset = index1[((ch & 0xff00) >> 8 )] << 8;217return index2[offset >> 12].charAt((offset & 0xfff) + (ch & 0xff));218}219220/*221* Can be changed by subclass222*/223protected int encodeSingle(char inputChar) {224if (inputChar < 0x80)225return (byte)inputChar;226else227return -1;228}229230/**231* Protected method which should be overridden by concrete DBCS232* CharsetEncoder classes which included supplementary characters233* within their mapping coverage.234* null return value indicates surrogate values could not be235* handled or encoded.236*/237protected byte[] encodeSurrogate(char highSurrogate, char lowSurrogate) {238return null;239}240}241242243