Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/nio/cs/ext/SimpleEUCEncoder.java
38919 views
/*1* Copyright (c) 2003, 2006, 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.nio.cs.ext;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;363738public abstract class SimpleEUCEncoder39extends CharsetEncoder40{4142protected short index1[];43protected String index2;44protected String index2a;45protected String index2b;46protected String index2c;47protected int mask1;48protected int mask2;49protected int shift;5051private byte[] outputByte = new byte[4];52private final Surrogate.Parser sgp = new Surrogate.Parser();5354protected SimpleEUCEncoder(Charset cs)55{56super(cs, 3.0f, 4.0f);57}5859/**60* Returns true if the given character can be converted to the61* target character encoding.62*/6364public boolean canEncode(char ch) {65int index;66String theChars;6768index = index1[((ch & mask1) >> shift)] + (ch & mask2);6970if (index < 7500)71theChars = index2;72else73if (index < 15000) {74index = index - 7500;75theChars = index2a;76}77else78if (index < 22500){79index = index - 15000;80theChars = index2b;81}82else {83index = index - 22500;84theChars = index2c;85}8687if (theChars.charAt(2*index) != '\u0000' ||88theChars.charAt(2*index + 1) != '\u0000')89return (true);9091// only return true if input char was unicode null - all others are92// undefined93return( ch == '\u0000');9495}96private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {97char[] sa = src.array();98int sp = src.arrayOffset() + src.position();99int sl = src.arrayOffset() + src.limit();100assert (sp <= sl);101sp = (sp <= sl ? sp : sl);102byte[] da = dst.array();103int dp = dst.arrayOffset() + dst.position();104int dl = dst.arrayOffset() + dst.limit();105assert (dp <= dl);106dp = (dp <= dl ? dp : dl);107108int index;109int spaceNeeded;110int i;111112try {113while (sp < sl) {114boolean allZeroes = true;115char inputChar = sa[sp];116if (Character.isSurrogate(inputChar)) {117if (sgp.parse(inputChar, sa, sp, sl) < 0)118return sgp.error();119return sgp.unmappableResult();120}121122if (inputChar >= '\uFFFE')123return CoderResult.unmappableForLength(1);124125String theChars;126char aChar;127128// We have a valid character, get the bytes for it129index = index1[((inputChar & mask1) >> shift)] + (inputChar & mask2);130131if (index < 7500)132theChars = index2;133else if (index < 15000) {134index = index - 7500;135theChars = index2a;136} else if (index < 22500){137index = index - 15000;138theChars = index2b;139}140else {141index = index - 22500;142theChars = index2c;143}144145aChar = theChars.charAt(2*index);146outputByte[0] = (byte)((aChar & 0xff00)>>8);147outputByte[1] = (byte)(aChar & 0x00ff);148aChar = theChars.charAt(2*index + 1);149outputByte[2] = (byte)((aChar & 0xff00)>>8);150outputByte[3] = (byte)(aChar & 0x00ff);151152for (i = 0; i < outputByte.length; i++) {153if (outputByte[i] != 0x00) {154allZeroes = false;155break;156}157}158159if (allZeroes && inputChar != '\u0000') {160return CoderResult.unmappableForLength(1);161}162163int oindex = 0;164165for (spaceNeeded = outputByte.length;166spaceNeeded > 1; spaceNeeded--){167if (outputByte[oindex++] != 0x00 )168break;169}170171if (dp + spaceNeeded > dl)172return CoderResult.OVERFLOW;173174for (i = outputByte.length - spaceNeeded;175i < outputByte.length; i++) {176da[dp++] = outputByte[i];177}178sp++;179}180return CoderResult.UNDERFLOW;181} finally {182src.position(sp - src.arrayOffset());183dst.position(dp - dst.arrayOffset());184}185}186187private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {188int index;189int spaceNeeded;190int i;191int mark = src.position();192try {193while (src.hasRemaining()) {194char inputChar = src.get();195boolean allZeroes = true;196if (Character.isSurrogate(inputChar)) {197if (sgp.parse(inputChar, src) < 0)198return sgp.error();199return sgp.unmappableResult();200}201202if (inputChar >= '\uFFFE')203return CoderResult.unmappableForLength(1);204205String theChars;206char aChar;207208// We have a valid character, get the bytes for it209index = index1[((inputChar & mask1) >> shift)] + (inputChar & mask2);210211if (index < 7500)212theChars = index2;213else if (index < 15000) {214index = index - 7500;215theChars = index2a;216} else if (index < 22500){217index = index - 15000;218theChars = index2b;219}220else {221index = index - 22500;222theChars = index2c;223}224225aChar = theChars.charAt(2*index);226outputByte[0] = (byte)((aChar & 0xff00)>>8);227outputByte[1] = (byte)(aChar & 0x00ff);228aChar = theChars.charAt(2*index + 1);229outputByte[2] = (byte)((aChar & 0xff00)>>8);230outputByte[3] = (byte)(aChar & 0x00ff);231232for (i = 0; i < outputByte.length; i++) {233if (outputByte[i] != 0x00) {234allZeroes = false;235break;236}237}238if (allZeroes && inputChar != '\u0000') {239return CoderResult.unmappableForLength(1);240}241242int oindex = 0;243244for (spaceNeeded = outputByte.length;245spaceNeeded > 1; spaceNeeded--){246if (outputByte[oindex++] != 0x00 )247break;248}249if (dst.remaining() < spaceNeeded)250return CoderResult.OVERFLOW;251252for (i = outputByte.length - spaceNeeded;253i < outputByte.length; i++) {254dst.put(outputByte[i]);255}256mark++;257}258return CoderResult.UNDERFLOW;259} finally {260src.position(mark);261}262}263264protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {265if (true && src.hasArray() && dst.hasArray())266return encodeArrayLoop(src, dst);267else268return encodeBufferLoop(src, dst);269}270271public byte encode(char inputChar) {272return (byte)index2.charAt(index1[(inputChar & mask1) >> shift] +273(inputChar & mask2));274}275}276277278