EUCString.java
import java.io.ByteToCharConverter; // 言語環境に対応した新しいクラス
import java.io.UnsupportedEncodingException;
import java.io.MalformedInputException;
/**
* 日本語EUCコードの文字列をソース内に記述できるようにしたクラス
* EUCString の定義(試作版です。)
*/
public class EUCString {
/** Unicode に変換された文字列 */
protected String string;
/** コンストラクタ。与えられたEUC文字列をUnicodeに変換して記憶 */
EUCString( String euc ) throws Exception {
ByteToCharConverter b2c; // バイト列を unicode に変換する
byte buf[]; // バイト列になった EUCコードを記憶
char cha1[]; // EUC で記述された文字配列
char cha2[]; // Unicode で記述された文字配列
try {
b2c = ByteToCharConverter.getConverter( "EUCJIS" );
}
catch( UnsupportedEncodingException e ){
throw (Exception)e;
}
cha1 = new char[ euc.length() ];
euc.getChars( 0, euc.length(), cha1, 0 );
buf = new byte[ euc.length() ];
for( int i=0; i<euc.length(); i++ ) {
buf[i] = (byte)cha1[i];
}
cha2 = new char[ euc.length() ];
try {
cha2 = b2c.convertAll( buf );
}
catch( MalformedInputException e ){
throw (Exception)e;
}
this.string = new String( cha2 ); // Unicode に変換された String
}
/** EUCの文字列を Unicodeに変換する static なメソッド */
public static String e2u( String euc ) throws Exception {
ByteToCharConverter b2c; // バイト列を unicode に変換する
byte buf[]; // バイト列になった EUCコードを記憶
char cha1[]; // EUC で記述された文字配列
char cha2[]; // Unicode で記述された文字配列
try {
b2c = ByteToCharConverter.getConverter( "EUCJIS" );
}
catch( UnsupportedEncodingException e ){
throw (Exception)e;
}
cha1 = new char[ euc.length() ];
euc.getChars( 0, euc.length(), cha1, 0 );
buf = new byte[ euc.length() ];
for( int i=0; i<euc.length(); i++ ) {
buf[i] = (byte)cha1[i];
}
cha2 = new char[ euc.length() ];
try {
cha2 = b2c.convertAll( buf );
}
catch( MalformedInputException e ){
throw (Exception)e;
}
return new String( cha2 ); // Unicode に変換された String
}
/** 記憶した Unicode文字列を返すメソッド */
public String toString() {
return this.string;
}
}