1 /* 2 * Copyright (c) 2000, 2004, 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 /* 27 */ 28 29 package sun.nio.cs; 30 31 import java.nio.ByteBuffer; 32 import java.nio.CharBuffer; 33 import java.nio.charset.Charset; 34 import java.nio.charset.CharsetEncoder; 35 import java.nio.charset.CoderResult; 36 import java.nio.charset.CharacterCodingException; 37 import java.nio.charset.MalformedInputException; 38 import java.nio.charset.UnmappableCharacterException; 39 import sun.nio.cs.Surrogate; 40 41 42 public abstract class SingleByteEncoder 43 extends CharsetEncoder 44 { 45 46 private final short index1[]; 47 private final String index2; 48 private final int mask1; 49 private final int mask2; 50 private final int shift; 51 52 private final Surrogate.Parser sgp = new Surrogate.Parser(); 53 54 protected SingleByteEncoder(Charset cs, 55 short[] index1, String index2, 56 int mask1, int mask2, int shift) 57 { 58 super(cs, 1.0f, 1.0f); 59 this.index1 = index1; 60 this.index2 = index2; 61 this.mask1 = mask1; 62 this.mask2 = mask2; 63 this.shift = shift; 64 } 65 66 public boolean canEncode(char c) { 67 char testEncode = index2.charAt(index1[(c & mask1) >> shift] 68 + (c & mask2)); 69 return testEncode != '\u0000' || c == '\u0000'; 70 } 71 72 private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) { 73 char[] sa = src.array(); 74 int sp = src.arrayOffset() + src.position(); 75 int sl = src.arrayOffset() + src.limit(); 76 assert (sp <= sl); 77 sp = (sp <= sl ? sp : sl); 78 byte[] da = dst.array(); 79 int dp = dst.arrayOffset() + dst.position(); 80 int dl = dst.arrayOffset() + dst.limit(); 81 assert (dp <= dl); 82 dp = (dp <= dl ? dp : dl); 83 84 try { 85 while (sp < sl) { 86 char c = sa[sp]; 87 if (Character.isSurrogate(c)) { 88 if (sgp.parse(c, sa, sp, sl) < 0) 89 return sgp.error(); 90 return sgp.unmappableResult(); 91 } 92 if (c >= '\uFFFE') 93 return CoderResult.unmappableForLength(1); 94 if (dl - dp < 1) 95 return CoderResult.OVERFLOW; 96 97 char e = index2.charAt(index1[(c & mask1) >> shift] 98 + (c & mask2)); 99 100 // If output byte is zero because input char is zero 101 // then character is mappable, o.w. fail 102 if (e == '\u0000' && c != '\u0000') 103 return CoderResult.unmappableForLength(1); 104 105 sp++; 106 da[dp++] = (byte)e; 107 } 108 return CoderResult.UNDERFLOW; 109 } finally { 110 src.position(sp - src.arrayOffset()); 111 dst.position(dp - dst.arrayOffset()); 112 } 113 } 114 115 private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) { 116 int mark = src.position(); 117 try { 118 while (src.hasRemaining()) { 119 char c = src.get(); 120 if (Character.isSurrogate(c)) { 121 if (sgp.parse(c, src) < 0) 122 return sgp.error(); 123 return sgp.unmappableResult(); 124 } 125 if (c >= '\uFFFE') 126 return CoderResult.unmappableForLength(1); 127 if (!dst.hasRemaining()) 128 return CoderResult.OVERFLOW; 129 130 char e = index2.charAt(index1[(c & mask1) >> shift] 131 + (c & mask2)); 132 133 // If output byte is zero because input char is zero 134 // then character is mappable, o.w. fail 135 if (e == '\u0000' && c != '\u0000') 136 return CoderResult.unmappableForLength(1); 137 138 mark++; 139 dst.put((byte)e); 140 } 141 return CoderResult.UNDERFLOW; 142 } finally { 143 src.position(mark); 144 } 145 } 146 147 protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) { 148 if (true && src.hasArray() && dst.hasArray()) 149 return encodeArrayLoop(src, dst); 150 else 151 return encodeBufferLoop(src, dst); 152 } 153 154 public byte encode(char inputChar) { 155 return (byte)index2.charAt(index1[(inputChar & mask1) >> shift] + 156 (inputChar & mask2)); 157 } 158 }