OSDN Git Service

rename package
[jindolf/JinParser.git] / src / main / java / jp / osdn / jindolf / parser / content / ShiftJis.java
1 /*
2  * Shift_JIS encoding utilities
3  *
4  * License : The MIT License
5  * Copyright(c) 2009 olyutorskii
6  */
7
8 package jp.osdn.jindolf.parser.content;
9
10 import java.nio.charset.Charset;
11
12 /**
13  * シフトJIS符号化ユーティリティ。
14  *
15  * <p>JIS X0208:1997 準拠。(Windows-31Jではない!)
16  *
17  * @see <a href="http://www.iana.org/assignments/character-sets">
18  * CHARACTER SETS</a>
19  * @see <a href="http://ja.wikipedia.org/wiki/Shift_JIS">
20  * Wikipedia: Shift_JIS</a>
21  */
22 public final class ShiftJis{
23
24     /** エンコード名。 */
25     public static final String ENCODE_NAME = "Shift_JIS";
26     /** SHift_JIS用Charsetインスタンス。 */
27     public static final Charset CHARSET = Charset.forName(ENCODE_NAME);
28     /** char1文字をエンコードした時の最大バイト数。 */
29     public static final int MAX_BYTES_PER_CHAR = 2;
30
31
32     /**
33      * 隠しコンストラクタ。
34      */
35     private ShiftJis(){
36         super();
37         return;
38     }
39
40
41     /**
42      * 任意のバイト値がシフトJISの1バイト目でありうるか否か判定する。
43      * 文字集合の判定は行わない。
44      *
45      * @param bval バイト値
46      * @return シフトJISの1バイト目でありうるならtrue
47      */
48     public static boolean isShiftJIS1stByte(byte bval){
49         int iVal = (int) bval & 0xff;
50         boolean result =
51                0x81 <= iVal && iVal <= 0x9f
52             || 0xe0 <= iVal && iVal <= 0xfc;
53         return result;
54     }
55
56     /**
57      * 任意のバイト値がシフトJISの2バイト目でありうるか否か判定する。
58      * 文字集合の判定は行わない。
59      *
60      * @param bval バイト値
61      * @return シフトJISの2バイト目でありうるならtrue
62      */
63     public static boolean isShiftJIS2ndByte(byte bval){
64         int iVal = (int) bval & 0xff;
65         boolean result =
66                0x40 <= iVal && iVal <= 0x7e
67             || 0x80 <= iVal && iVal <= 0xfc;
68         return result;
69     }
70
71     /**
72      * 任意のバイト値ペアがシフトJISでありうるか否か判定する。
73      * 文字集合の判定は行わない。
74      *
75      * @param b1st 第一バイト値
76      * @param b2nd 第二バイト値
77      * @return シフトJISならtrue
78      */
79     public static boolean isShiftJIS(byte b1st, byte b2nd){
80         boolean result =
81                ShiftJis.isShiftJIS1stByte(b1st)
82             && ShiftJis.isShiftJIS2ndByte(b2nd);
83         return result;
84     }
85
86 }