1 21 package gnu.mail.util; 22 23 import java.io.InputStream ; 24 import java.io.FilterInputStream ; 25 import java.io.IOException ; 26 27 28 36 public class UUDecoderStream 37 extends FilterInputStream 38 { 39 40 42 private byte[] decode_buffer; 43 44 46 private int index; 47 48 50 private byte[] buffer; 51 52 54 private int bufsize; 55 56 58 private boolean hasPrefix; 59 60 62 protected String name; 63 64 66 protected int mode; 67 68 72 public UUDecoderStream(InputStream in) 73 { 74 super(in); 75 if (!(in instanceof LineInputStream)) 76 this.in = new LineInputStream(in); 77 this.name = null; 78 this.mode = -1; 79 hasPrefix = false; 80 buffer = new byte[45]; 81 bufsize = 0; 82 decode_buffer = new byte[60]; 83 index = 0; 84 } 85 86 89 public String getName() 90 throws IOException 91 { 92 if (!hasPrefix) 93 readPrefix(); 94 return name; 95 } 96 97 101 private void decode() 102 throws IOException 103 { 104 int decode_index; 105 int c1; 106 int c2; 107 int c3; 108 int c4; 109 int a; 110 int b; 111 int c; 112 decode_index = 0; 114 index = 0; 115 bufsize = 0; 116 while (decode_index < decode_buffer.length) 117 { 118 c1 = (decode_buffer[decode_index] - ' ') & 0x3f; 120 c2 = (decode_buffer[decode_index+1] - ' ') & 0x3f; 121 c3 = (decode_buffer[decode_index+2] - ' ') & 0x3f; 122 c4 = (decode_buffer[decode_index+3] - ' ') & 0x3f; 123 a = ((c1 << 2) & 0xfc) | ((c2 >>> 4) & 3); 125 b = ((c2 << 4) & 0xf0) | ((c3 >>> 2) & 0xf); 126 c = ((c3 << 6) & 0xc0) | (c4 & 0x3f); 127 buffer[bufsize] = (byte) (a & 0xff); 129 buffer[bufsize+1] = (byte) (b & 0xff); 130 buffer[bufsize+2] = (byte) (c & 0xff); 131 decode_index += 4; 133 bufsize += 3; 134 } 135 } 136 137 145 public int read() 146 throws IOException 147 { 148 int next; 149 int lengthByte; 150 int length; 151 int decodeLength; 152 int result; 153 String line; 154 if (hasPrefix == false) 156 readPrefix(); 157 LineInputStream lin = (LineInputStream)in; 159 if (index == bufsize) 160 { 161 line = lin.readLine(); 163 lengthByte = line.getBytes()[0]; 165 length = (lengthByte - ' ') & 0x3f; 166 if (length > 45) 168 throw new IOException ("UUDecode error: line length to large (" 169 + length + ")"); 170 if (length == 0) 172 { 173 readSuffix(); 174 return -1; 175 } 176 decodeLength = line.length() - 1; 178 if ((decodeLength % 4) != 0) 180 throw new IOException ("UUDecode error: line length not multiple of 4 (" 181 + decodeLength + ")"); 182 decode_buffer = line.substring(1).getBytes(); 184 decode(); 186 } 187 next = buffer[index]; 189 index += 1; 191 return next; 192 } 193 194 204 public int read(byte[] bytes, int offset, int length) 205 throws IOException 206 { 207 int index; 213 for (index = offset; index < length; index++) 215 bytes[index] = (byte) read(); 216 return length - offset; 218 } 219 220 223 public int available() 224 throws IOException 225 { 226 if (hasPrefix == false) 227 { 228 readPrefix(); 229 read(); 230 index = 0; 231 } 232 return bufsize - index; 234 } 235 236 238 public boolean markSupported() 239 { 240 return false; 241 } 242 243 248 public int getMode() 249 throws IOException 250 { 251 if (!hasPrefix) 252 readPrefix(); 253 return mode; 254 } 255 256 261 private void readPrefix() 262 throws IOException 263 { 264 String line = ((LineInputStream)in).readLine(); 270 if (!line.startsWith("begin") == false) 272 throw new IOException ("UUDecoder error: No Begin"); 273 try 275 { 276 mode = Integer.parseInt(line.substring(6, 9)); 277 } 278 catch (Exception e) 279 { 280 throw new IOException ("UUDecoder error: Unable to determine mode"); 281 } 282 try 284 { 285 name = line.substring(10); 286 } 287 catch (Exception e) 288 { 289 throw new IOException ("UUDecoder error: Unable to determine name"); 290 } 291 hasPrefix = true; 293 } 294 295 300 private void readSuffix() 301 throws IOException 302 { 303 String line = ((LineInputStream)in).readLine(); 305 if (!line.startsWith("end")) 307 throw new IOException ("UUDecoder error: No End: " + line); 308 } 309 310 } 311 | Popular Tags |