public class MimeTokenStream
extends java.lang.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.
| Modifier and Type | Field and Description |
|---|---|
private BodyDescriptorBuilder |
bodyDescBuilder |
private MimeConfig |
config |
private EntityStateMachine |
currentStateMachine |
private java.util.LinkedList<EntityStateMachine> |
entities |
private FieldBuilder |
fieldBuilder |
private DecodeMonitor |
monitor |
private RecursionMode |
recursionMode |
private MimeEntity |
rootentity |
private EntityState |
state |
| 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 |
|---|---|
private void |
doParse(java.io.InputStream stream,
EntityState start) |
BodyDescriptor |
getBodyDescriptor()
Gets a descriptor for the current entity.
|
MimeConfig |
getConfig() |
java.io.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. |
java.io.InputStream |
getInputStream()
This method returns the raw entity, preamble, or epilogue contents.
|
java.io.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(java.io.InputStream stream)
Instructs the
MimeTokenStream to parse the given streams contents. |
Field |
parseHeadless(java.io.InputStream stream,
java.lang.String contentType)
Instructs the
MimeTokenStream to parse the given content with
the content type. |
void |
setRecursionMode(RecursionMode mode)
Sets the current recursion.
|
static java.lang.String |
stateToString(EntityState state)
Renders a state as a string suitable for logging.
|
void |
stop()
Finishes the parsing and stops reading lines.
|
private final MimeConfig config
private final DecodeMonitor monitor
private final FieldBuilder fieldBuilder
private final BodyDescriptorBuilder bodyDescBuilder
private final java.util.LinkedList<EntityStateMachine> entities
private EntityState state
private EntityStateMachine currentStateMachine
private RecursionMode recursionMode
private MimeEntity rootentity
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(java.io.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(java.io.InputStream stream, java.lang.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.
private void doParse(java.io.InputStream stream,
EntityState start)
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_RECURSEpublic void stop()
public EntityState getState()
public java.io.InputStream getInputStream()
getState() returns either of
EntityState.T_RAW_ENTITY, EntityState.T_PREAMBLE, or
EntityState.T_EPILOGUE.java.lang.IllegalStateException - getState() returns an
invalid value.public java.io.InputStream getDecodedInputStream()
getState() returns either of
EntityState.T_RAW_ENTITY, EntityState.T_PREAMBLE, or
EntityState.T_EPILOGUE.java.lang.IllegalStateException - getState() returns an
invalid value.public java.io.Reader getReader()
throws java.io.UnsupportedEncodingException
getInputStream().
Consult the javadoc for that method for known limitations.Reader, not nulljava.lang.IllegalStateException - getState() returns an
invalid valuejava.io.UnsupportedEncodingException - if there is no JVM support
for decoding the charsetgetInputStream()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.java.lang.IllegalStateException - getState() returns another
value than EntityState.T_FIELD.public EntityState next() throws java.io.IOException, MimeException
java.lang.IllegalStateException - The method has been called, although
getState() was already EntityState.T_END_OF_STREAM.java.io.IOExceptionMimeExceptionpublic static java.lang.String stateToString(EntityState state)
state - public MimeConfig getConfig()