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.motif; 27 28 import java.nio.charset.*; 29 import sun.nio.cs.ext.*; 30 import static sun.nio.cs.CharsetMapping.*; 31 32 public class X11GBK extends Charset { 33 public X11GBK () { 34 super("X11GBK", null); 35 } 36 public CharsetEncoder newEncoder() { 37 return new Encoder(this); 38 } 39 public CharsetDecoder newDecoder() { 40 return new GBK().newDecoder(); 41 } 42 43 public boolean contains(Charset cs) { 44 return cs instanceof X11GBK; 45 } 46 47 private class Encoder extends DoubleByte.Encoder { 48 49 private DoubleByte.Encoder enc = (DoubleByte.Encoder)new GBK().newEncoder(); 50 51 Encoder(Charset cs) { 52 super(cs, (char[])null, (char[])null); 53 } 54 55 public boolean canEncode(char ch){ 56 if (ch < 0x80) return false; 57 return enc.canEncode(ch); 58 } 59 60 public int encodeChar(char ch) { 61 if (ch < 0x80) 62 return UNMAPPABLE_ENCODING; 63 return enc.encodeChar(ch); 64 } 65 } 66 } | 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.motif; 27 28 import java.nio.ByteBuffer; 29 import java.nio.CharBuffer; 30 import java.nio.charset.*; 31 import static sun.awt.motif.DoubleByte.*; 32 33 public class X11GBK extends Charset { 34 35 private static Charset gbk = Charset.forName("GBK"); 36 37 public X11GBK () { 38 super("X11GBK", null); 39 } 40 public CharsetEncoder newEncoder() { 41 return new Encoder(this); 42 } 43 public CharsetDecoder newDecoder() { 44 return gbk.newDecoder(); 45 } 46 47 public boolean contains(Charset cs) { 48 return cs instanceof X11GBK; 49 } 50 51 static class Encoder extends CharsetEncoder { 52 53 static final int MAX_SINGLEBYTE = 0xff; 54 static private CharsetEncoder enc = gbk.newEncoder(); 55 56 Encoder(Charset cs) { 57 super(cs, enc.averageBytesPerChar(), enc.maxBytesPerChar(), 58 enc.replacement()); 59 enc.onMalformedInput(CodingErrorAction.REPORT); 60 enc.onUnmappableCharacter(CodingErrorAction.REPORT); 61 } 62 63 public boolean canEncode(char ch){ 64 if (ch < 0x80) return false; 65 return enc.canEncode(ch); 66 } 67 68 public int encodeChar(char ch) { 69 if (ch < 0x80) { 70 return UNMAPPABLE_ENCODING; 71 } 72 char[] buf = new char[1]; 73 buf[0] = ch; 74 ByteBuffer bb = null; 75 CharBuffer cb = CharBuffer.wrap(buf); 76 boolean mapped = false; 77 try { 78 bb = enc.encode(cb); 79 mapped = true; 80 } catch (CharacterCodingException cce) { 81 } 82 if (mapped && bb != null) { 83 int ret = UNMAPPABLE_ENCODING; 84 if (bb.remaining() == 1) { 85 ret = bb.get() & 0xff; 86 } else if (bb.remaining() > 1) { 87 ret = ((bb.get() & 0xff) << 8) | (bb.get() & 0xff); 88 } 89 return ret; 90 } else { 91 return UNMAPPABLE_ENCODING; 92 } 93 } 94 95 private SurrogateParser sgp; 96 protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) { 97 int mark = src.position(); 98 try { 99 while (src.hasRemaining()) { 100 char c = src.get(); 101 int bb = encodeChar(c); 102 if (bb == UNMAPPABLE_ENCODING) { 103 if (Character.isSurrogate(c)) { 104 if (sgp == null) { 105 sgp = new SurrogateParser(); 106 } 107 if (sgp.parse(c, src) < 0) 108 return sgp.error(); 109 return sgp.unmappableResult(); 110 } 111 return CoderResult.unmappableForLength(1); 112 } 113 if (bb > MAX_SINGLEBYTE) { // DoubleByte 114 if (dst.remaining() < 2) 115 return CoderResult.OVERFLOW; 116 dst.put((byte)(bb >> 8)); 117 dst.put((byte)(bb)); 118 } else { 119 if (dst.remaining() < 1) 120 return CoderResult.OVERFLOW; 121 dst.put((byte)bb); 122 } 123 mark++; 124 } 125 return CoderResult.UNDERFLOW; 126 } finally { 127 src.position(mark); 128 } 129 130 } 131 } 132 } |