import java.io.*;
import java.util.*;
import java.net.*;
import java.util.StringTokenizer;
public class Base64 {
private final char[] DIGIT =
{
'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','+','/'
};
public String encodeFromFile(String fname) {
final int BUFSIZ = 54;
FileInputStream fis = null;
FileOutputStream fos = null;
StringBuffer sbuf = new StringBuffer();
try {
fis = new FileInputStream(fname);
byte buf[] = new byte[BUFSIZ];
int len = 0;
while ((len = fis.read(buf)) > 0){
sbuf.append(encode(buf,len));
sbuf.append("\n");
}
fis.close();
}catch( IOException e ) {
System.out.println(e);
}finally{
return sbuf.toString();
}
}
public String encode (byte bbuf[], int num) {
byte[] buf = new byte[num];
for ( int i = 0; i < num; i++ )
buf[i] = bbuf[i];
return(encode(buf));
}
public String encode (byte[] bytes) {
StringBuffer buf = new StringBuffer();
int i = 0,pad,top = bytes.length - (bytes.length % 3);
long n;
while(i < top) {
n = ((((long)bytes[i++]) & 0xFF) << 16) | ((((long)bytes[i++]) & 0xFF)<< 8)
| (((long)bytes[i++]) & 0xFF);
buf.append(DIGIT[(int)((n >>> 18) & 0x3F)]);
buf.append(DIGIT[(int)((n >>> 12) & 0x3F)]);
buf.append(DIGIT[(int)((n >>> 6) & 0x3F)]);
buf.append(DIGIT[(int)(n & 0x3F)]);
}
if(i < bytes.length) {
n = (((long)bytes[i++]) & 0xFF) << 16;
pad = 2;
if(i < bytes.length) {
n |= (((long)bytes[i]) & 0xFF) << 8;
pad = 1;
}
int s = 18;
for(i = 0;i < 4 - pad;i++) {
buf.append(DIGIT[(int)((n >>> s) & 0x3F)]);
s -= 6;
}
for(i = 0;i < pad;i++) {
buf.append('=');
}
}
return buf.toString();
}
)
------------------------------------------
¼³¸í:
base64 encodingÀÔ´Ï´Ù.email encoding¹æ¹ý¿¡¼ °¡À帹ÀÌ ¾²ÀÌ´Â ¹æ¹ý
ÀÔ´Ï´Ù.
Chris Pratt°¡ ¸¸µç¼Ò½º¿¡¼ encodeFromFile(String fname)ºÎºÐ¸¸À»
Ãß°¡ Çß½À´Ï´Ù.
À§ÀÇ ¼Ò½º´Â
±Ã±ÝÇÑÁ¡ÀÖÀ¸¸é Q&A¿¡ ±ÛÀ»³²°ÜÁÖ¼¼¿ä
|