Coverage report

  %line %branch
com.ozacc.mail.fetch.impl.sk_jp.io.CharCodeConverter
0% 
0% 

 1  
 /*
 2  
  * Copyright (c) 2003 Shin Kinoshita All Rights Reserved.
 3  
  */
 4  
 package com.ozacc.mail.fetch.impl.sk_jp.io;
 5  
 
 6  
 import java.io.ByteArrayOutputStream;
 7  
 import java.io.UnsupportedEncodingException;
 8  
 
 9  
 /**
 10  
  * ʸ»ú´Ø·¸¤Î¥³¥ó¥Ð¡¼¥¿¤Ç¤¹¡£
 11  
  * °?Éô¥³¡¼¥É¤Î¥ª¥?¥¸¥Ê¥?¤Ï<a href="http://www-cms.phys.s.u-tokyo.ac.jp/~naoki/CIPINTRO/CCGI/kanjicod.html">Japanese Kanji Code</a>¤Ë¤Æ¸ø³«¤µ¤?¤Æ¤¤¤?¤â¤Î¤Ç¤¹¡£
 12  
  * ¤Þ¤¿¡¢http://www.sk-jp.com/cgi-bin/treebbs.cgi?kako=1&all=644&s=681
 13  
  * ¤Ë¤Æ YOSI ¤µ¤ó¤¬¸ø³«¤µ¤?¤¿¥³¡¼¥É¤â»²¹Í¤Ë¤·¤Æ¤¤¤Þ¤¹(¤È¤¤¤¦¤«¼Â¼ÁƱ¤¸¤Ç¤¹)¡£
 14  
  *
 15  
  * @author Shin
 16  
  * @version $Revision: 1.1.2.1 $ $Date: 2005/01/18 07:20:36 $
 17  
  */
 18  0
 public class CharCodeConverter {
 19  
     public static final byte[] SJIS_KANA;
 20  
     static {
 21  
         try {
 22  
             // Á´³Ñ¤Ø¤ÎÊÑ´¹¥Æ¡¼¥Ö¥?
 23  0
             SJIS_KANA = "¡£¡Ö¡×¡¢¡¦¥ò¥¡¥£¥¥¥§¥©¥ã¥å¥ç¥Ã¡¼¥¢¥¤¥¦¥¨¥ª¥«¥­¥¯¥±¥³¥µ¥·¥¹¥»¥½¥¿¥Á¥Ä¥Æ¥È¥Ê¥Ë¥Ì¥Í¥Î¥Ï¥Ò¥Õ¥Ø¥Û¥Þ¥ß¥à¥á¥â¥ä¥æ¥è¥é¥?¥?¥?¥úÁ?¥ó¡«¡¬".getBytes("Shift_JIS");
 24  0
         } catch (UnsupportedEncodingException e) {
 25  0
             throw new RuntimeException("CANT HAPPEN");
 26  
         }
 27  
     }
 28  
 
 29  
     /**
 30  
      * Shift_JIS ¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¥¹¥­¡¼¥à¤Ë´ð¤Å¤¯¥Ð¥¤¥ÈÎó¤?
 31  
      * ISO-2022-JP ¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¥¹¥­¡¼¥à¤ËÊÑ´¹¤·¤Þ¤¹¡£
 32  
      * ¡ÖȾ³Ñ¥«¥Ê¡×¤ÏÂб?¤¹¤?Á´³Ñʸ»ú¤ËÊÑ´¹¤·¤Þ¤¹¡£
 33  
      */
 34  
     public static byte[] sjisToJis(byte[] sjisBytes) {
 35  0
         ByteArrayOutputStream out = new ByteArrayOutputStream();
 36  0
         boolean nonAscii = false;
 37  0
         int len = sjisBytes.length;
 38  0
         for (int i = 0; i < len; i++ ) {
 39  0
             if (sjisBytes[i] >= 0) {
 40  0
                 if (nonAscii) {
 41  0
                     nonAscii = false;
 42  0
                     out.write(0x1b);
 43  0
                     out.write('(');
 44  0
                     out.write('B');
 45  
                 }
 46  0
                 out.write(sjisBytes[i]);
 47  
             } else {
 48  0
                 if (!nonAscii) {
 49  0
                     nonAscii = true;
 50  0
                     out.write(0x1b);
 51  0
                     out.write('$');
 52  0
                     out.write('B');
 53  
                 }
 54  0
                 int b = sjisBytes[i] & 0xff;
 55  0
                 if (b >= 0xa1 && b <= 0xdf) {
 56  
                     // Ⱦ³Ñ¥«¥Ê¤ÏÁ´³Ñ¤ËÊÑ´¹
 57  0
                     int kanaIndex = (b - 0xA1) * 2;
 58  0
                     sjisToJis(out, SJIS_KANA[kanaIndex], SJIS_KANA[kanaIndex + 1]);
 59  
                 } else {
 60  0
                     i++;
 61  0
                     if (i == len) break;
 62  0
                     sjisToJis(out, sjisBytes[i - 1], sjisBytes[i]);
 63  
                 }
 64  
             }
 65  
         }
 66  0
         if (nonAscii) {
 67  0
             out.write(0x1b);
 68  0
             out.write('(');
 69  0
             out.write('B');
 70  
         }
 71  0
         return out.toByteArray();
 72  
     }
 73  
     /**
 74  
      * £±Ê¸»ú¤Î£²¥Ð¥¤¥È Shift_JIS ¥³¡¼¥É¤? JIS ¥³¡¼¥É¤ËÊÑ´¹¤·¤Æ½ñ¤­½Ð¤·¤Þ¤¹¡£
 75  
      */
 76  
     private static void sjisToJis(
 77  
                 ByteArrayOutputStream out, byte bh, byte bl) {
 78  0
         int h = (bh << 1) & 0xFF;
 79  0
         int l = bl & 0xFF;
 80  0
         if (l < 0x9F) {
 81  0
             if (h < 0x3F) h += 0x1F; else h -= 0x61;
 82  0
             if (l > 0x7E) l -= 0x20; else l -= 0x1F;
 83  
         } else {
 84  0
             if (h < 0x3F) h += 0x20; else h -= 0x60;
 85  0
             l -= 0x7E;
 86  
         }
 87  0
         out.write(h);
 88  0
         out.write(l);
 89  0
     }
 90  
 
 91  
     /**
 92  
      * ÇÛÎó¤Ï¥?¥¤¥É¥­¥ã¥é¥¯¥¿¤Î¶­³¦¤Ë¤Ê¤¤¤³¤È¤òÁ°Äó¤È¤·¤Æ¤¤¤Þ¤¹¡£
 93  
      * ¤Þ¤¿¡¢¥¨¥¹¥±¡¼¥×¥·¡¼¥±¥ó¥¹¤¬Å¬Àڤ˴ޤޤ?¤?¤³¤È¤âÁ°Äó¤Ç¤¹¡£
 94  
      * ¥¨¥¹¥±¡¼¥×¥·¡¼¥±¥ó¥¹¤¬"(B"/"$B"°Ê³°¤Î¾?¹ç¤Ï̵»?¤·¤Þ¤¹¡£
 95  
      */
 96  
     public byte[] jisTosjis(byte[] jisBytes) {
 97  0
         ByteArrayOutputStream out = new ByteArrayOutputStream();
 98  0
         boolean nonAscii = false;
 99  0
         boolean kana = false;
 100  0
         int len = jisBytes.length;
 101  0
         for (int i = 0; i < len; i++) {
 102  0
             if (jisBytes[i] == 0x1b) {
 103  0
                 if (i + 2 >= len) break;
 104  0
                 if (jisBytes[i + 1] == '$' && jisBytes[i + 2] == 'B') {
 105  0
                     nonAscii = true;
 106  0
                     i += 2;
 107  0
                 } else if (jisBytes[i + 1] == '(' && jisBytes[i + 2] == 'I') {
 108  0
                     kana = true;
 109  0
                     i += 2;
 110  0
                 } else if (jisBytes[i + 1] == '(' && jisBytes[i + 2] == 'B') {
 111  0
                     nonAscii = false;
 112  0
                     kana = false;
 113  0
                     i += 2;
 114  
                 } else {
 115  
                     // illegal sequence ¤ÏÅöÌÌ̵»?
 116  0
                     nonAscii = false;
 117  0
                     kana = false;
 118  
                 }
 119  0
                 continue;
 120  
             }
 121  0
             if (jisBytes[i] == 0x0e) { // SO
 122  0
                 kana = true;
 123  0
                 continue;
 124  
             }
 125  0
             if (jisBytes[i] == 0x0f) { // SI
 126  0
                 kana = false;
 127  0
                 continue;
 128  
             }
 129  0
             if (kana) {
 130  0
                 out.write(jisBytes[i] | 0x80);
 131  0
             } else if (nonAscii) {
 132  0
                 i++;
 133  0
                 if (i == jisBytes.length) break;
 134  0
                 jisToSjis(out, jisBytes[i - 1], jisBytes[i]);
 135  
             } else {
 136  0
                 out.write(jisBytes[i]);
 137  
             }
 138  
         }
 139  0
         return out.toByteArray();
 140  
     }
 141  
     /**
 142  
      * £±Ê¸»ú¤Î£²¥Ð¥¤¥È JIS ¥³¡¼¥É¤? Shift_JIS ¤ËÊÑ´¹¤·¤Æ½ñ¤­½Ð¤·¤Þ¤¹¡£
 143  
      */
 144  
     private static void jisToSjis(
 145  
                 ByteArrayOutputStream out, byte bh, byte bl) {
 146  0
         int h = bh & 0xFF;
 147  0
         int l = bl & 0xFF;
 148  0
         if ((h & 0x01) > 0) {
 149  0
             h >>= 1;
 150  0
             if (h < 0x2F) h += 0x71; else h -= 0x4F;
 151  0
             if (l > 0x5F) l += 0x20; else l += 0x1F;
 152  
         } else {
 153  0
             h >>= 1;
 154  0
             if (h < 0x2F) h += 0x70; else h -= 0x50;
 155  0
             l += 0x7E;
 156  
         }
 157  0
         out.write(h);
 158  0
         out.write(l);
 159  0
     }
 160  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.