Path: blob/master/src/java.base/share/classes/sun/nio/cs/SingleByte.java
41159 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}5051public static final class Decoder extends CharsetDecoder52implements ArrayDecoder {5354private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();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);217218while (len-- > 0) {219char c = sa[sp];220int b = encode(c);221if (b == UNMAPPABLE_ENCODING) {222if (Character.isSurrogate(c)) {223if (sgp == null)224sgp = new Surrogate.Parser();225if (sgp.parse(c, sa, sp, sl) < 0) {226return withResult(sgp.error(), src, sp, dst, dp);227}228return withResult(sgp.unmappableResult(), src, sp, dst, dp);229}230return withResult(CoderResult.unmappableForLength(1),231src, sp, dst, dp);232}233da[dp++] = (byte)b;234sp++;235}236return withResult(sp < sl ? CoderResult.OVERFLOW : CoderResult.UNDERFLOW,237src, sp, dst, dp);238}239240private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {241int mark = src.position();242try {243while (src.hasRemaining()) {244char c = src.get();245int b = encode(c);246if (b == UNMAPPABLE_ENCODING) {247if (Character.isSurrogate(c)) {248if (sgp == null)249sgp = new Surrogate.Parser();250if (sgp.parse(c, src) < 0)251return sgp.error();252return sgp.unmappableResult();253}254return CoderResult.unmappableForLength(1);255}256if (!dst.hasRemaining())257return CoderResult.OVERFLOW;258dst.put((byte)b);259mark++;260}261return CoderResult.UNDERFLOW;262} finally {263src.position(mark);264}265}266267protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {268if (src.hasArray() && dst.hasArray())269return encodeArrayLoop(src, dst);270else271return encodeBufferLoop(src, dst);272}273274public final int encode(char ch) {275char index = c2bIndex[ch >> 8];276if (index == UNMAPPABLE_ENCODING)277return UNMAPPABLE_ENCODING;278return c2b[index + (ch & 0xff)];279}280281private byte repl = (byte)'?';282protected void implReplaceWith(byte[] newReplacement) {283repl = newReplacement[0];284}285286public int encode(char[] src, int sp, int len, byte[] dst) {287int dp = 0;288int sl = sp + Math.min(len, dst.length);289while (sp < sl) {290char c = src[sp++];291int b = encode(c);292if (b != UNMAPPABLE_ENCODING) {293dst[dp++] = (byte)b;294continue;295}296if (Character.isHighSurrogate(c) && sp < sl &&297Character.isLowSurrogate(src[sp])) {298if (len > dst.length) {299sl++;300len--;301}302sp++;303}304dst[dp++] = repl;305}306return dp;307}308309@Override310public int encodeFromLatin1(byte[] src, int sp, int len, byte[] dst) {311int dp = 0;312int sl = sp + Math.min(len, dst.length);313while (sp < sl) {314char c = (char)(src[sp++] & 0xff);315int b = encode(c);316if (b == UNMAPPABLE_ENCODING) {317dst[dp++] = repl;318} else {319dst[dp++] = (byte)b;320}321}322return dp;323}324325@Override326public int encodeFromUTF16(byte[] src, int sp, int len, byte[] dst) {327int dp = 0;328int sl = sp + Math.min(len, dst.length);329while (sp < sl) {330char c = StringUTF16.getChar(src, sp++);331int b = encode(c);332if (b != UNMAPPABLE_ENCODING) {333dst[dp++] = (byte)b;334continue;335}336if (Character.isHighSurrogate(c) && sp < sl &&337Character.isLowSurrogate(StringUTF16.getChar(src, sp))) {338if (len > dst.length) {339sl++;340len--;341}342sp++;343}344dst[dp++] = repl;345}346return dp;347}348349@Override350public boolean isASCIICompatible() {351return isASCIICompatible;352}353}354355// init the c2b and c2bIndex tables from b2c.356public static void initC2B(char[] b2c, char[] c2bNR,357char[] c2b, char[] c2bIndex) {358for (int i = 0; i < c2bIndex.length; i++)359c2bIndex[i] = UNMAPPABLE_ENCODING;360for (int i = 0; i < c2b.length; i++)361c2b[i] = UNMAPPABLE_ENCODING;362int off = 0;363for (int i = 0; i < b2c.length; i++) {364char c = b2c[i];365if (c == UNMAPPABLE_DECODING)366continue;367int index = (c >> 8);368if (c2bIndex[index] == UNMAPPABLE_ENCODING) {369c2bIndex[index] = (char)off;370off += 0x100;371}372index = c2bIndex[index] + (c & 0xff);373c2b[index] = (char)((i>=0x80)?(i-0x80):(i+0x80));374}375if (c2bNR != null) {376// c-->b nr entries377int i = 0;378while (i < c2bNR.length) {379char b = c2bNR[i++];380char c = c2bNR[i++];381int index = (c >> 8);382if (c2bIndex[index] == UNMAPPABLE_ENCODING) {383c2bIndex[index] = (char)off;384off += 0x100;385}386index = c2bIndex[index] + (c & 0xff);387c2b[index] = b;388}389}390}391}392393394