001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt;
027:
028: import java.nio.CharBuffer;
029: import java.nio.ByteBuffer;
030: import java.nio.charset.*;
031:
032: //This class delegates all invokes to the charset "javaCs" if
033: //its subclasses do not provide their own en/decode solution.
034:
035: public class AWTCharset extends Charset {
036: protected Charset awtCs;
037: protected Charset javaCs;
038:
039: public AWTCharset(String awtCsName, Charset javaCs) {
040: super (awtCsName, null);
041: this .javaCs = javaCs;
042: this .awtCs = this ;
043: }
044:
045: public boolean contains(Charset cs) {
046: if (javaCs == null)
047: return false;
048: return javaCs.contains(cs);
049: }
050:
051: public CharsetEncoder newEncoder() {
052: if (javaCs == null)
053: throw new Error("Encoder is not supported by this Charset");
054: return new Encoder(javaCs.newEncoder());
055: }
056:
057: public CharsetDecoder newDecoder() {
058: if (javaCs == null)
059: throw new Error("Decoder is not supported by this Charset");
060: return new Decoder(javaCs.newDecoder());
061: }
062:
063: public class Encoder extends CharsetEncoder {
064: protected CharsetEncoder enc;
065:
066: protected Encoder() {
067: this (javaCs.newEncoder());
068: }
069:
070: protected Encoder(CharsetEncoder enc) {
071: super (awtCs, enc.averageBytesPerChar(), enc
072: .maxBytesPerChar());
073: this .enc = enc;
074: }
075:
076: public boolean canEncode(char c) {
077: return enc.canEncode(c);
078: }
079:
080: public boolean canEncode(CharSequence cs) {
081: return enc.canEncode(cs);
082: }
083:
084: protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
085: return enc.encode(src, dst, true);
086: }
087:
088: protected CoderResult implFlush(ByteBuffer out) {
089: return enc.flush(out);
090: }
091:
092: protected void implReset() {
093: enc.reset();
094: }
095:
096: protected void implReplaceWith(byte[] newReplacement) {
097: if (enc != null)
098: enc.replaceWith(newReplacement);
099: }
100:
101: protected void implOnMalformedInput(CodingErrorAction newAction) {
102: enc.onMalformedInput(newAction);
103: }
104:
105: protected void implOnUnmappableCharacter(
106: CodingErrorAction newAction) {
107: enc.onUnmappableCharacter(newAction);
108: }
109:
110: public boolean isLegalReplacement(byte[] repl) {
111: return true;
112: }
113: }
114:
115: public class Decoder extends CharsetDecoder {
116: protected CharsetDecoder dec;
117: private String nr;
118:
119: protected Decoder() {
120: this (javaCs.newDecoder());
121: }
122:
123: protected Decoder(CharsetDecoder dec) {
124: super (awtCs, dec.averageCharsPerByte(), dec
125: .maxCharsPerByte());
126: this .dec = dec;
127: }
128:
129: protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
130: return dec.decode(src, dst, true);
131: }
132:
133: ByteBuffer fbb = ByteBuffer.allocate(0);
134:
135: protected CoderResult implFlush(CharBuffer out) {
136: dec.decode(fbb, out, true);
137: return dec.flush(out);
138: }
139:
140: protected void implReset() {
141: dec.reset();
142: }
143:
144: protected void implReplaceWith(String newReplacement) {
145: if (dec != null)
146: dec.replaceWith(newReplacement);
147: }
148:
149: protected void implOnMalformedInput(CodingErrorAction newAction) {
150: dec.onMalformedInput(newAction);
151: }
152:
153: protected void implOnUnmappableCharacter(
154: CodingErrorAction newAction) {
155: dec.onUnmappableCharacter(newAction);
156: }
157: }
158: }
|