1 21 package gnu.mail.util; 22 23 24 import java.io.OutputStream ; 26 import java.io.FilterOutputStream ; 27 import java.io.IOException ; 28 29 35 public class UUEncoderStream 36 extends FilterOutputStream 37 { 38 39 43 private byte[] buffer; 44 45 48 private int bufsize; 49 50 53 private boolean wrotePrefix; 54 55 58 protected String name; 59 60 63 protected int mode; 64 65 70 public UUEncoderStream(OutputStream stream) 71 { 72 this(stream, "encoder.buf", 644); 73 } 75 81 public UUEncoderStream(OutputStream stream, String name) 82 { 83 this(stream, name, 644); 84 } 86 92 public UUEncoderStream(OutputStream stream, String name, int mode) 93 { 94 super(stream); 95 this.name = name; 96 this.mode = mode; 97 wrotePrefix = false; 98 buffer = new byte[45]; 99 bufsize = 0; 100 } 102 103 107 111 public void flush() throws IOException 112 { 113 encode(); 114 } 116 123 public void write(byte[] bytes, int offset, int length) 124 throws IOException 125 { 126 127 133 int index; 135 136 for (index = offset; index < length; index++) 138 { 139 write(bytes[index]); 140 } 142 } 144 149 public void write(byte[] bytes) throws IOException 150 { 151 write(bytes, 0, bytes.length); 152 } 154 159 public void write(int b) throws IOException 160 { 161 162 if (wrotePrefix == false) 164 { 165 writePrefix(); 166 } 168 buffer[bufsize] = (byte) b; 170 bufsize += 1; 171 172 if (bufsize == buffer.length) 174 { 175 encode(); 176 } 178 } 180 184 public void close() throws IOException 185 { 186 187 flush(); 189 190 writeSuffix(); 192 193 out.close(); 195 196 } 198 202 private void encode() throws IOException 203 { 204 205 int index; 207 int c1; 208 int c2; 209 int c3; 210 int c4; 211 int a; 212 int b; 213 int c; 214 215 super.write((bufsize & 0x3f) + ' '); 217 218 index = 0; 220 while (index < bufsize) 221 { 222 223 a = buffer[index]; 225 if (index + 1 < bufsize) 226 { 227 b = buffer[index+1]; 228 } else 229 { 230 b = 1; 231 } if (index + 2 < bufsize) 233 { 234 c = buffer[index+2]; 235 } else 236 { 237 c = 1; 238 } 240 c1 = (a >>> 2) & 0xff; 242 c2 = ((a << 4) & 0x30) | ((b >>> 4) & 0xf); 243 c3 = ((b << 2) & 0x3c) | ((c >>> 6) & 0x3); 244 c4 = c & 0x3f; 245 246 out.write(c1 + ' '); 248 out.write(c2 + ' '); 249 out.write(c3 + ' '); 250 out.write(c4 + ' '); 251 252 index += 3; 254 255 } 257 out.write('\n'); 259 260 bufsize = 0; 262 263 } 265 272 public void setNameMode(String name, int mode) 273 { 274 this.name = name; 275 this.mode = mode; 276 } 278 282 private void writePrefix() throws IOException 283 { 284 285 StringBuffer prefix; 287 String modeString; 288 289 prefix = new StringBuffer (); 291 prefix.append("begin "); 292 if (mode < 100) 293 { 294 prefix.append("0"); 295 } if (mode < 10) 297 { 298 prefix.append("0"); 299 } prefix.append(mode); 301 prefix.append(" " + name + "\n"); 302 303 out.write(prefix.toString().getBytes()); 305 306 wrotePrefix = true; 308 309 } 311 316 private void writeSuffix() throws IOException 317 { 318 319 String suffix; 321 322 suffix = " \nend\n"; 324 325 out.write(suffix.getBytes()); 327 328 } 330 331 } | Popular Tags |