// Rev.1 2000,09,24 import java.io.*; import java.util.*; /** * テキストファイルを読んで、JIS&MIMEエンコードしてファイルに書き出す。 * @author Hiroyuki Murata */ class MimeEnc { static final String base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static final String mimehead = "=?ISO-2022-JP?B?"; static final byte[] SI = {0x1b, (byte)'$', (byte)'B'}; static final byte[] SO = {0x1b, (byte)'(', (byte)'B'}; static final byte[] SO2 = {0x1b, (byte)'('}; /** * コンストラクタ(ファイル読み書き) */ MimeEnc(String infile, String outfile) { try { BufferedReader br = new BufferedReader(new FileReader(infile)); BufferedWriter bw = new BufferedWriter(new FileWriter(outfile)); String line; while ((line = br.readLine()) != null) { bw.write(mimeenc(line)); bw.newLine(); } bw.close(); br.close(); } catch (IOException e) { e.printStackTrace(); } } /** * JISバイト配列(ASCII含む)をMIMEエンコード */ String mimeenc(String line) { // MIMEエンコード StringBuffer sbuf = new StringBuffer(100); byte[] jis = null; try { jis = line.getBytes("JIS"); } catch (UnsupportedEncodingException e) { } // バグよけ(1)。 末尾のSOが欠落したら補う。 if (lastIndexOf(jis,SI) > lastIndexOf(jis,SO2)) { //debug("append SO"); byte[] jis2 = new byte[jis.length + 3]; int i = 0; for (; i= 6 && lastIndexOf(jis, SO2) == jis.length-3 && matchBytes(jis, jis.length-6, SO2) == SO2.length ) { //debug("delete SO"); byte[] jis2 = new byte[jis.length - 3]; for (int i=0; i>2 ); c4[1] = base64.charAt(((cs[i ]<<4) | (cs[i+1]>>4)) & 0x3f); c4[2] = base64.charAt(((cs[i+1]<<2) | (cs[i+2]>>6)) & 0x3f); c4[3] = base64.charAt( cs[i+2] & 0x3f); if (ilen <= (i+2)) { if (ilen < (i+2)) c4[2] = '='; c4[3] = '='; } sbuf.append(c4); } return sbuf.toString(); } /** * バイト配列を検索 *

* @param src 検索されるバイト配列 * @param start 検索開始箇所 * @param match 検索パターンのバイト配列 * @return 検出した位置。見つからない場合は -1。 */ int indexOf(byte[] src, int start, byte[] match) { int imax = src.length - match.length; for (int i=start; i<=imax; i++) { if (matchBytes(src, i, match) == match.length) return i; } return -1; } /** * バイト配列を末尾から検索 *

* @param src 検索されるバイト配列 * @param match 検索パターンのバイト配列 * @return 検出した位置。見つからない場合は -1。 */ int lastIndexOf(byte[] src, byte[] match) { int imax = src.length - match.length; for (int i=imax; i>=0; i--) { if (matchBytes(src, i, match) == match.length) return i; } return -1; } /** * バイト配列の指定箇所から何バイト一致するか *

* @param src 照合されるバイト配列 * @param start 照合開始箇所 * @param match 照合パターンのバイト配列 * @return 一致したバイト数 */ int matchBytes(byte[] src, int start, byte[] match) { int i = 0; int j = start; try { for (; i 0x20 && (c2 > 0x20 || c2 == 0x1b)) { sbuf.append(" "); } } catch (IndexOutOfBoundsException e) { } } //------------------------------------------------------ /** デバッグ用文字列表示 */ static void debug(String str) { System.out.println(str); } /** デバッグ用バイト配列表示 */ static void debug(byte[] word) { for (int i=0; i