1 21 22 27 28 package com.sun.mail.util; 29 30 import java.io.*; 31 32 40 41 public class UUEncoderStream extends FilterOutputStream { 42 private byte[] buffer; private int bufsize = 0; private boolean wrotePrefix = false; 45 46 protected String name; protected int mode; 49 53 public UUEncoderStream(OutputStream out) { 54 this(out, "encoder.buf", 644); 55 } 56 57 62 public UUEncoderStream(OutputStream out, String name) { 63 this(out, name, 644); 64 } 65 66 72 public UUEncoderStream(OutputStream out, String name, int mode) { 73 super(out); 74 this.name = name; 75 this.mode = mode; 76 buffer = new byte[45]; 77 } 78 79 84 public void setNameMode(String name, int mode) { 85 this.name = name; 86 this.mode = mode; 87 } 88 89 public void write(byte[] b, int off, int len) throws IOException { 90 for (int i = 0; i < len; i++) 91 write(b[off + i]); 92 } 93 94 public void write(byte[] data) throws IOException { 95 write(data, 0, data.length); 96 } 97 98 public void write(int c) throws IOException { 99 103 buffer[bufsize++] = (byte)c; 104 if (bufsize == 45) { 105 writePrefix(); 106 encode(); 107 bufsize = 0; 108 } 109 } 110 111 public void flush() throws IOException { 112 if (bufsize > 0) { writePrefix(); 114 encode(); } 116 writeSuffix(); 117 out.flush(); 118 } 119 120 public void close() throws IOException { 121 flush(); 122 out.close(); 123 } 124 125 128 private void writePrefix() throws IOException { 129 if (!wrotePrefix) { 130 PrintStream ps = new PrintStream(out); 131 ps.println("begin " + mode + " " + name); 132 ps.flush(); 133 wrotePrefix = true; 134 } 135 } 136 137 141 private void writeSuffix() throws IOException { 142 PrintStream ps = new PrintStream(out); 143 ps.println(" \nend"); 144 ps.flush(); 145 } 146 147 156 private void encode() throws IOException { 157 byte a, b, c; 158 int c1, c2, c3, c4; 159 int i = 0; 160 161 out.write((bufsize & 0x3f) + ' '); 163 164 while (i < bufsize) { 165 a = buffer[i++]; 166 if (i < bufsize) { 167 b = buffer[i++]; 168 if (i < bufsize) 169 c = buffer[i++]; 170 else c = 1; 172 } 173 else { b = 1; 175 c = 1; 176 } 177 178 c1 = (a >>> 2) & 0x3f; 179 c2 = ((a << 4) & 0x30) | ((b >>> 4) & 0xf); 180 c3 = ((b << 2) & 0x3c) | ((c >>> 6) & 0x3); 181 c4 = c & 0x3f; 182 out.write(c1 + ' '); 183 out.write(c2 + ' '); 184 out.write(c3 + ' '); 185 out.write(c4 + ' '); 186 } 187 out.write('\n'); 189 } 190 191 202 } 203 | Popular Tags |