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