File: IMAPResponseTokenizer.java

package info (click to toggle)
libgnuinet-java 1.1.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,648 kB
  • ctags: 1,882
  • sloc: java: 14,444; sh: 8,326; makefile: 236; xml: 127
file content (445 lines) | stat: -rw-r--r-- 14,569 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/*
 * IMAPResponseTokenizer.java
 * Copyright (C) 2003 The Free Software Foundation
 * 
 * This file is part of GNU inetlib, a library.
 * 
 * GNU inetlib is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * GNU inetlib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Linking this library statically or dynamically with other modules is
 * making a combined work based on this library.  Thus, the terms and
 * conditions of the GNU General Public License cover the whole
 * combination.
 *
 * As a special exception, the copyright holders of this library give you
 * permission to link this library with independent modules to produce an
 * executable, regardless of the license terms of these independent
 * modules, and to copy and distribute the resulting executable under
 * terms of your choice, provided that you also meet, for each linked
 * independent module, the terms and conditions of the license of that
 * module.  An independent module is a module which is not derived from
 * or based on this library.  If you modify this library, you may extend
 * this exception to your version of the library, but you are not
 * obliged to do so.  If you do not wish to do so, delete this
 * exception statement from your version.
 */

package gnu.inet.imap;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.net.ProtocolException;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * An object that can parse an underlying socket stream containing IMAP
 * protocol server responses into IMAPResponse tokens.
 *
 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
 */
