1 18 package org.apache.batik.util; 19 20 import java.io.IOException ; 21 import java.io.OutputStream ; 22 import java.io.PrintStream ; 23 24 37 public class Base64EncoderStream extends OutputStream { 38 39 40 private final static byte pem_array[] = { 41 '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','+','/' }; 51 52 byte [] atom = new byte[3]; 53 int atomLen = 0; 54 byte [] encodeBuf = new byte[4]; 55 int lineLen = 0; 56 57 PrintStream out; 58 boolean closeOutOnClose; 59 60 public Base64EncoderStream(OutputStream out) { 61 this.out = new PrintStream (out); 62 closeOutOnClose = true; 63 } 64 65 public Base64EncoderStream(OutputStream out, boolean closeOutOnClose) { 66 this.out = new PrintStream (out); 67 this.closeOutOnClose = closeOutOnClose; 68 } 69 70 public void close () throws IOException { 71 if (out != null) { 72 encodeAtom(); 73 out.flush(); 74 if (closeOutOnClose) 75 out.close(); 76 out=null; 77 } 78 } 79 80 86 public void flush() throws IOException { 87 out.flush(); 88 } 89 90 public void write(int b) throws IOException { 91 atom[atomLen++] = (byte)b; 92 if (atomLen == 3) 93 encodeAtom(); 94 } 95 96 public void write(byte []data) throws IOException { 97 encodeFromArray(data, 0, data.length); 98 } 99 100 public void write(byte [] data, int off, int len) throws IOException { 101 encodeFromArray(data, off, len); 102 } 103 104 110 void encodeAtom() throws IOException { 111 byte a, b, c; 112 113 switch (atomLen) { 114 case 0: return; 115 case 1: 116 a = atom[0]; 117 encodeBuf[0] = pem_array[((a >>> 2) & 0x3F)]; 118 encodeBuf[1] = pem_array[((a << 4) & 0x30)]; 119 encodeBuf[2] = encodeBuf[3] = '='; 120 break; 121 case 2: 122 a = atom[0]; 123 b = atom[1]; 124 encodeBuf[0] = pem_array[((a >>> 2) & 0x3F)]; 125 encodeBuf[1] = pem_array[(((a << 4) & 0x30) | ((b >>> 4) & 0x0F))]; 126 encodeBuf[2] = pem_array[((b << 2) & 0x3C)]; 127 encodeBuf[3] = '='; 128 break; 129 default: 130 a = atom[0]; 131 b = atom[1]; 132 c = atom[2]; 133 encodeBuf[0] = pem_array[((a >>> 2) & 0x3F)]; 134 encodeBuf[1] = pem_array[(((a << 4) & 0x30) | ((b >>> 4) & 0x0F))]; 135 encodeBuf[2] = pem_array[(((b << 2) & 0x3C) | ((c >>> 6) & 0x03))]; 136 encodeBuf[3] = pem_array[c & 0x3F]; 137 } 138 if (lineLen == 64) { 139 out.println(); 140 lineLen = 0; 141 } 142 out.write(encodeBuf); 143 144 lineLen += 4; 145 atomLen = 0; 146 } 147 148 154 void encodeFromArray(byte[] data, int offset, int len) 155 throws IOException { 156 byte a, b, c; 157 if (len == 0) 158 return; 159 160 164 if (atomLen != 0) { 165 switch(atomLen) { 166 case 1: 167 atom[1] = data[offset++]; len--; atomLen++; 168 if (len == 0) return; 169 atom[2] = data[offset++]; len--; atomLen++; 170 break; 171 case 2: 172 atom[2] = data[offset++]; len--; atomLen++; 173 break; 174 default: 175 } 176 encodeAtom(); 177 } 178 179 while (len >=3) { 180 a = data[offset++]; 181 b = data[offset++]; 182 c = data[offset++]; 183 184 encodeBuf[0] = pem_array[((a >>> 2) & 0x3F)]; 185 encodeBuf[1] = pem_array[(((a << 4) & 0x30) | ((b >>> 4) & 0x0F))]; 186 encodeBuf[2] = pem_array[(((b << 2) & 0x3C) | ((c >>> 6) & 0x03))]; 187 encodeBuf[3] = pem_array[c & 0x3F]; 188 out.write(encodeBuf); 189 190 lineLen += 4; 191 if (lineLen == 64) { 192 out.println(); 193 lineLen = 0; 194 } 195 196 len -=3; 197 } 198 199 switch (len) { 200 case 1: 201 atom[0] = data[offset]; 202 break; 203 case 2: 204 atom[0] = data[offset]; 205 atom[1] = data[offset+1]; 206 break; 207 default: 208 } 209 atomLen = len; 210 } 211 212 213 214 } 215 | Popular Tags |