Source Code Cross Referenced for PassiveHeaderParserInputStream.java in  » Mail-Clients » columba-1.4 » org » columba » mail » parser » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Mail Clients » columba 1.4 » org.columba.mail.parser 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        //The contents of this file are subject to the Mozilla Public License Version 1.1
002:        //(the "License"); you may not use this file except in compliance with the 
003:        //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004:        //
005:        //Software distributed under the License is distributed on an "AS IS" basis,
006:        //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
007:        //for the specific language governing rights and
008:        //limitations under the License.
009:        //
010:        //The Original Code is "The Columba Project"
011:        //
012:        //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013:        //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003. 
014:        //
015:        //All Rights Reserved.
016:
017:        package org.columba.mail.parser;
018:
019:        import java.io.FilterInputStream;
020:        import java.io.IOException;
021:        import java.io.InputStream;
022:
023:        import org.columba.core.base.Barrier;
024:        import org.columba.core.base.Mutex;
025:        import org.columba.ristretto.io.CharSequenceSource;
026:        import org.columba.ristretto.message.Header;
027:        import org.columba.ristretto.parser.HeaderParser;
028:        import org.columba.ristretto.parser.ParserException;
029:
030:        /**
031:         * FilterInputStream that passively reads and parses 
032:         * a Header from the stream. 
033:         * 
034:         * @author Timo Stich <tstich@users.sourceforge.net>
035:         */
036:        public class PassiveHeaderParserInputStream extends FilterInputStream {
037:
038:            private static final int READING = 0;
039:            private static final int DONE = 1;
040:            private static final String HEADER_END = "\r\n\r\n";
041:
042:            private StringBuffer buffer;
043:            private int mode;
044:            private Header header;
045:
046:            private Mutex mutex;
047:            private Barrier barrier;
048:
049:            /**
050:             * Constructs the PassiveHeaderParserInputStream.
051:             * 
052:             * @param arg0 the message stream
053:             */
054:            public PassiveHeaderParserInputStream(InputStream arg0) {
055:                super (arg0);
056:
057:                buffer = new StringBuffer();
058:
059:                mutex = new Mutex();
060:                barrier = new Barrier();
061:            }
062:
063:            public int read() throws IOException {
064:                int character = super .read();
065:
066:                if (mode == READING) {
067:                    if (character == -1) {
068:                        // The Stream finished before the header was completely
069:                        // read!
070:
071:                        // Create a emtpy header an back off
072:                        header = new Header();
073:                        barrier.open();
074:                    } else {
075:                        buffer.append((char) character);
076:                        checkHeaderReadComplete();
077:                    }
078:                }
079:
080:                return character;
081:            }
082:
083:            private void checkHeaderReadComplete() {
084:                mutex.lock();
085:                if (buffer.indexOf(HEADER_END) != -1) {
086:                    mode = DONE;
087:                    try {
088:                        // do the parsing
089:                        header = HeaderParser.parse(new CharSequenceSource(
090:                                buffer));
091:                    } catch (ParserException e) {
092:                        //TODO (@author tstich): do something
093:                    }
094:                    barrier.open();
095:                    buffer = null;
096:                }
097:                mutex.release();
098:            }
099:
100:            public int read(byte[] arg0, int arg1, int arg2) throws IOException {
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(arg0, 0, read, "US-ASCII"));
113:                        checkHeaderReadComplete();
114:                    }
115:                }
116:
117:                return read;
118:            }
119:
120:            /**
121:             * Checks if the Header is already available.
122:             * 
123:             * @return true if the Header is parsed
124:             */
125:            public boolean isHeaderAvailable() {
126:                return mode == DONE;
127:            }
128:
129:            /**
130:             * This call blocks until the Header is parsed.
131:             * 
132:             * @return Returns the header.
133:             */
134:            public Header getHeader() {
135:                barrier.join();
136:
137:                return header;
138:            }
139:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.