1 /*
   2  * Copyright (c) 2003, 2010, 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.io;
  27 
  28 import sun.nio.cs.ext.JIS_X_0208_Solaris_Encoder;
  29 
  30 /**
  31  * @author Limin Shi
  32  * @author Mark Son-Bell
  33  * @author Ian Little
  34  *
  35  * PCK char->byte converter for Solaris platform containing additional
  36  * mappings for vendor defined chars (NEC row 13 & IBM extension chars)
  37  * (bugID 4765370)
  38  */
  39 public class CharToBytePCK extends CharToByteSJIS {
  40     CharToByteJIS0201 cbJIS0201 = new CharToByteJIS0201();
  41     short[] j0208Index1 = JIS_X_0208_Solaris_Encoder.getIndex1();
  42     String[] j0208Index2 = JIS_X_0208_Solaris_Encoder.getIndex2();
  43 
  44     public String getCharacterEncoding() {
  45         return "PCK";
  46     }
  47 
  48     protected int convSingleByte(char inputChar, byte[] outputByte) {
  49         byte b;
  50 
  51         // \u0000 - \u007F map straight through
  52         if ((inputChar & 0xFF80) == 0) {
  53             outputByte[0] = (byte)inputChar;
  54             return 1;
  55         }
  56 
  57         if ((b = cbJIS0201.getNative(inputChar)) == 0)
  58             return 0;
  59 
  60         outputByte[0] = b;
  61         return 1;
  62     }
  63 
  64     protected int getNative(char ch) {
  65         int result = 0;
  66 
  67          switch (ch) {
  68             case '\u2015':
  69                 return 0x815C;
  70             case '\u2014':
  71                 return 0;
  72             default:
  73                 break;
  74         }
  75 
  76         if ((result = super.getNative(ch)) != 0) {
  77             return result;
  78         } else {
  79             int offset = j0208Index1[ch >> 8] << 8;
  80             int pos = j0208Index2[offset >> 12].charAt((offset & 0xfff) + (ch & 0xff));
  81 
  82             if (pos != 0) {
  83                 /*
  84                  * This algorithm for converting from JIS to SJIS comes from Ken Lunde's
  85                  * "Understanding Japanese Information Processing", pg 163.
  86                  */
  87                 int c1 = (pos >> 8) & 0xff;
  88                 int c2 = pos & 0xff;
  89                 int rowOffset = c1 < 0x5F ? 0x70 : 0xB0;
  90                 int cellOffset = (c1 % 2 == 1) ? (c2 > 0x5F ? 0x20 : 0x1F) : 0x7E;
  91                 result =  ((((c1 + 1 ) >> 1) + rowOffset) << 8) | (c2 + cellOffset);
  92             }
  93         }
  94         return result;
  95     }
  96 }