付録I.3 文字コード変換UOCの実装例

文字コード変換UOCの実装例を次に示します。

public class OwnConvertUoc implements CSCOwnCodeConverter {

  // 日立コード変換オプション
  HJCOption option = null;
  // 日立コード変換結果
  HJCResult result = null;
  // エンコード
  String encode = null;

  // コンストラクタ
  public void OwnConvertUoc() {
     result = new HJCResult();
     option = new HJCOption();
  }

  // 独自定義ファイルをJavaのプロパティ形式で受け取る
  public void setProperties(final Properties properties)
                 throws CSCOwnCodeConverterException {

     if ( properties == null ) {
        // 独自定義ファイルが登録されていない
        // カスタム受付/カスタムアダプタ以外から実行されている
        // 可能性があるのでエラーにしない
        encode = "";
        return;
     }

     encode = properties.getProperty("encode");

     if ( encode == null ) {
        // キーがない場合
        String message = "文字コードが指定されていません。";
        throw new CSCOwnCodeConverterException(message);
     }
  }

  // 独自コードをUnicodeに変換する
  public char[] ownCodeToUnicode(final byte[] inBuffer)
                   throws CSCOwnCodeConverterException {

     if ( encode.equals("SJIS") ) {
        // SJIS(MS932)からUnicodeに変換
        // 日立コード変換を使用
        HJCString inStr = new HJCString( inBuffer );
        try {
           HJCConverters.cs_ms932tounicode(inStr, result, option);
        } catch ( Exception e ){
           throw new CSCOwnCodeConverterException(e);
        }
        return result.getStrResult().toString().toCharArray();
     }
     else if ( encode.equals("KEIS") ) {
        // KEISからUnicodeに変換
        // 日立コード変換を使用
        HJCString inStr = new HJCString( inBuffer );
        try {
           HJCConverters.cs_keistounicode(inStr, result, option);
        } catch ( Exception e ){
           throw new CSCOwnCodeConverterException(e);
        }
        return result.getStrResult().toString().toCharArray();
     }
     else {
        // 対象外の文字コード
        String message = "対象外の文字コードです。";
        throw new CSCOwnCodeConverterException(message);
     }
  }

  // Unicodeを独自コードに変換する
  public byte[] unicodeToOwnCode(final char[] inBuffer)
                 throws CSCOwnCodeConverterException {
     // (中略)
  }

  // 正常に変換できるバイト数を返す
  public int available(final byte[] inBuffer)
                 throws CSCOwnCodeConverterException {
     // (中略)
  }
}