1 /* 2 * Copyright (c) 1996, 2012, 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.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 X11JIS0201 extends Charset { 34 35 private static Charset jis0201 = null; 36 private static CharsetEncoder enc = null; 37 38 static { 39 jis0201 = Charset.forName("JIS_X0201"); 40 if (jis0201 != null) { 41 enc = jis0201.newEncoder(); 42 enc.onMalformedInput(CodingErrorAction.REPORT); 43 enc.onUnmappableCharacter(CodingErrorAction.REPORT); 44 } 45 } 46 47 public X11JIS0201 () { 48 super("X11JIS0201", null); 49 } 50 51 public CharsetEncoder newEncoder() { 52 return new Encoder(this); 53 } 54 55 public CharsetDecoder newDecoder() { 56 return jis0201.newDecoder(); 57 } 58 59 public boolean contains(Charset cs) { 60 return cs instanceof X11JIS0201; 61 } 62 63 private class Encoder extends CharsetEncoder { 64 65 public Encoder(Charset cs) { 66 super(cs, 1.0f, 1.0f); 67 } 68 69 public boolean canEncode(char c){ 70 if ((c >= 0xff61 && c <= 0xff9f) 71 || c == 0x203e 72 || c == 0xa5) { 73 return true; 74 } 75 return false; 76 } 77 78 private SurrogateParser sgp; 79 protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) { 80 81 CoderResult cr = CoderResult.UNDERFLOW; 82 if (enc == null) { /* essentially bail */ 83 return cr; 84 } 85 char[] sa = src.array(); 86 int sp = src.arrayOffset() + src.position(); 87 int sl = src.arrayOffset() + src.limit(); 88 89 byte[] da = dst.array(); 90 int dp = dst.arrayOffset() + dst.position(); 91 int dl = dst.arrayOffset() + dst.limit(); 92 if ((dl - dp) < (sl - sp)) { 93 sl = sp + (dl - dp); 94 cr = CoderResult.OVERFLOW; 95 } 96 try { 97 char[] buf = new char[1]; 98 while (sp < sl) { 99 boolean mapped = false; 100 char c = sa[sp]; 101 ByteBuffer bb = null; 102 /* CharsetEncoders may not be thread safe */ 103 synchronized (enc) { 104 if (enc.canEncode(c)) { 105 buf[0] = c; 106 CharBuffer cb = CharBuffer.wrap(buf); 107 try { 108 bb = enc.encode(cb); 109 mapped = true; 110 } catch (CharacterCodingException cce) { 111 } 112 } 113 } 114 if (mapped && bb != null) { 115 da[dp++] = bb.get(); 116 sp++; 117 } else { 118 if (Character.isSurrogate(c)) { 119 if (sgp == null) 120 sgp = new SurrogateParser(); 121 if (sgp.parse(c, sa, sp, sl) >= 0) 122 return CoderResult.unmappableForLength(2); 123 } 124 return CoderResult.unmappableForLength(1); 125 } 126 } 127 return cr; 128 } finally { 129 src.position(sp - src.arrayOffset()); 130 dst.position(dp - dst.arrayOffset()); 131 } 132 } 133 } 134 } 135