1 21 22 27 28 package com.sun.mail.util; 29 30 import java.io.*; 31 32 41 42 public class UUDecoderStream extends FilterInputStream { 43 private String name; 44 private int mode; 45 46 private byte[] buffer; private int bufsize = 0; private int index = 0; private boolean gotPrefix = false; 50 private boolean gotEnd = false; 51 private LineInputStream lin; 52 53 57 public UUDecoderStream(InputStream in) { 58 super(in); 59 lin = new LineInputStream(in); 60 buffer = new byte[45]; } 62 63 76 77 public int read() throws IOException { 78 if (index >= bufsize) { 79 readPrefix(); 80 if (!decode()) 81 return -1; 82 index = 0; } 84 return buffer[index++] & 0xff; } 86 87 public int read(byte[] buf, int off, int len) throws IOException { 88 int i, c; 89 for (i = 0; i < len; i++) { 90 if ((c = read()) == -1) { 91 if (i == 0) i = -1; break; 94 } 95 buf[off+i] = (byte)c; 96 } 97 return i; 98 } 99 100 public boolean markSupported() { 101 return false; 102 } 103 104 public int available() throws IOException { 105 return ((in.available() * 3)/4 + (bufsize-index)); 108 } 109 110 117 public String getName() throws IOException { 118 readPrefix(); 119 return name; 120 } 121 122 129 public int getMode() throws IOException { 130 readPrefix(); 131 return mode; 132 } 133 134 139 private void readPrefix() throws IOException { 140 if (gotPrefix) return; 142 143 String s; 144 for (;;) { 145 s = lin.readLine(); if (s == null) 148 throw new IOException("UUDecoder error: No Begin"); 149 if (s.regionMatches(true, 0, "begin", 0, 5)) { 150 try { 151 mode = Integer.parseInt(s.substring(6,9)); 152 } catch (NumberFormatException ex) { 153 throw new IOException("UUDecoder error: " + ex.toString()); 154 } 155 name = s.substring(10); 156 gotPrefix = true; 157 return; 158 } 159 } 160 } 161 162 private boolean decode() throws IOException { 163 164 if (gotEnd) 165 return false; 166 bufsize = 0; 167 String line; 168 do { 169 line = lin.readLine(); 170 171 176 if (line == null) 177 throw new IOException("Missing End"); 178 if (line.regionMatches(true, 0, "end", 0, 3)) { 179 gotEnd = true; 180 return false; 181 } 182 } while (line.length() == 0); 183 int count = line.charAt(0); 184 if (count < ' ') 185 throw new IOException("Buffer format error"); 186 187 193 count = (count - ' ') & 0x3f; 194 195 if (count == 0) { 196 line = lin.readLine(); 197 if (line == null || !line.regionMatches(true, 0, "end", 0, 3)) 198 throw new IOException("Missing End"); 199 gotEnd = true; 200 return false; 201 } 202 203 int need = ((count * 8)+5)/6; 204 if (line.length() < need + 1) 206 throw new IOException("Short buffer error"); 207 208 int i = 1; 209 byte a, b; 210 216 while (bufsize < count) { 217 a = (byte)((line.charAt(i++) - ' ') & 0x3f); 219 b = (byte)((line.charAt(i++) - ' ') & 0x3f); 220 buffer[bufsize++] = (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3)); 221 222 if (bufsize < count) { 223 a = b; 224 b = (byte)((line.charAt(i++) - ' ') & 0x3f); 225 buffer[bufsize++] = 226 (byte)(((a << 4) & 0xf0) | ((b >>> 2) & 0xf)); 227 } 228 229 if (bufsize < count) { 230 a = b; 231 b = (byte)((line.charAt(i++) - ' ') & 0x3f); 232 buffer[bufsize++] = (byte)(((a << 6) & 0xc0) | (b & 0x3f)); 233 } 234 } 235 return true; 236 } 237 238 253 } 254 | Popular Tags |