KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > parser > PassiveHeaderParserInputStream


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16

17 package org.columba.mail.parser;
18
19 import java.io.FilterInputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22
23 import org.columba.core.base.Barrier;
24 import org.columba.core.base.Mutex;
25 import org.columba.ristretto.io.CharSequenceSource;
26 import org.columba.ristretto.message.Header;
27 import org.columba.ristretto.parser.HeaderParser;
28 import org.columba.ristretto.parser.ParserException;
29
30 /**
31  * FilterInputStream that passively reads and parses
32  * a Header from the stream.
33  *
34  * @author Timo Stich <tstich@users.sourceforge.net>
35  */

36 public class PassiveHeaderParserInputStream extends FilterInputStream JavaDoc {
37
38     private static final int READING = 0;
39     private static final int DONE = 1;
40     private static final String JavaDoc HEADER_END = "\r\n\r\n";
41     
42     
43     private StringBuffer JavaDoc buffer;
44     private int mode;
45     private Header header;
46     
47     private Mutex mutex;
48     private Barrier barrier;
49     
50     /**
51      * Constructs the PassiveHeaderParserInputStream.
52      *
53      * @param arg0 the message stream
54      */

55     public PassiveHeaderParserInputStream(InputStream JavaDoc arg0) {
56         super(arg0);
57         
58         buffer = new StringBuffer JavaDoc();
59         
60         mutex= new Mutex();
61         barrier = new Barrier();
62     }
63
64     public int read() throws IOException JavaDoc {
65         int character = super.read();
66         
67         if( mode == READING ) {
68             if( character == -1 ) {
69                 // The Stream finished before the header was completely
70
// read!
71

72                 // Create a emtpy header an back off
73
header = new Header();
74                 barrier.open();
75             } else {
76                 buffer.append((char)character);
77                 checkHeaderReadComplete();
78             }
79         }
80         
81         return character;
82     }
83
84     private void checkHeaderReadComplete() {
85         mutex.lock();
86         if( buffer.indexOf(HEADER_END) != -1) {
87             mode = DONE;
88             try {
89                 // do the parsing
90
header = HeaderParser.parse(new CharSequenceSource(buffer));
91             } catch (ParserException e) {
92                 //TODO (@author tstich): do something
93
}
94             barrier.open();
95             buffer = null;
96         }
97         mutex.release();
98     }
99
100     public int read(byte[] arg0, int arg1, int arg2) throws IOException JavaDoc {
101         int read = super.read(arg0, arg1, arg2);
102         
103         if( mode == READING ) {
104             if( read == -1 ) {
105                 // The Stream finished before the header was completely
106
// read!
107

108                 // Create a emtpy header an back off
109
header = new Header();
110                 barrier.open();
111             } else {
112                 buffer.append(new String JavaDoc(arg0,0,read,"US-ASCII"));
113                 checkHeaderReadComplete();
114             }
115         }
116         
117         return read;
118     }
119     
120     
121     /**
122      * Checks if the Header is already available.
123      *
124      * @return true if the Header is parsed
125      */

126     public boolean isHeaderAvailable() {
127         return mode == DONE;
128     }
129
130     /**
131      * This call blocks until the Header is parsed.
132      *
133      * @return Returns the header.
134      */

135     public Header getHeader() {
136         barrier.join();
137         
138         return header;
139     }
140 }
141
Popular Tags