1 21 22 27 28 package com.sun.mail.util; 29 30 import java.io.*; 31 32 41 42 public class BASE64EncoderStream extends FilterOutputStream { 43 private byte[] buffer; private int bufsize = 0; private int count = 0; private int bytesPerLine; private int lineLimit; private boolean noCRLF = false; 49 50 private static byte[] newline = new byte[] { '\r', '\n' }; 51 52 61 public BASE64EncoderStream(OutputStream out, int bytesPerLine) { 62 super(out); 63 buffer = new byte[3]; 64 if (bytesPerLine == Integer.MAX_VALUE || bytesPerLine < 4) { 65 noCRLF = true; 66 bytesPerLine = 76; 67 } 68 this.bytesPerLine = (bytesPerLine / 4) * 4; lineLimit = bytesPerLine / 4 * 3; 70 } 71 72 77 public BASE64EncoderStream(OutputStream out) { 78 this(out, 76); 79 } 80 81 91 public void write(byte[] b, int off, int len) throws IOException { 92 int inx; 93 94 while ((bufsize != 0 || count != 0) && len > 0) { 96 write(b[off++]); 97 len--; 98 } 99 100 byte[] outbuf; 103 if (noCRLF) { 104 outbuf = new byte[bytesPerLine]; 105 } else { 106 outbuf = new byte[bytesPerLine + 2]; 107 outbuf[bytesPerLine] = (byte)'\r'; 108 outbuf[bytesPerLine + 1] = (byte)'\n'; 109 } 110 for (inx = 0; inx + lineLimit < len; inx += lineLimit) 111 out.write(encode(b, off + inx, lineLimit, outbuf)); 112 113 for (; inx < len; inx++) 115 write(b[off + inx]); 116 } 117 118 123 public void write(byte[] b) throws IOException { 124 write(b, 0, b.length); 125 } 126 127 132 public void write(int c) throws IOException { 133 buffer[bufsize++] = (byte)c; 134 if (bufsize == 3) { encode(); 136 bufsize = 0; 137 } 138 } 139 140 145 public void flush() throws IOException { 146 if (bufsize > 0) { encode(); bufsize = 0; 149 } 150 out.flush(); 151 } 152 153 157 public void close() throws IOException { 158 flush(); 159 out.close(); 160 } 161 162 163 private final static char pem_array[] = { 164 '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','+','/' }; 173 174 private void encode() throws IOException { 175 if (count + 4 > bytesPerLine) { 178 if (!noCRLF) 179 out.write(newline); 180 count = 0; 181 } 182 out.write(encode(buffer, 0, bufsize, null)); 183 count += 4; 185 } 186 187 193 public static byte[] encode(byte[] inbuf) { 194 if (inbuf.length == 0) 195 return inbuf; 196 return encode(inbuf, 0, inbuf.length, null); 197 } 198 199 204 private static byte[] encode(byte[] inbuf, int off, int size, 205 byte[] outbuf) { 206 if (outbuf == null) 207 outbuf = new byte[((size + 2) / 3) * 4]; 208 int inpos, outpos; 209 int val; 210 for (inpos = off, outpos = 0; size >= 3; size -= 3, outpos += 4) { 211 val = inbuf[inpos++] & 0xff; 212 val <<= 8; 213 val |= inbuf[inpos++] & 0xff; 214 val <<= 8; 215 val |= inbuf[inpos++] & 0xff; 216 outbuf[outpos+3] = (byte)pem_array[val & 0x3f]; 217 val >>= 6; 218 outbuf[outpos+2] = (byte)pem_array[val & 0x3f]; 219 val >>= 6; 220 outbuf[outpos+1] = (byte)pem_array[val & 0x3f]; 221 val >>= 6; 222 outbuf[outpos+0] = (byte)pem_array[val & 0x3f]; 223 } 224 if (size == 1) { 226 val = inbuf[inpos++] & 0xff; 227 val <<= 4; 228 outbuf[outpos+3] = (byte)'='; outbuf[outpos+2] = (byte)'='; outbuf[outpos+1] = (byte)pem_array[val & 0x3f]; 231 val >>= 6; 232 outbuf[outpos+0] = (byte)pem_array[val & 0x3f]; 233 } else if (size == 2) { 234 val = inbuf[inpos++] & 0xff; 235 val <<= 8; 236 val |= inbuf[inpos++] & 0xff; 237 val <<= 2; 238 outbuf[outpos+3] = (byte)'='; outbuf[outpos+2] = (byte)pem_array[val & 0x3f]; 240 val >>= 6; 241 outbuf[outpos+1] = (byte)pem_array[val & 0x3f]; 242 val >>= 6; 243 outbuf[outpos+0] = (byte)pem_array[val & 0x3f]; 244 } 245 return outbuf; 246 } 247 248 259 } 260 | Popular Tags |