Class MimeTokenStream
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.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final BodyDescriptorBuilder
private final MimeConfig
private EntityStateMachine
private final ArrayDeque
<EntityStateMachine> private final FieldBuilder
private final DecodeMonitor
private RecursionMode
private MimeEntity
private EntityState
-
Constructor Summary
ConstructorsConstructorDescriptionConstructs a standard (lax) stream.MimeTokenStream
(MimeConfig config) MimeTokenStream
(MimeConfig config, DecodeMonitor monitor, BodyDescriptorBuilder bodyDescBuilder) MimeTokenStream
(MimeConfig config, DecodeMonitor monitor, FieldBuilder fieldBuilder, BodyDescriptorBuilder bodyDescBuilder) MimeTokenStream
(MimeConfig config, BodyDescriptorBuilder bodyDescBuilder) -
Method Summary
Modifier and TypeMethodDescriptionprivate void
doParse
(InputStream stream, EntityState start) Gets a descriptor for the current entity.This method returns a transfer decoded stream based on the MIME fields with the standard defaults.getField()
This method is valid, ifgetState()
returnsEntityState.T_FIELD
.This method returns the raw entity, preamble, or epilogue contents.Gets a reader configured for the current body or body part.Gets the current recursion mode.getState()
Returns the current state.boolean
isRaw()
Determines if this parser is currently in raw mode.next()
This method advances the token stream to the next token.void
parse
(InputStream stream) Instructs theMimeTokenStream
to parse the given streams contents.parseHeadless
(InputStream stream, String contentType) Instructs theMimeTokenStream
to parse the given content with the content type.void
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.
-
Field Details
-
config
-
monitor
-
fieldBuilder
-
bodyDescBuilder
-
entities
-
state
-
currentStateMachine
-
recursionMode
-
rootentity
-
-
Constructor Details
-
MimeTokenStream
public MimeTokenStream()Constructs a standard (lax) stream. Optional validation events will be logged only. UseMimeConfig.Builder.setStrictParsing(boolean)
to turn on strict parsing mode and pass the config object toMimeTokenStream(MimeConfig)
to create a stream that strictly validates the input. -
MimeTokenStream
-
MimeTokenStream
-
MimeTokenStream
public MimeTokenStream(MimeConfig config, DecodeMonitor monitor, BodyDescriptorBuilder bodyDescBuilder) -
MimeTokenStream
public MimeTokenStream(MimeConfig config, DecodeMonitor monitor, FieldBuilder fieldBuilder, BodyDescriptorBuilder bodyDescBuilder)
-
-
Method Details
-
parse
Instructs theMimeTokenStream
to parse the given streams contents. If theMimeTokenStream
has already been in use, resets the streams internal state. -
parseHeadless
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.- Returns:
- a parsed Field representing the input contentType
-
doParse
-
isRaw
public boolean isRaw()Determines if this parser is currently in raw mode.- Returns:
true
if in raw mode,false
otherwise.- See Also:
-
getRecursionMode
Gets the current recursion mode. The recursion mode specifies the approach taken to parsing parts.RecursionMode.M_RAW
mode does not parse the part at all.RecursionMode.M_RECURSE
mode recursively parses each mail when anmessage/rfc822
part is encountered;RecursionMode.M_NO_RECURSE
does not. -
setRecursionMode
Sets the current recursion. The recursion mode specifies the approach taken to parsing parts.RecursionMode.M_RAW
mode does not parse the part at all.RecursionMode.M_RECURSE
mode recursively parses each mail when anmessage/rfc822
part is encountered;RecursionMode.M_NO_RECURSE
does not.- Parameters:
mode
-RecursionMode.M_RECURSE
,RecursionMode.M_RAW
orRecursionMode.M_NO_RECURSE
-
stop
public void stop()Finishes the parsing and stops reading lines. NOTE: No more lines will be parsed but the parser will still trigger 'end' events to match previously triggered 'start' events. -
getState
Returns the current state. -
getInputStream
This method returns the raw entity, preamble, or epilogue contents.
This method is valid, if
getState()
returns either ofEntityState.T_RAW_ENTITY
,EntityState.T_PREAMBLE
, orEntityState.T_EPILOGUE
.- Returns:
- Data stream, depending on the current state.
- Throws:
IllegalStateException
-getState()
returns an invalid value.
-
getDecodedInputStream
This method returns a transfer decoded stream based on the MIME fields with the standard defaults.
This method is valid, if
getState()
returns either ofEntityState.T_RAW_ENTITY
,EntityState.T_PREAMBLE
, orEntityState.T_EPILOGUE
.- Returns:
- Data stream, depending on the current state.
- Throws:
IllegalStateException
-getState()
returns an invalid value.
-
getReader
Gets a reader configured for the current body or body part. The reader will return a transfer and charset decoded stream of characters based on the MIME fields with the standard defaults. This is a conveniance method and relies ongetInputStream()
. Consult the javadoc for that method for known limitations.- Returns:
Reader
, not null- Throws:
IllegalStateException
-getState()
returns an invalid valueUnsupportedEncodingException
- if there is no JVM support for decoding the charset- See Also:
-
getBodyDescriptor
Gets a descriptor for the current entity. This method is valid if
getState()
returns:- Returns:
BodyDescriptor
, not nulls
-
getField
This method is valid, ifgetState()
returnsEntityState.T_FIELD
.- Returns:
- String with the fields raw contents.
- Throws:
IllegalStateException
-getState()
returns another value thanEntityState.T_FIELD
.
-
next
This method advances the token stream to the next token.- Throws:
IllegalStateException
- The method has been called, althoughgetState()
was alreadyEntityState.T_END_OF_STREAM
.IOException
MimeException
-
stateToString
Renders a state as a string suitable for logging.- Parameters:
state
-- Returns:
- rendered as string, not null
-
getConfig
-