public class MimeTokenStream extends Object
Parses MIME (or RFC822) message streams of bytes or characters. The stream is converted into an event stream.
Typical usage:
MimeTokenStream stream = new MimeTokenStream(); InputStream instream = new FileInputStream("mime.msg"); try { stream.parse(instream); for (int state = stream.getState(); state != MimeTokenStream.T_END_OF_STREAM; state = stream.next()) { switch (state) { case MimeTokenStream.T_BODY: System.out.println("Body detected, contents = " + stream.getInputStream() + ", header data = " + stream.getBodyDescriptor()); break; case MimeTokenStream.T_FIELD: System.out.println("Header field detected: " + stream.getField()); break; case MimeTokenStream.T_START_MULTIPART: System.out.println("Multipart message detexted," + " header data = " + stream.getBodyDescriptor()); ... } } } finally { instream.close(); }
Instances of MimeTokenStream
are reusable: Invoking the
method parse(InputStream)
resets the token streams internal
state. However, they are definitely not thread safe. If you
have a multi threaded application, then the suggested use is to have
one instance per thread.
Constructor and Description |
---|
MimeTokenStream()
Constructs a standard (lax) stream.
|
MimeTokenStream(MimeConfig config) |
MimeTokenStream(MimeConfig config,
BodyDescriptorBuilder bodyDescBuilder) |
MimeTokenStream(MimeConfig config,
DecodeMonitor monitor,
BodyDescriptorBuilder bodyDescBuilder) |
MimeTokenStream(MimeConfig config,
DecodeMonitor monitor,
FieldBuilder fieldBuilder,
BodyDescriptorBuilder bodyDescBuilder) |
Modifier and Type | Method and Description |
---|---|
BodyDescriptor |
getBodyDescriptor()
Gets a descriptor for the current entity.
|
MimeConfig |
getConfig() |
InputStream |
getDecodedInputStream()
This method returns a transfer decoded stream based on the MIME
fields with the standard defaults.
|
Field |
getField()
This method is valid, if
getState() returns EntityState.T_FIELD . |
InputStream |
getInputStream()
This method returns the raw entity, preamble, or epilogue contents.
|
Reader |
getReader()
Gets a reader configured for the current body or body part.
|
RecursionMode |
getRecursionMode()
Gets the current recursion mode.
|
EntityState |
getState()
Returns the current state.
|
boolean |
isRaw()
Determines if this parser is currently in raw mode.
|
EntityState |
next()
This method advances the token stream to the next token.
|
void |
parse(InputStream stream)
Instructs the
MimeTokenStream to parse the given streams contents. |
Field |
parseHeadless(InputStream stream,
String contentType)
Instructs the
MimeTokenStream to parse the given content with
the content type. |
void |
setRecursionMode(RecursionMode mode)
Sets the current recursion.
|
static String |
stateToString(EntityState state)
Renders a state as a string suitable for logging.
|
void |
stop()
Finishes the parsing and stops reading lines.
|
public MimeTokenStream()
MimeConfig.setStrictParsing(boolean)
to turn on strict
parsing mode and pass the config object to
MimeTokenStream(MimeConfig)
to create
a stream that strictly validates the input.public MimeTokenStream(MimeConfig config)
public MimeTokenStream(MimeConfig config, BodyDescriptorBuilder bodyDescBuilder)
public MimeTokenStream(MimeConfig config, DecodeMonitor monitor, BodyDescriptorBuilder bodyDescBuilder)
public MimeTokenStream(MimeConfig config, DecodeMonitor monitor, FieldBuilder fieldBuilder, BodyDescriptorBuilder bodyDescBuilder)
public void parse(InputStream stream)
MimeTokenStream
to parse the given streams contents.
If the MimeTokenStream
has already been in use, resets the streams
internal state.public Field parseHeadless(InputStream stream, String contentType)
Instructs the MimeTokenStream
to parse the given content with
the content type. The message stream is assumed to have no message header
and is expected to begin with a message body. This can be the case when
the message content is transmitted using a different transport protocol
such as HTTP.
If the MimeTokenStream
has already been in use, resets the
streams internal state.
public boolean isRaw()
true
if in raw mode, false
otherwise.setRecursionMode(RecursionMode)
public RecursionMode getRecursionMode()
RecursionMode.M_RAW
mode does not parse the part at all.
RecursionMode.M_RECURSE
mode recursively parses each mail
when an message/rfc822
part is encountered;
RecursionMode.M_NO_RECURSE
does not.public void setRecursionMode(RecursionMode mode)
RecursionMode.M_RAW
mode does not parse the part at all.
RecursionMode.M_RECURSE
mode recursively parses each mail
when an message/rfc822
part is encountered;
RecursionMode.M_NO_RECURSE
does not.mode
- RecursionMode.M_RECURSE
, RecursionMode.M_RAW
or
RecursionMode.M_NO_RECURSE
public void stop()
public EntityState getState()
public InputStream getInputStream()
getState()
returns either of
EntityState.T_RAW_ENTITY
, EntityState.T_PREAMBLE
, or
EntityState.T_EPILOGUE
.IllegalStateException
- getState()
returns an
invalid value.public InputStream getDecodedInputStream()
getState()
returns either of
EntityState.T_RAW_ENTITY
, EntityState.T_PREAMBLE
, or
EntityState.T_EPILOGUE
.IllegalStateException
- getState()
returns an
invalid value.public Reader getReader()
getInputStream()
.
Consult the javadoc for that method for known limitations.Reader
, not nullIllegalStateException
- getState()
returns an
invalid valueUnsupportedCharsetException
- if there is no JVM support
for decoding the charsetIllegalCharsetNameException
- if the charset name specified
in the mime type is illegalgetInputStream()
public BodyDescriptor getBodyDescriptor()
Gets a descriptor for the current entity.
This method is valid if getState()
returns:
BodyDescriptor
, not nullspublic Field getField()
getState()
returns EntityState.T_FIELD
.IllegalStateException
- getState()
returns another
value than EntityState.T_FIELD
.public EntityState next() throws IOException, MimeException
IllegalStateException
- The method has been called, although
getState()
was already EntityState.T_END_OF_STREAM
.IOException
MimeException
public static final String stateToString(EntityState state)
state
- public MimeConfig getConfig()
Copyright © 2004-2012 The Apache Software Foundation. All Rights Reserved.