1 /* 2 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.awt; 27 28 import java.nio.CharBuffer; 29 import java.nio.ByteBuffer; 30 import java.nio.charset.*; 31 32 33 //This class delegates all invokes to the charset "javaCs" if 34 //its subclasses do not provide their own en/decode solution. 35 36 public class AWTCharset extends Charset { 37 protected Charset awtCs; 38 protected Charset javaCs; 39 AWTCharset(String awtCsName, Charset javaCs)40 public AWTCharset(String awtCsName, Charset javaCs) { 41 super(awtCsName, null); 42 this.javaCs = javaCs; 43 this.awtCs = this; 44 } 45 contains(Charset cs)46 public boolean contains(Charset cs) { 47 if (javaCs == null) return false; 48 return javaCs.contains(cs); 49 } 50 newEncoder()51 public CharsetEncoder newEncoder() { 52 if (javaCs == null) 53 throw new Error("Encoder is not supported by this Charset"); 54 return new Encoder(javaCs.newEncoder()); 55 } 56 newDecoder()57 public CharsetDecoder newDecoder() { 58 if (javaCs == null) 59 throw new Error("Decoder is not supported by this Charset"); 60 return new Decoder(javaCs.newDecoder()); 61 } 62 63 public class Encoder extends CharsetEncoder { 64 protected CharsetEncoder enc; Encoder()65 protected Encoder () { 66 this(javaCs.newEncoder()); 67 } Encoder(CharsetEncoder enc)68 protected Encoder (CharsetEncoder enc) { 69 super(awtCs, 70 enc.averageBytesPerChar(), 71 enc.maxBytesPerChar()); 72 this.enc = enc; 73 } canEncode(char c)74 public boolean canEncode(char c) { 75 return enc.canEncode(c); 76 } canEncode(CharSequence cs)77 public boolean canEncode(CharSequence cs) { 78 return enc.canEncode(cs); 79 } encodeLoop(CharBuffer src, ByteBuffer dst)80 protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) { 81 return enc.encode(src, dst, true); 82 } implFlush(ByteBuffer out)83 protected CoderResult implFlush(ByteBuffer out) { 84 return enc.flush(out); 85 } implReset()86 protected void implReset() { 87 enc.reset(); 88 } implReplaceWith(byte[] newReplacement)89 protected void implReplaceWith(byte[] newReplacement) { 90 if (enc != null) 91 enc.replaceWith(newReplacement); 92 } implOnMalformedInput(CodingErrorAction newAction)93 protected void implOnMalformedInput(CodingErrorAction newAction) { 94 enc.onMalformedInput(newAction); 95 } implOnUnmappableCharacter(CodingErrorAction newAction)96 protected void implOnUnmappableCharacter(CodingErrorAction newAction) { 97 enc.onUnmappableCharacter(newAction); 98 } isLegalReplacement(byte[] repl)99 public boolean isLegalReplacement(byte[] repl) { 100 return true; 101 } 102 } 103 104 public class Decoder extends CharsetDecoder { 105 protected CharsetDecoder dec; 106 private String nr; 107 Decoder()108 protected Decoder () { 109 this(javaCs.newDecoder()); 110 } 111 Decoder(CharsetDecoder dec)112 protected Decoder (CharsetDecoder dec) { 113 super(awtCs, 114 dec.averageCharsPerByte(), 115 dec.maxCharsPerByte()); 116 this.dec = dec; 117 } decodeLoop(ByteBuffer src, CharBuffer dst)118 protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) { 119 return dec.decode(src, dst, true); 120 } 121 ByteBuffer fbb = ByteBuffer.allocate(0); implFlush(CharBuffer out)122 protected CoderResult implFlush(CharBuffer out) { 123 dec.decode(fbb, out, true); 124 return dec.flush(out); 125 } implReset()126 protected void implReset() { 127 dec.reset(); 128 } implReplaceWith(String newReplacement)129 protected void implReplaceWith(String newReplacement) { 130 if (dec != null) 131 dec.replaceWith(newReplacement); 132 } implOnMalformedInput(CodingErrorAction newAction)133 protected void implOnMalformedInput(CodingErrorAction newAction) { 134 dec.onMalformedInput(newAction); 135 } implOnUnmappableCharacter(CodingErrorAction newAction)136 protected void implOnUnmappableCharacter(CodingErrorAction newAction) { 137 dec.onUnmappableCharacter(newAction); 138 } 139 } 140 } 141