public class IMAPResponseTokenizer
  implements IMAPConstants
{

  /**
   * The server stream.
   */
  protected InputStream in;

  private byte[] buffer = null;

  private static final int BUFFER_SIZE = 4096; // Seems to be a good value
  private static final String DEFAULT_ENCODING = "US-ASCII";

  private static final int STATE_TAG = 0;
  private static final int STATE_COUNT = 1;
  private static final int STATE_ID = 2;
  private static final int STATE_MAYBE_CODE = 3;
  private static final int STATE_CODE = 4;
  private static final int STATE_LITERAL_LENGTH = 5;
  private static final int STATE_LITERAL = 6;
  private static final int STATE_TEXT = 7;
  private static final int STATE_STATUS = 8;

  /**
   * Constructor.
   * @param in the server socket input stream
   */
  public IMAPResponseTokenizer(InputStream in)
  {
    this.in = in;
  }

  /*
   * Reads bytes, from the underlying stream if necessary, or from the
   * cache. If moreNeeded is specified, always performs a read to append
   * more to the cache.
   */
  byte[] read(boolean moreNeeded)
    throws IOException
  {
    if (buffer != null && !moreNeeded && buffer.length > 0)
      {
        return buffer;
      }
    int max = in.available();
    if (max < 1)
      {
        max = BUFFER_SIZE;
      }
    byte[] tmp = new byte[max];
    int len = 0;
    while (len == 0)
      {
        len = in.read(tmp, 0, max);
      }
    if (len == -1)
      {
        return null;              // EOF
      }
    int blen =(buffer == null) ? 0 : buffer.length;
    byte[] uni = new byte[blen + len];
    if (blen != 0)
      {
        System.arraycopy(buffer, 0, uni, 0, blen);
      }
    System.arraycopy(tmp, 0, uni, blen, len);
    buffer = uni;
    return buffer;
  }

  /*
   * Invalidates the byte cache up to the specified index.
   */
  void mark(int index)
  {
    int len = buffer.length;
    int start = index + 1;
    if (start < len)
      {
        int n =(len - start);
        byte[] tmp = new byte[n];
        System.arraycopy(buffer, start, tmp, 0, n);
        buffer = tmp;
      }
    else
      {
        buffer = null;
      }
  }

  /**
   * Returns the next IMAPResponse.
   */
  public IMAPResponse next()
    throws IOException
  {
    // Perform read
    byte[] buf = read(false);
    if (buf == null)
      {
        return null;              // pass EOF back up the chain
      }
    int len = buf.length;
    
    IMAPResponse response = new IMAPResponse();
    ByteArrayOutputStream genericSink = new ByteArrayOutputStream();
    ByteArrayOutputStream literalSink = null;
    int literalCount = 0, literalLength = -1;
    Stack context = new Stack();
    int state = STATE_TAG;
    boolean inQuote = false;
    boolean inContent = false;
    for (int i = 0; i < len; i++)
      {
        byte b = buf[i];
        switch (state)
          {
          case STATE_TAG:          // expect tag
            if (i == 0 && b == 0x2a)        // untagged
              {
                response.tag = IMAPResponse.UNTAGGED;
              }
            else if (i == 0 && b == 0x2b)   // continuation
              {
                response.tag = IMAPResponse.CONTINUATION;
              }
            else if (b == 0x20)     // delimiter
              {
                if (response.tag == null)
                  {
                    byte[] tb = genericSink.toByteArray();
                    response.tag = new String(tb, DEFAULT_ENCODING);
                  }
                genericSink.reset();
                if (response.isContinuation())
                  {
                    state = STATE_TEXT;
                  }
                else
                  {
                    state = STATE_COUNT;
                  }
              }
            else                    // tag literal
              {
                genericSink.write(b);
              }
            break;
          case STATE_COUNT:        // expect count or id
            if (b < 0x30 || b > 0x39)
              {
                state = STATE_ID;
              }
            if (b == 0x20)          // delimiter
              {
                byte[] cb = genericSink.toByteArray();
                genericSink.reset();
                String cs = new String(cb, DEFAULT_ENCODING);
                try
                  {
                    response.count = Integer.parseInt(cs);
                  }
                catch (NumberFormatException e)
                  {
                    throw new ProtocolException("Expecting number: " + cs);
                  }
                state = STATE_ID;
              }
            else
              {
                genericSink.write(b);
              }
            break;
          case STATE_ID:           // expect id
            if (b == 0x20)          // delimiter
              {
                byte[] ib = genericSink.toByteArray();
                genericSink.reset();
                response.id = new String(ib, DEFAULT_ENCODING).intern();
                state = STATE_MAYBE_CODE;
              }
            else if (b == 0x0a)     // EOL
              {
                byte[] ib = genericSink.toByteArray();
                genericSink.reset();
                response.id = new String(ib, DEFAULT_ENCODING).intern();
                state = STATE_TAG;
                // mark bytes read
                mark(i);
                return response;
              }
            else if (b != 0x0d)     // id literal
              {
                genericSink.write(b);
              }
            break;
          case STATE_MAYBE_CODE:   // expect code or text
            if (b == 0x28 || b == 0x5b)
              {
                List top = new ArrayList();
                response.code = top;
                context.push(top);
                state = STATE_CODE;
              }
            else
              {
                if (response.id == FETCH)
                  {
                    // We can't have text here so it must be the beginning of
                    // FETCH FLAGS. Go back to ID state.
                    genericSink.reset();
                    byte[] fetchBytes =
                      new byte[] { 0x46, 0x45, 0x54, 0x43, 0x48, 0x20};
                    genericSink.write(fetchBytes);
                    genericSink.write(b);
                    state = STATE_ID;
                  }
                else if (response.id == STATUS)
                  {
                    // We are in the mailbox name part of the STATUS response
                    genericSink.write(b);
                    state = STATE_STATUS;
                  }
                else
                  {
                    genericSink.write(b);
                    state = STATE_TEXT;
                  }
              }
            break;
          case STATE_STATUS:
            if (b == 0x20)          // delimiter
              {
                response.mailbox = genericSink.toString();
                genericSink.reset();
                state = STATE_MAYBE_CODE;
              }
            else
              {
                genericSink.write(b);
              }
            break;
          case STATE_CODE:         // response code inside parentheses
            if (b == 0x22)          // quote delimiter
              {
                inQuote = !inQuote;
              }
            else if (inQuote)
              {
                genericSink.write(b);
              }
            else
              {
                if (b == 0x28 || b == 0x5b)   // start parenthesis/bracket
                  {
                    List parent =(List) context.peek();
                    List top = new ArrayList();
                    if (genericSink.size() > 0)
                      {
                        byte[] tb = genericSink.toByteArray();
                        String token =
                          new String(tb, DEFAULT_ENCODING).intern();
                        Pair pair = new Pair(token, top);
                        parent.add(pair);
                        genericSink.reset();
                      }
                    else
                      {
                        parent.add(top);
                      }
                    context.push(top);
                  }
                else if (b == 0x29 || b == 0x5d)      // end parenthesis/bracket
                  {
                    List top =(List) context.pop();
                    // flush genericSink
                    if (genericSink.size() > 0)
                      {
                        byte[] tb = genericSink.toByteArray();
                        String token =
                          new String(tb, DEFAULT_ENCODING).intern();
                        top.add(token);
                        genericSink.reset();
                      }
                  }
                else if (b == 0x7b)   // literal length
                  {
                    genericSink.reset();
                    state = STATE_LITERAL_LENGTH;
                  }
                else if (b == 0x20)   // token delimiter
                  {
                    if (context.size() == 0)    // end state
                      {
                        state = STATE_TEXT;
                      }
                    else                // add token
                      {
                        List top =(List) context.peek();
                        // flush genericSink
                        if (genericSink.size() > 0)
                          {
                            byte[] tb = genericSink.toByteArray();
                            String token =
                              new String(tb, DEFAULT_ENCODING).intern();
                            top.add(token);
                            genericSink.reset();
                          }
                      }
                  }
                else if (b == 0x0a)   // EOL
                  {
                    state = STATE_TAG;
                    // mark bytes read
                    mark(i);
                    return response;
                  }
                else if (b != 0x0d)   // ignore CR
                  {
                    genericSink.write(b);
                  }
              }
            break;
          case STATE_LITERAL_LENGTH:
            if (b == 0x7d)          // end literal length
              {
                byte[] cb = genericSink.toByteArray();
                genericSink.reset();
                String cs = new String(cb, DEFAULT_ENCODING);
                try
                  {
                    literalLength = Integer.parseInt(cs);
                  }
                catch (NumberFormatException e)
                  {
                    throw new ProtocolException("Expecting number: " + cs);
                  }
              }
            else if (b == 0x0a)     // start literal
              {
                state = STATE_LITERAL;
                literalSink = new ByteArrayOutputStream();
                literalCount = 0;
              }
            else if (b != 0x0d)     // ignore CR
              {
                genericSink.write(b);
              }
            break;
          case STATE_LITERAL:
            if (literalCount >= literalLength)
              {
                List top =(List) context.peek();
                byte[] literal = literalSink.toByteArray();
                top.add(literal);
                literalSink = null;
                inContent = false;
                state = STATE_CODE;
              }
            else
              {
                literalSink.write(b);
                literalCount++;
              }
            break;
          case STATE_TEXT:         // human-readable text
            if (b == 0x0a)          // delimiter
              {
                byte[] tb = genericSink.toByteArray();
                genericSink.reset();
                response.text = new String(tb, DEFAULT_ENCODING);
                state = STATE_TAG;
                // mark bytes read
                mark(i);
                return response;
              }
            else if (b != 0x0d)     // ignore CR
              {
                genericSink.write(b);
              }
            break;
          }
      }
    // get more bytes
    buf = read(true);
    return next();
  }
  
}