1 /* 2 * Copyright (c) 2000, 2006, 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 30 import java.nio.ByteBuffer; 31 import java.nio.CharBuffer; 32 import java.nio.charset.Charset; 33 import java.nio.charset.CharsetDecoder; 34 import java.nio.charset.CoderResult; 35 import java.nio.charset.CharacterCodingException; 36 import java.nio.charset.MalformedInputException; 37 import java.nio.charset.UnmappableCharacterException; 38 39 40 public abstract class SingleByteDecoder 41 extends CharsetDecoder 42 { 43 44 private final String byteToCharTable; 45 46 protected SingleByteDecoder(Charset cs, String byteToCharTable) { 47 super(cs, 1.0f, 1.0f); 48 this.byteToCharTable = byteToCharTable; 49 } 50 51 private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) { 52 byte[] sa = src.array(); 53 int sp = src.arrayOffset() + src.position(); 54 int sl = src.arrayOffset() + src.limit(); 55 assert (sp <= sl); 56 sp = (sp <= sl ? sp : sl); 57 char[] da = dst.array(); 58 int dp = dst.arrayOffset() + dst.position(); 59 int dl = dst.arrayOffset() + dst.limit(); 60 assert (dp <= dl); 61 dp = (dp <= dl ? dp : dl); 62 63 try { 64 while (sp < sl) { 65 int b = sa[sp]; 66 67 char c = decode(b); 68 if (c == '\uFFFD') 69 return CoderResult.unmappableForLength(1); 70 if (dl - dp < 1) 71 return CoderResult.OVERFLOW; 72 da[dp++] = c; 73 sp++; 74 } 75 return CoderResult.UNDERFLOW; 76 } finally { 77 src.position(sp - src.arrayOffset()); 78 dst.position(dp - dst.arrayOffset()); 79 } 80 } 81 82 private CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) { 83 int mark = src.position(); 84 try { 85 while (src.hasRemaining()) { 86 int b = src.get(); 87 88 char c = decode(b); 89 if (c == '\uFFFD') 90 return CoderResult.unmappableForLength(1); 91 if (!dst.hasRemaining()) 92 return CoderResult.OVERFLOW; 93 mark++; 94 dst.put(c); 95 } 96 return CoderResult.UNDERFLOW; 97 } finally { 98 src.position(mark); 99 } 100 } 101 102 protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) { 103 if (true && src.hasArray() && dst.hasArray()) 104 return decodeArrayLoop(src, dst); 105 else 106 return decodeBufferLoop(src, dst); 107 } 108 109 public char decode(int byteIndex) { 110 int n = byteIndex + 128; 111 if (n >= byteToCharTable.length() || n < 0) 112 return '\uFFFD'; 113 return byteToCharTable.charAt(n); 114 } 115 }