-/*\r
- * Copyright (c) 2003 Shin Kinoshita All Rights Reserved.\r
- */\r
-package com.ozacc.mail.fetch.impl.sk_jp.io;\r
-\r
-import java.io.ByteArrayOutputStream;\r
-import java.io.UnsupportedEncodingException;\r
-\r
-/**\r
- * 文字関係のコンバータです。\r
- * 一部コードのオリジナルは<a href="http://www-cms.phys.s.u-tokyo.ac.jp/~naoki/CIPINTRO/CCGI/kanjicod.html">Japanese Kanji Code</a>にて公開されているものです。\r
- * また、http://www.sk-jp.com/cgi-bin/treebbs.cgi?kako=1&all=644&s=681\r
- * にて YOSI さんが公開されたコードも参考にしています(というか実質同じです)。\r
- *\r
- * @author Shin\r
- * @version $Revision: 1.1.2.1 $ $Date: 2005/01/18 07:20:36 $\r
- */\r
-public class CharCodeConverter {\r
- public static final byte[] SJIS_KANA;\r
- static {\r
- try {\r
- // 全角への変換テーブル\r
- SJIS_KANA = "。「」、・ヲァィゥェォャュョッーアイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワン゛゜".getBytes("Shift_JIS");\r
- } catch (UnsupportedEncodingException e) {\r
- throw new RuntimeException("CANT HAPPEN");\r
- }\r
- }\r
-\r
- /**\r
- * Shift_JIS エンコーディングスキームに基づくバイト列を\r
- * ISO-2022-JP エンコーディングスキームに変換します。\r
- * 「半角カナ」は対応する全角文字に変換します。\r
- */\r
- public static byte[] sjisToJis(byte[] sjisBytes) {\r
- ByteArrayOutputStream out = new ByteArrayOutputStream();\r
- boolean nonAscii = false;\r
- int len = sjisBytes.length;\r
- for (int i = 0; i < len; i++ ) {\r
- if (sjisBytes[i] >= 0) {\r
- if (nonAscii) {\r
- nonAscii = false;\r
- out.write(0x1b);\r
- out.write('(');\r
- out.write('B');\r
- }\r
- out.write(sjisBytes[i]);\r
- } else {\r
- if (!nonAscii) {\r
- nonAscii = true;\r
- out.write(0x1b);\r
- out.write('$');\r
- out.write('B');\r
- }\r
- int b = sjisBytes[i] & 0xff;\r
- if (b >= 0xa1 && b <= 0xdf) {\r
- // 半角カナは全角に変換\r
- int kanaIndex = (b - 0xA1) * 2;\r
- sjisToJis(out, SJIS_KANA[kanaIndex], SJIS_KANA[kanaIndex + 1]);\r
- } else {\r
- i++;\r
- if (i == len) break;\r
- sjisToJis(out, sjisBytes[i - 1], sjisBytes[i]);\r
- }\r
- }\r
- }\r
- if (nonAscii) {\r
- out.write(0x1b);\r
- out.write('(');\r
- out.write('B');\r
- }\r
- return out.toByteArray();\r
- }\r
- /**\r
- * 1文字の2バイト Shift_JIS コードを JIS コードに変換して書き出します。\r
- */\r
- private static void sjisToJis(\r
- ByteArrayOutputStream out, byte bh, byte bl) {\r
- int h = (bh << 1) & 0xFF;\r
- int l = bl & 0xFF;\r
- if (l < 0x9F) {\r
- if (h < 0x3F) h += 0x1F; else h -= 0x61;\r
- if (l > 0x7E) l -= 0x20; else l -= 0x1F;\r
- } else {\r
- if (h < 0x3F) h += 0x20; else h -= 0x60;\r
- l -= 0x7E;\r
- }\r
- out.write(h);\r
- out.write(l);\r
- }\r
-\r
- /**\r
- * 配列はワイドキャラクタの境界にないことを前提としています。\r
- * また、エスケープシーケンスが適切に含まれることも前提です。\r
- * エスケープシーケンスが"(B"/"$B"以外の場合は無視します。\r
- */\r
- public byte[] jisTosjis(byte[] jisBytes) {\r
- ByteArrayOutputStream out = new ByteArrayOutputStream();\r
- boolean nonAscii = false;\r
- boolean kana = false;\r
- int len = jisBytes.length;\r
- for (int i = 0; i < len; i++) {\r
- if (jisBytes[i] == 0x1b) {\r
- if (i + 2 >= len) break;\r
- if (jisBytes[i + 1] == '$' && jisBytes[i + 2] == 'B') {\r
- nonAscii = true;\r
- i += 2;\r
- } else if (jisBytes[i + 1] == '(' && jisBytes[i + 2] == 'I') {\r
- kana = true;\r
- i += 2;\r
- } else if (jisBytes[i + 1] == '(' && jisBytes[i + 2] == 'B') {\r
- nonAscii = false;\r
- kana = false;\r
- i += 2;\r
- } else {\r
- // illegal sequence は当面無視\r
- nonAscii = false;\r
- kana = false;\r
- }\r
- continue;\r
- }\r
- if (jisBytes[i] == 0x0e) { // SO\r
- kana = true;\r
- continue;\r
- }\r
- if (jisBytes[i] == 0x0f) { // SI\r
- kana = false;\r
- continue;\r
- }\r
- if (kana) {\r
- out.write(jisBytes[i] | 0x80);\r
- } else if (nonAscii) {\r
- i++;\r
- if (i == jisBytes.length) break;\r
- jisToSjis(out, jisBytes[i - 1], jisBytes[i]);\r
- } else {\r
- out.write(jisBytes[i]);\r
- }\r
- }\r
- return out.toByteArray();\r
- }\r
- /**\r
- * 1文字の2バイト JIS コードを Shift_JIS に変換して書き出します。\r
- */\r
- private static void jisToSjis(\r
- ByteArrayOutputStream out, byte bh, byte bl) {\r
- int h = bh & 0xFF;\r
- int l = bl & 0xFF;\r
- if ((h & 0x01) > 0) {\r
- h >>= 1;\r
- if (h < 0x2F) h += 0x71; else h -= 0x4F;\r
- if (l > 0x5F) l += 0x20; else l += 0x1F;\r
- } else {\r
- h >>= 1;\r
- if (h < 0x2F) h += 0x70; else h -= 0x50;\r
- l += 0x7E;\r
- }\r
- out.write(h);\r
- out.write(l);\r
- }\r
-}\r
+/*
+ * Copyright (c) 2003 Shin Kinoshita All Rights Reserved.
+ */
+package com.ozacc.mail.fetch.impl.sk_jp.io;
+
+import java.io.ByteArrayOutputStream;
+import java.io.UnsupportedEncodingException;
+
+/**
+ * 文字関係のコンバータです。
+ * 一部コードのオリジナルは<a href="http://www-cms.phys.s.u-tokyo.ac.jp/~naoki/CIPINTRO/CCGI/kanjicod.html">Japanese Kanji Code</a>にて公開されているものです。
+ * また、http://www.sk-jp.com/cgi-bin/treebbs.cgi?kako=1&all=644&s=681
+ * にて YOSI さんが公開されたコードも参考にしています(というか実質同じです)。
+ *
+ * @author Shin
+ * @version $Revision: 1.1.2.1 $ $Date: 2005/01/18 07:20:36 $
+ */
+public class CharCodeConverter {
+ public static final byte[] SJIS_KANA;
+ static {
+ try {
+ // 全角への変換テーブル
+ SJIS_KANA = "。「」、・ヲァィゥェォャュョッーアイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワン゛゜".getBytes("Shift_JIS");
+ } catch (UnsupportedEncodingException e) {
+ throw new RuntimeException("CANT HAPPEN");
+ }
+ }
+
+ /**
+ * Shift_JIS エンコーディングスキームに基づくバイト列を
+ * ISO-2022-JP エンコーディングスキームに変換します。
+ * 「半角カナ」は対応する全角文字に変換します。
+ */
+ public static byte[] sjisToJis(byte[] sjisBytes) {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ boolean nonAscii = false;
+ int len = sjisBytes.length;
+ for (int i = 0; i < len; i++ ) {
+ if (sjisBytes[i] >= 0) {
+ if (nonAscii) {
+ nonAscii = false;
+ out.write(0x1b);
+ out.write('(');
+ out.write('B');
+ }
+ out.write(sjisBytes[i]);
+ } else {
+ if (!nonAscii) {
+ nonAscii = true;
+ out.write(0x1b);
+ out.write('$');
+ out.write('B');
+ }
+ int b = sjisBytes[i] & 0xff;
+ if (b >= 0xa1 && b <= 0xdf) {
+ // 半角カナは全角に変換
+ int kanaIndex = (b - 0xA1) * 2;
+ sjisToJis(out, SJIS_KANA[kanaIndex], SJIS_KANA[kanaIndex + 1]);
+ } else {
+ i++;
+ if (i == len) break;
+ sjisToJis(out, sjisBytes[i - 1], sjisBytes[i]);
+ }
+ }
+ }
+ if (nonAscii) {
+ out.write(0x1b);
+ out.write('(');
+ out.write('B');
+ }
+ return out.toByteArray();
+ }
+ /**
+ * 1文字の2バイト Shift_JIS コードを JIS コードに変換して書き出します。
+ */
+ private static void sjisToJis(
+ ByteArrayOutputStream out, byte bh, byte bl) {
+ int h = (bh << 1) & 0xFF;
+ int l = bl & 0xFF;
+ if (l < 0x9F) {
+ if (h < 0x3F) h += 0x1F; else h -= 0x61;
+ if (l > 0x7E) l -= 0x20; else l -= 0x1F;
+ } else {
+ if (h < 0x3F) h += 0x20; else h -= 0x60;
+ l -= 0x7E;
+ }
+ out.write(h);
+ out.write(l);
+ }
+
+ /**
+ * 配列はワイドキャラクタの境界にないことを前提としています。
+ * また、エスケープシーケンスが適切に含まれることも前提です。
+ * エスケープシーケンスが"(B"/"$B"以外の場合は無視します。
+ */
+ public byte[] jisTosjis(byte[] jisBytes) {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ boolean nonAscii = false;
+ boolean kana = false;
+ int len = jisBytes.length;
+ for (int i = 0; i < len; i++) {
+ if (jisBytes[i] == 0x1b) {
+ if (i + 2 >= len) break;
+ if (jisBytes[i + 1] == '$' && jisBytes[i + 2] == 'B') {
+ nonAscii = true;
+ i += 2;
+ } else if (jisBytes[i + 1] == '(' && jisBytes[i + 2] == 'I') {
+ kana = true;
+ i += 2;
+ } else if (jisBytes[i + 1] == '(' && jisBytes[i + 2] == 'B') {
+ nonAscii = false;
+ kana = false;
+ i += 2;
+ } else {
+ // illegal sequence は当面無視
+ nonAscii = false;
+ kana = false;
+ }
+ continue;
+ }
+ if (jisBytes[i] == 0x0e) { // SO
+ kana = true;
+ continue;
+ }
+ if (jisBytes[i] == 0x0f) { // SI
+ kana = false;
+ continue;
+ }
+ if (kana) {
+ out.write(jisBytes[i] | 0x80);
+ } else if (nonAscii) {
+ i++;
+ if (i == jisBytes.length) break;
+ jisToSjis(out, jisBytes[i - 1], jisBytes[i]);
+ } else {
+ out.write(jisBytes[i]);
+ }
+ }
+ return out.toByteArray();
+ }
+ /**
+ * 1文字の2バイト JIS コードを Shift_JIS に変換して書き出します。
+ */
+ private static void jisToSjis(
+ ByteArrayOutputStream out, byte bh, byte bl) {
+ int h = bh & 0xFF;
+ int l = bl & 0xFF;
+ if ((h & 0x01) > 0) {
+ h >>= 1;
+ if (h < 0x2F) h += 0x71; else h -= 0x4F;
+ if (l > 0x5F) l += 0x20; else l += 0x1F;
+ } else {
+ h >>= 1;
+ if (h < 0x2F) h += 0x70; else h -= 0x50;
+ l += 0x7E;
+ }
+ out.write(h);
+ out.write(l);
+ }
+}