1 21 22 27 28 package com.sun.mail.iap; 29 30 import java.util.Vector ; 31 import java.io.*; 32 import com.sun.mail.util.*; 33 34 38 39 public class Argument { 40 protected Vector items; 41 42 45 public Argument() { 46 items = new Vector (1); 47 } 48 49 54 public void append(Argument arg) { 55 items.ensureCapacity(items.size() + arg.items.size()); 56 for (int i=0; i < arg.items.size(); i++) 57 items.addElement(arg.items.elementAt(i)); 58 } 59 60 69 public void writeString(String s) { 70 items.addElement(new AString(ASCIIUtility.getBytes(s))); 71 } 72 73 77 public void writeString(String s, String charset) 78 throws UnsupportedEncodingException { 79 if (charset == null) writeString(s); 81 else 82 items.addElement(new AString(s.getBytes(charset))); 83 } 84 85 89 public void writeBytes(byte[] b) { 90 items.addElement(b); 91 } 92 93 97 public void writeBytes(ByteArrayOutputStream b) { 98 items.addElement(b); 99 } 100 101 105 public void writeBytes(Literal b) { 106 items.addElement(b); 107 } 108 109 115 public void writeAtom(String s) { 116 items.addElement(new Atom(s)); 117 } 118 119 123 public void writeNumber(int i) { 124 items.addElement(new Integer (i)); 125 } 126 127 131 public void writeNumber(long i) { 132 items.addElement(new Long (i)); 133 } 134 135 139 public void writeArgument(Argument c) { 140 items.addElement(c); 141 } 142 143 146 public void write(Protocol protocol) 147 throws IOException, ProtocolException { 148 int size = items != null ? items.size() : 0; 149 DataOutputStream os = (DataOutputStream)protocol.getOutputStream(); 150 151 for (int i=0; i < size; i++) { 152 if (i > 0) os.write(' '); 154 155 Object o = items.elementAt(i); 156 if (o instanceof Atom) { 157 os.writeBytes(((Atom)o).string); 158 } else if (o instanceof Number ) { 159 os.writeBytes(((Number )o).toString()); 160 } else if (o instanceof AString) { 161 astring(((AString)o).bytes, protocol); 162 } else if (o instanceof byte[]) { 163 literal((byte[])o, protocol); 164 } else if (o instanceof ByteArrayOutputStream) { 165 literal((ByteArrayOutputStream)o, protocol); 166 } else if (o instanceof Literal) { 167 literal((Literal)o, protocol); 168 } else if (o instanceof Argument) { 169 os.write('('); ((Argument)o).write(protocol); 171 os.write(')'); } 173 } 174 } 175 176 179 private void astring(byte[] bytes, Protocol protocol) 180 throws IOException, ProtocolException { 181 DataOutputStream os = (DataOutputStream)protocol.getOutputStream(); 182 int len = bytes.length; 183 184 if (len > 1024) { 186 literal(bytes, protocol); 187 return; 188 } 189 190 boolean quote = len == 0 ? true: false; 192 boolean escape = false; 193 194 byte b; 195 for (int i = 0; i < len; i++) { 196 b = bytes[i]; 197 if (b == '\0' || b == '\r' || b == '\n' || ((b & 0xff) > 0177)) { 198 literal(bytes, protocol); 200 return; 201 } 202 if (b == '*' || b == '%' || b == '(' || b == ')' || b == '{' || 203 b == '"' || b == '\\' || ((b & 0xff) <= ' ')) { 204 quote = true; 205 if (b == '"' || b == '\\') escape = true; 207 } 208 } 209 210 if (quote) os.write('"'); 212 213 if (escape) { 214 for (int i = 0; i < len; i++) { 216 b = bytes[i]; 217 if (b == '"' || b == '\\') 218 os.write('\\'); 219 os.write(b); 220 } 221 } else 222 os.write(bytes); 223 224 225 if (quote) os.write('"'); 227 } 228 229 232 private void literal(byte[] b, Protocol protocol) 233 throws IOException, ProtocolException { 234 startLiteral(protocol, b.length).write(b); 235 } 236 237 240 private void literal(ByteArrayOutputStream b, Protocol protocol) 241 throws IOException, ProtocolException { 242 b.writeTo(startLiteral(protocol, b.size())); 243 } 244 245 248 private void literal(Literal b, Protocol protocol) 249 throws IOException, ProtocolException { 250 b.writeTo(startLiteral(protocol, b.size())); 251 } 252 253 private OutputStream startLiteral(Protocol protocol, int size) 254 throws IOException, ProtocolException { 255 DataOutputStream os = (DataOutputStream)protocol.getOutputStream(); 256 boolean nonSync = protocol.supportsNonSyncLiterals(); 257 258 os.write('{'); 259 os.writeBytes(Integer.toString(size)); 260 if (nonSync) os.writeBytes("+}\r\n"); 262 else 263 os.writeBytes("}\r\n"); 264 os.flush(); 265 266 if (!nonSync) { 269 for (; ;) { 270 Response r = protocol.readResponse(); 271 if (r.isContinuation()) 272 break; 273 if (r.isTagged()) 274 throw new LiteralException(r); 275 } 278 } 279 return os; 280 } 281 } 282 283 class Atom { 284 String string; 285 286 Atom(String s) { 287 string = s; 288 } 289 } 290 291 class AString { 292 byte[] bytes; 293 294 AString(byte[] b) { 295 bytes = b; 296 } 297 } 298 | Popular Tags |