Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/nio/cs/US_ASCII.java
38918 views
/*1* Copyright (c) 2000, 2004, 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 java.nio.ByteBuffer;28import java.nio.CharBuffer;29import java.nio.charset.Charset;30import java.nio.charset.CharsetDecoder;31import java.nio.charset.CharsetEncoder;32import java.nio.charset.CoderResult;33import java.util.Arrays;3435public class US_ASCII36extends Charset37implements HistoricallyNamedCharset38{3940public US_ASCII() {41super("US-ASCII", StandardCharsets.aliases_US_ASCII);42}4344public String historicalName() {45return "ASCII";46}4748public boolean contains(Charset cs) {49return (cs instanceof US_ASCII);50}5152public CharsetDecoder newDecoder() {53return new Decoder(this);54}5556public CharsetEncoder newEncoder() {57return new Encoder(this);58}5960private static class Decoder extends CharsetDecoder61implements ArrayDecoder {6263private Decoder(Charset cs) {64super(cs, 1.0f, 1.0f);65}6667private CoderResult decodeArrayLoop(ByteBuffer src,68CharBuffer dst)69{70byte[] sa = src.array();71int sp = src.arrayOffset() + src.position();72int sl = src.arrayOffset() + src.limit();73assert (sp <= sl);74sp = (sp <= sl ? sp : sl);75char[] da = dst.array();76int dp = dst.arrayOffset() + dst.position();77int dl = dst.arrayOffset() + dst.limit();78assert (dp <= dl);79dp = (dp <= dl ? dp : dl);8081try {82while (sp < sl) {83byte b = sa[sp];84if (b >= 0) {85if (dp >= dl)86return CoderResult.OVERFLOW;87da[dp++] = (char)b;88sp++;89continue;90}91return CoderResult.malformedForLength(1);92}93return CoderResult.UNDERFLOW;94} finally {95src.position(sp - src.arrayOffset());96dst.position(dp - dst.arrayOffset());97}98}99100private CoderResult decodeBufferLoop(ByteBuffer src,101CharBuffer dst)102{103int mark = src.position();104try {105while (src.hasRemaining()) {106byte b = src.get();107if (b >= 0) {108if (!dst.hasRemaining())109return CoderResult.OVERFLOW;110dst.put((char)b);111mark++;112continue;113}114return CoderResult.malformedForLength(1);115}116return CoderResult.UNDERFLOW;117} finally {118src.position(mark);119}120}121122protected CoderResult decodeLoop(ByteBuffer src,123CharBuffer dst)124{125if (src.hasArray() && dst.hasArray())126return decodeArrayLoop(src, dst);127else128return decodeBufferLoop(src, dst);129}130131private char repl = '\uFFFD';132protected void implReplaceWith(String newReplacement) {133repl = newReplacement.charAt(0);134}135136public int decode(byte[] src, int sp, int len, char[] dst) {137int dp = 0;138len = Math.min(len, dst.length);139while (dp < len) {140byte b = src[sp++];141if (b >= 0)142dst[dp++] = (char)b;143else144dst[dp++] = repl;145}146return dp;147}148}149150private static class Encoder extends CharsetEncoder151implements ArrayEncoder {152153private Encoder(Charset cs) {154super(cs, 1.0f, 1.0f);155}156157public boolean canEncode(char c) {158return c < 0x80;159}160161public boolean isLegalReplacement(byte[] repl) {162return (repl.length == 1 && repl[0] >= 0) ||163super.isLegalReplacement(repl);164}165166private final Surrogate.Parser sgp = new Surrogate.Parser();167private CoderResult encodeArrayLoop(CharBuffer src,168ByteBuffer dst)169{170char[] sa = src.array();171int sp = src.arrayOffset() + src.position();172int sl = src.arrayOffset() + src.limit();173assert (sp <= sl);174sp = (sp <= sl ? sp : sl);175byte[] da = dst.array();176int dp = dst.arrayOffset() + dst.position();177int dl = dst.arrayOffset() + dst.limit();178assert (dp <= dl);179dp = (dp <= dl ? dp : dl);180181try {182while (sp < sl) {183char c = sa[sp];184if (c < 0x80) {185if (dp >= dl)186return CoderResult.OVERFLOW;187da[dp] = (byte)c;188sp++; dp++;189continue;190}191if (sgp.parse(c, sa, sp, sl) < 0)192return sgp.error();193return sgp.unmappableResult();194}195return CoderResult.UNDERFLOW;196} finally {197src.position(sp - src.arrayOffset());198dst.position(dp - dst.arrayOffset());199}200}201202private CoderResult encodeBufferLoop(CharBuffer src,203ByteBuffer dst)204{205int mark = src.position();206try {207while (src.hasRemaining()) {208char c = src.get();209if (c < 0x80) {210if (!dst.hasRemaining())211return CoderResult.OVERFLOW;212dst.put((byte)c);213mark++;214continue;215}216if (sgp.parse(c, src) < 0)217return sgp.error();218return sgp.unmappableResult();219}220return CoderResult.UNDERFLOW;221} finally {222src.position(mark);223}224}225226protected CoderResult encodeLoop(CharBuffer src,227ByteBuffer dst)228{229if (src.hasArray() && dst.hasArray())230return encodeArrayLoop(src, dst);231else232return encodeBufferLoop(src, dst);233}234235private byte repl = (byte)'?';236protected void implReplaceWith(byte[] newReplacement) {237repl = newReplacement[0];238}239240public int encode(char[] src, int sp, int len, byte[] dst) {241int dp = 0;242int sl = sp + Math.min(len, dst.length);243while (sp < sl) {244char c = src[sp++];245if (c < 0x80) {246dst[dp++] = (byte)c;247continue;248}249if (Character.isHighSurrogate(c) && sp < sl &&250Character.isLowSurrogate(src[sp])) {251if (len > dst.length) {252sl++;253len--;254}255sp++;256}257dst[dp++] = repl;258}259return dp;260}261}262263}264265266