Base64OutputStream

Base64OutputStream

Base64 の Stream エンコード実装例です。

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Base64OutputStream extends FilterOutputStream {

  private int col = 0;
  private int i = 0;
  private int[] buf = new int[3];

  public Base64OutputStream(OutputStream out) {
    super(out);
  }

  public void write(int c) throws IOException {
    buf[i++] = c;
    if (i == 3) {
      super.write(b64Tbl[ (buf[0] & 0xFC) >> 2]);
      super.write(b64Tbl[((buf[0] & 0x03) << 4) | ((buf[1] & 0xF0) >> 4)]);
      super.write(b64Tbl[((buf[1] & 0x0F) << 2) | ((buf[2] & 0xC0) >> 6)]);
      super.write(b64Tbl[  buf[2] & 0x3F]);
      col += 4;
      i = 0;
      if (col >= 76) {
        super.write('\n');
        col = 0;
      }
    }
  }

  public void flush() throws IOException {
    if (i == 1) {
      super.write(b64Tbl[(buf[0] & 0xFC) >> 2]);
      super.write(b64Tbl[(buf[0] & 0x03) << 4]);
      super.write('=');
      super.write('=');

    } else if (i == 2) {
      super.write(b64Tbl[ (buf[0] & 0xFC) >> 2]);
      super.write(b64Tbl[((buf[0] & 0x03) << 4) | ((buf[1] & 0xF0) >> 4)]);
      super.write(b64Tbl[ (buf[1] & 0x0F) << 2]);
      super.write('=');
    }
  }

  private static char[] b64Tbl = {
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
    'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
    'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
    'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
    'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
    'w', 'x', 'y', 'z', '0', '1', '2', '3',
    '4', '5', '6', '7', '8', '9', '+', '/'
    };
}

ちなみにBase64エンコード手順は

元データの先頭から3バイト (24bit) ずつ取り出す。取り出した3バイトを6bitずつ4つに分割し、6bitの値にA-Z,a-z,0-9,+,/を割り当てる。最後にエンコードしたデータが3バイトに満たなかった場合、その分だけ = 記号を最後に付加する。一行に許される最大文字数は 76 文字で、それ以上になる場合には改行する。
ASCII文字の Base の変換は以下の手順で QmFzZQ== となる。

              B        a        s           e
16進数        42       61       73          65
 2進数        01000010 01100001 01110011    01100101

6bit分割      010000 100110 000101 110011   011001 010000
対応文字変換  Q      m      F      z        Z      Q      = =