OSDN Git Service

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