Path: blob/master/src/java.base/share/classes/sun/nio/cs/SingleByte.java
67862 views
/*1* Copyright (c) 2008, 2021, 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*/2425package sun.nio.cs;2627import jdk.internal.access.JavaLangAccess;28import jdk.internal.access.SharedSecrets;2930import java.nio.Buffer;31import java.nio.ByteBuffer;32import java.nio.CharBuffer;33import java.nio.charset.Charset;34import java.nio.charset.CharsetDecoder;35import java.nio.charset.CharsetEncoder;36import java.nio.charset.CoderResult;37import java.util.Arrays;38import static sun.nio.cs.CharsetMapping.*;3940public class SingleByte41{42private static final CoderResult withResult(CoderResult cr,43Buffer src, int sp,44Buffer dst, int dp)45{46src.position(sp - src.arrayOffset());47dst.position(dp - dst.arrayOffset());48return cr;49}5051private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();5253public static final class Decoder extends CharsetDecoder54implements ArrayDecoder {5556private final char[] b2c;57private final boolean isASCIICompatible;58private final boolean isLatin1Decodable;5960public Decoder(Charset cs, char[] b2c) {61super(cs, 1.0f, 1.0f);62this.b2c = b2c;63this.isASCIICompatible = false;64this.isLatin1Decodable = false;65}6667public Decoder(Charset cs, char[] b2c, boolean isASCIICompatible) {68super(cs, 1.0f, 1.0f);69this.b2c = b2c;70this.isASCIICompatible = isASCIICompatible;71this.isLatin1Decodable = false;72}7374public Decoder(Charset cs, char[] b2c, boolean isASCIICompatible, boolean isLatin1Decodable) {75super(cs, 1.0f, 1.0f);76this.b2c = b2c;77this.isASCIICompatible = isASCIICompatible;78this.isLatin1Decodable = isLatin1Decodable;79}8081private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {82byte[] sa = src.array();83int sp = src.arrayOffset() + src.position();84int sl = src.arrayOffset() + src.limit();8586char[] da = dst.array();87int dp = dst.arrayOffset() + dst.position();88int dl = dst.arrayOffset() + dst.limit();8990CoderResult cr = CoderResult.UNDERFLOW;91if ((dl - dp) < (sl - sp)) {92sl = sp + (dl - dp);93cr = CoderResult.OVERFLOW;94}9596if (isASCIICompatible) {97int n = JLA.decodeASCII(sa, sp, da, dp, Math.min(dl - dp, sl - sp));98sp += n;99dp += n;100}101while (sp < sl) {102char c = decode(sa[sp]);103if (c == UNMAPPABLE_DECODING) {104return withResult(CoderResult.unmappableForLength(1),105src, sp, dst, dp);106}107da[dp++] = c;108sp++;109}110return withResult(cr, src, sp, dst, dp);111}112113private CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {114int mark = src.position();115try {116while (src.hasRemaining()) {117char c = decode(src.get());118if (c == UNMAPPABLE_DECODING)119return CoderResult.unmappableForLength(1);120if (!dst.hasRemaining())121return CoderResult.OVERFLOW;122dst.put(c);123mark++;124}125return CoderResult.UNDERFLOW;126} finally {127src.position(mark);128}129}130131protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {132if (src.hasArray() && dst.hasArray())133return decodeArrayLoop(src, dst);134else135return decodeBufferLoop(src, dst);136}137138public final char decode(int b) {139return b2c[b + 128];140}141142private char repl = '\uFFFD';143protected void implReplaceWith(String newReplacement) {144repl = newReplacement.charAt(0);145}146147@Override148public int decodeToLatin1(byte[] src, int sp, int len, byte[] dst) {149if (len > dst.length)150len = dst.length;151152int dp = 0;153while (dp < len) {154dst[dp++] = (byte)decode(src[sp++]);155}156return dp;157}158159@Override160public int decode(byte[] src, int sp, int len, char[] dst) {161if (len > dst.length)162len = dst.length;163int dp = 0;164while (dp < len) {165dst[dp] = decode(src[sp++]);166if (dst[dp] == UNMAPPABLE_DECODING) {167dst[dp] = repl;168}169dp++;170}171return dp;172}173174@Override175public boolean isASCIICompatible() {176return isASCIICompatible;177}178179@Override180public boolean isLatin1Decodable() {181return isLatin1Decodable;182}183}184185public static final class Encoder extends CharsetEncoder186implements ArrayEncoder {187private Surrogate.Parser sgp;188private final char[] c2b;189private final char[] c2bIndex;190private final boolean isASCIICompatible;191192public Encoder(Charset cs, char[] c2b, char[] c2bIndex, boolean isASCIICompatible) {193super(cs, 1.0f, 1.0f);194this.c2b = c2b;195this.c2bIndex = c2bIndex;196this.isASCIICompatible = isASCIICompatible;197}198199public boolean canEncode(char c) {200return encode(c) != UNMAPPABLE_ENCODING;201}202203public boolean isLegalReplacement(byte[] repl) {204return ((repl.length == 1 && repl[0] == (byte)'?') ||205super.isLegalReplacement(repl));206}207208private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {209char[] sa = src.array();210int sp = src.arrayOffset() + src.position();211int sl = src.arrayOffset() + src.limit();212213byte[] da = dst.array();214int dp = dst.arrayOffset() + dst.position();215int dl = dst.arrayOffset() + dst.limit();216int len = Math.min(dl - dp, sl - sp);217218if (isASCIICompatible) {219int n = JLA.encodeASCII(sa, sp, da, dp, len);220sp += n;221dp += n;222len -= n;223}224while (len-- > 0) {225char c = sa[sp];226int b = encode(c);227if (b == UNMAPPABLE_ENCODING) {228if (Character.isSurrogate(c)) {229if (sgp == null)230sgp = new Surrogate.Parser();231if (sgp.parse(c, sa, sp, sl) < 0) {232return withResult(sgp.error(), src, sp, dst, dp);233}234return withResult(sgp.unmappableResult(), src, sp, dst, dp);235}236return withResult(CoderResult.unmappableForLength(1),237src, sp, dst, dp);238}239da[dp++] = (byte)b;240sp++;241}242return withResult(sp < sl ? CoderResult.OVERFLOW : CoderResult.UNDERFLOW,243src, sp, dst, dp);244}245246private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {247int mark = src.position();248try {249while (src.hasRemaining()) {250char c = src.get();251int b = encode(c);252if (b == UNMAPPABLE_ENCODING) {253if (Character.isSurrogate(c)) {254if (sgp == null)255sgp = new Surrogate.Parser();256if (sgp.parse(c, src) < 0)257return sgp.error();258return sgp.unmappableResult();259}260return CoderResult.unmappableForLength(1);261}262if (!dst.hasRemaining())263return CoderResult.OVERFLOW;264dst.put((byte)b);265mark++;266}267return CoderResult.UNDERFLOW;268} finally {269src.position(mark);270}271}272273protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {274if (src.hasArray() && dst.hasArray())275return encodeArrayLoop(src, dst);276else277return encodeBufferLoop(src, dst);278}279280public final int encode(char ch) {281char index = c2bIndex[ch >> 8];282if (index == UNMAPPABLE_ENCODING)283return UNMAPPABLE_ENCODING;284return c2b[index + (ch & 0xff)];285}286287private byte repl = (byte)'?';288protected void implReplaceWith(byte[] newReplacement) {289repl = newReplacement[0];290}291292public int encode(char[] src, int sp, int len, byte[] dst) {293int dp = 0;294int sl = sp + Math.min(len, dst.length);295while (sp < sl) {296char c = src[sp++];297int b = encode(c);298if (b != UNMAPPABLE_ENCODING) {299dst[dp++] = (byte)b;300continue;301}302if (Character.isHighSurrogate(c) && sp < sl &&303Character.isLowSurrogate(src[sp])) {304if (len > dst.length) {305sl++;306len--;307}308sp++;309}310dst[dp++] = repl;311}312return dp;313}314315@Override316public int encodeFromLatin1(byte[] src, int sp, int len, byte[] dst) {317int dp = 0;318int sl = sp + Math.min(len, dst.length);319while (sp < sl) {320char c = (char)(src[sp++] & 0xff);321int b = encode(c);322if (b == UNMAPPABLE_ENCODING) {323dst[dp++] = repl;324} else {325dst[dp++] = (byte)b;326}327}328return dp;329}330331@Override332public int encodeFromUTF16(byte[] src, int sp, int len, byte[] dst) {333int dp = 0;334int sl = sp + Math.min(len, dst.length);335while (sp < sl) {336char c = StringUTF16.getChar(src, sp++);337int b = encode(c);338if (b != UNMAPPABLE_ENCODING) {339dst[dp++] = (byte)b;340continue;341}342if (Character.isHighSurrogate(c) && sp < sl &&343Character.isLowSurrogate(StringUTF16.getChar(src, sp))) {344if (len > dst.length) {345sl++;346len--;347}348sp++;349}350dst[dp++] = repl;351}352return dp;353}354355@Override356public boolean isASCIICompatible() {357return isASCIICompatible;358}359}360361// init the c2b and c2bIndex tables from b2c.362public static void initC2B(char[] b2c, char[] c2bNR,363char[] c2b, char[] c2bIndex) {364for (int i = 0; i < c2bIndex.length; i++)365c2bIndex[i] = UNMAPPABLE_ENCODING;366for (int i = 0; i < c2b.length; i++)367c2b[i] = UNMAPPABLE_ENCODING;368int off = 0;369for (int i = 0; i < b2c.length; i++) {370char c = b2c[i];371if (c == UNMAPPABLE_DECODING)372continue;373int index = (c >> 8);374if (c2bIndex[index] == UNMAPPABLE_ENCODING) {375c2bIndex[index] = (char)off;376off += 0x100;377}378index = c2bIndex[index] + (c & 0xff);379c2b[index] = (char)((i>=0x80)?(i-0x80):(i+0x80));380}381if (c2bNR != null) {382// c-->b nr entries383int i = 0;384while (i < c2bNR.length) {385char b = c2bNR[i++];386char c = c2bNR[i++];387int index = (c >> 8);388if (c2bIndex[index] == UNMAPPABLE_ENCODING) {389c2bIndex[index] = (char)off;390off += 0x100;391}392index = c2bIndex[index] + (c & 0xff);393c2b[index] = b;394}395}396}397}398399400