Path: blob/master/src/java.desktop/share/classes/sun/awt/AWTCharset.java
41152 views
/*1* Copyright (c) 2005, 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.awt;2627import java.nio.CharBuffer;28import java.nio.ByteBuffer;29import java.nio.charset.*;303132//This class delegates all invokes to the charset "javaCs" if33//its subclasses do not provide their own en/decode solution.3435public class AWTCharset extends Charset {36protected Charset awtCs;37protected Charset javaCs;3839public AWTCharset(String awtCsName, Charset javaCs) {40super(awtCsName, null);41this.javaCs = javaCs;42this.awtCs = this;43}4445public boolean contains(Charset cs) {46if (javaCs == null) return false;47return javaCs.contains(cs);48}4950public CharsetEncoder newEncoder() {51if (javaCs == null)52throw new Error("Encoder is not supported by this Charset");53return new Encoder(javaCs.newEncoder());54}5556public CharsetDecoder newDecoder() {57if (javaCs == null)58throw new Error("Decoder is not supported by this Charset");59return new Decoder(javaCs.newDecoder());60}6162public class Encoder extends CharsetEncoder {63protected CharsetEncoder enc;64protected Encoder () {65this(javaCs.newEncoder());66}67protected Encoder (CharsetEncoder enc) {68super(awtCs,69enc.averageBytesPerChar(),70enc.maxBytesPerChar());71this.enc = enc;72}73public boolean canEncode(char c) {74return enc.canEncode(c);75}76public boolean canEncode(CharSequence cs) {77return enc.canEncode(cs);78}79protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {80return enc.encode(src, dst, true);81}82protected CoderResult implFlush(ByteBuffer out) {83return enc.flush(out);84}85protected void implReset() {86enc.reset();87}88protected void implReplaceWith(byte[] newReplacement) {89if (enc != null)90enc.replaceWith(newReplacement);91}92protected void implOnMalformedInput(CodingErrorAction newAction) {93enc.onMalformedInput(newAction);94}95protected void implOnUnmappableCharacter(CodingErrorAction newAction) {96enc.onUnmappableCharacter(newAction);97}98public boolean isLegalReplacement(byte[] repl) {99return true;100}101}102103public class Decoder extends CharsetDecoder {104protected CharsetDecoder dec;105private String nr;106107protected Decoder () {108this(javaCs.newDecoder());109}110111protected Decoder (CharsetDecoder dec) {112super(awtCs,113dec.averageCharsPerByte(),114dec.maxCharsPerByte());115this.dec = dec;116}117protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {118return dec.decode(src, dst, true);119}120ByteBuffer fbb = ByteBuffer.allocate(0);121protected CoderResult implFlush(CharBuffer out) {122dec.decode(fbb, out, true);123return dec.flush(out);124}125protected void implReset() {126dec.reset();127}128protected void implReplaceWith(String newReplacement) {129if (dec != null)130dec.replaceWith(newReplacement);131}132protected void implOnMalformedInput(CodingErrorAction newAction) {133dec.onMalformedInput(newAction);134}135protected void implOnUnmappableCharacter(CodingErrorAction newAction) {136dec.onUnmappableCharacter(newAction);137}138}139}140141142