2 ~~ Licensed to the Apache Software Foundation (ASF) under one
\r
3 ~~ or more contributor license agreements. See the NOTICE file
\r
4 ~~ distributed with this work for additional information
\r
5 ~~ regarding copyright ownership. The ASF licenses this file
\r
6 ~~ to you under the Apache License, Version 2.0 (the
\r
7 ~~ "License"); you may not use this file except in compliance
\r
8 ~~ with the License. You may obtain a copy of the License at
\r
10 ~~ http://www.apache.org/licenses/LICENSE-2.0
\r
12 ~~ Unless required by applicable law or agreed to in writing,
\r
13 ~~ software distributed under the License is distributed on an
\r
14 ~~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
\r
15 ~~ KIND, either express or implied. See the License for the
\r
16 ~~ specific language governing permissions and limitations
\r
17 ~~ under the License.
\r
25 Mime4j provides two different API's: An event based API by using
\r
26 the {{{apidocs/org/apache/james/mime4j/MimeStreamParser.html}
\r
27 MimeStreamParser}}. Alternatively, you may use the iterative
\r
28 API, which is available through the
\r
29 {{{apidocs/org/apache/james/mime4j/MimeTokenStream.html}
\r
30 MimeTokenStream}}. In terms of speed, you should not note
\r
33 * {{{#Token Streams}Token Streams}}
\r
35 * {{{#Sample Token Stream}Sample Token Stream}}
\r
37 * {{{#Event Handlers}Event Handlers}}
\r
39 * {{{#Sample Event Stream}Sample Event Stream}}
\r
43 The iterative approach is using the class
\r
44 {{{apidocs/org/apache/james/mime4j/MimeTokenStream.html}
\r
45 MimeTokenStream}}. Here's an example, how you could use
\r
48 --------------------------------------------------------------------
\r
49 MimeTokenStream stream = new MimeTokenStream();
\r
50 stream.parse(new BufferedInputStream(new FileInputStream("mime.msg")));
\r
51 for (int state = stream.getState();
\r
52 state != MimeTokenStream.T_END_OF_STREAM;
\r
53 state = stream.next()) {
\r
55 case MimeTokenStream.T_BODY:
\r
56 System.out.println("Body detected, contents = "
\r
57 + stream.getInputStream() + ", header data = "
\r
58 + stream.getBodyDescriptor());
\r
60 case MimeTokenStream.T_FIELD:
\r
61 System.out.println("Header field detected: "
\r
62 + stream.getField());
\r
64 case MimeTokenStream.T_START_MULTIPART:
\r
65 System.out.println("Multipart message detexted,"
\r
67 + stream.getBodyDescriptor());
\r
71 --------------------------------------------------------------------
\r
73 The token stream provides a set of tokens. Tokens are identified
\r
74 by a state. Most states are simply event indicators, with no
\r
75 additional data available. However, there are some states,
\r
76 which provide additional data. For example, the state
\r
77 <<<T_BODY>>>, which indicates that an actual body is available,
\r
78 If you note this state, then you may ask for the bodies contents,
\r
79 which are provided through the <<<getInputStream()>>> method,
\r
80 or you might ask for the header data by invoking
\r
81 <<<getBodyDescriptor()>>>.
\r
83 {Sample Token Stream}
\r
85 The following sample should give you a rough idea of the order,
\r
86 in which you'll receive tokens:
\r
88 --------------------------------------------------------------------
\r
116 --------------------------------------------------------------------
\r
118 The example shows a multipart message with two parts.
\r
122 The event based API requires, that you provide an event handler,
\r
123 which receives events. The event handler is an object, which
\r
124 implements the {{{apidocs/org/apache/james/mime4j/ContentHandler.html}
\r
125 ContentHandler}} interface. Here's an example, how you could
\r
126 implement an event handler:
\r
128 --------------------------------------------------------------------
\r
129 public class MyContentHandler extends org.apache.james.mime4j.ContentHandler {
\r
130 public body(BodyDescriptor bd, InputStream is)
\r
131 throws MimeException, IOException {
\r
132 System.out.println("Body detected, contents = "
\r
133 + is + ", header data = " + bd);
\r
135 public void field(String fieldData) throws MimeException {
\r
136 System.out.println("Header field detected: "
\r
139 public void startMultipart(BodyDescriptor bd) throws MimeException {
\r
140 System.out.println("Multipart message detexted, header data = "
\r
145 --------------------------------------------------------------------
\r
147 A little bit of additional code allows us to create an example, which
\r
148 is functionally equivalent to the example from the section on
\r
149 {{{#Token Streams}Token Streams}}:
\r
151 --------------------------------------------------------------------
\r
152 ContentHandler handler = new MyContentHandler();
\r
153 MimeStreamParser parser = new MimeStreamParser();
\r
154 parser.setContentHandler(handler);
\r
155 parser.parse(new BufferedInputStream(new FileInputStream("mime.msg")));
\r
156 --------------------------------------------------------------------
\r
158 {Sample Event Stream}
\r
160 Like above for tokens, we provide an additional example, which
\r
161 demonstrates the typical order of events that you have to expect:
\r
163 --------------------------------------------------------------------
\r
191 --------------------------------------------------------------------
\r