Path: blob/master/src/java.base/share/classes/sun/nio/cs/US_ASCII.java
41159 views
/*1* Copyright (c) 2000, 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.ByteBuffer;31import java.nio.CharBuffer;32import java.nio.charset.Charset;33import java.nio.charset.CharsetDecoder;34import java.nio.charset.CharsetEncoder;35import java.nio.charset.CoderResult;3637public class US_ASCII38extends Charset39implements HistoricallyNamedCharset40{41public static final US_ASCII INSTANCE = new US_ASCII();4243public US_ASCII() {44super("US-ASCII", StandardCharsets.aliases_US_ASCII());45}4647public String historicalName() {48return "ASCII";49}5051public boolean contains(Charset cs) {52return (cs instanceof US_ASCII);53}5455public CharsetDecoder newDecoder() {56return new Decoder(this);57}5859public CharsetEncoder newEncoder() {60return new Encoder(this);61}6263private static class Decoder extends CharsetDecoder {6465private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();6667private Decoder(Charset cs) {68super(cs, 1.0f, 1.0f);69}7071private CoderResult decodeArrayLoop(ByteBuffer src,72CharBuffer dst)73{74byte[] sa = src.array();75int soff = src.arrayOffset();76int sp = soff + src.position();77int sl = soff + src.limit();7879char[] da = dst.array();80int doff = dst.arrayOffset();81int dp = doff + dst.position();82int dl = doff + dst.limit();8384// ASCII only loop85int n = JLA.decodeASCII(sa, sp, da, dp, Math.min(sl - sp, dl - dp));86sp += n;87dp += n;88src.position(sp - soff);89dst.position(dp - doff);90if (sp < sl) {91if (dp >= dl) {92return CoderResult.OVERFLOW;93}94return CoderResult.malformedForLength(1);95}96return CoderResult.UNDERFLOW;97}9899private CoderResult decodeBufferLoop(ByteBuffer src,100CharBuffer dst)101{102int mark = src.position();103try {104while (src.hasRemaining()) {105byte b = src.get();106if (b >= 0) {107if (!dst.hasRemaining())108return CoderResult.OVERFLOW;109dst.put((char)b);110mark++;111continue;112}113return CoderResult.malformedForLength(1);114}115return CoderResult.UNDERFLOW;116} finally {117src.position(mark);118}119}120121protected CoderResult decodeLoop(ByteBuffer src,122CharBuffer dst)123{124if (src.hasArray() && dst.hasArray())125return decodeArrayLoop(src, dst);126else127return decodeBufferLoop(src, dst);128}129}130131private static class Encoder extends CharsetEncoder {132133private Encoder(Charset cs) {134super(cs, 1.0f, 1.0f);135}136137public boolean canEncode(char c) {138return c < 0x80;139}140141public boolean isLegalReplacement(byte[] repl) {142return (repl.length == 1 && repl[0] >= 0) ||143super.isLegalReplacement(repl);144}145146private final Surrogate.Parser sgp = new Surrogate.Parser();147private CoderResult encodeArrayLoop(CharBuffer src,148ByteBuffer dst)149{150char[] sa = src.array();151int sp = src.arrayOffset() + src.position();152int sl = src.arrayOffset() + src.limit();153assert (sp <= sl);154sp = (sp <= sl ? sp : sl);155byte[] da = dst.array();156int dp = dst.arrayOffset() + dst.position();157int dl = dst.arrayOffset() + dst.limit();158assert (dp <= dl);159dp = (dp <= dl ? dp : dl);160161try {162while (sp < sl) {163char c = sa[sp];164if (c < 0x80) {165if (dp >= dl)166return CoderResult.OVERFLOW;167da[dp] = (byte)c;168sp++; dp++;169continue;170}171if (sgp.parse(c, sa, sp, sl) < 0)172return sgp.error();173return sgp.unmappableResult();174}175return CoderResult.UNDERFLOW;176} finally {177src.position(sp - src.arrayOffset());178dst.position(dp - dst.arrayOffset());179}180}181182private CoderResult encodeBufferLoop(CharBuffer src,183ByteBuffer dst)184{185int mark = src.position();186try {187while (src.hasRemaining()) {188char c = src.get();189if (c < 0x80) {190if (!dst.hasRemaining())191return CoderResult.OVERFLOW;192dst.put((byte)c);193mark++;194continue;195}196if (sgp.parse(c, src) < 0)197return sgp.error();198return sgp.unmappableResult();199}200return CoderResult.UNDERFLOW;201} finally {202src.position(mark);203}204}205206protected CoderResult encodeLoop(CharBuffer src,207ByteBuffer dst)208{209if (src.hasArray() && dst.hasArray())210return encodeArrayLoop(src, dst);211else212return encodeBufferLoop(src, dst);213}214215}216}217218219