ePOST API

rice.email.proxy.util
Class MimeParser

java.lang.Object
  extended by rice.email.proxy.util.MimeParser

public class MimeParser
extends java.lang.Object

originally from http://www.nsftools.com/tips/MimeParser.java


Field Summary
protected  java.util.Stack boundaries
          The stack of all boundaries
protected  byte[] boundary
           
protected  byte[] buffer
          The internal buffer used to store the parsed data
static int BUFFER_SIZE
          The initial size of the buffer to use
protected  int bufferLength
           
static int END_DOCUMENT
           
static int END_MULTIPART
           
protected  int event
          The current event
protected  byte[] header
          The parsed header, part, and boundary data, if it exists
protected  java.io.InputStream in
          Where we are reading the data from
protected  byte[] part
           
static int SINGLE_PART
           
static int START_DOCUMENT
          static flags reporting the type of event just hit
static int START_HEADERS_PART
           
static int START_MULTIPART
           
 
Constructor Summary
MimeParser(java.io.InputStream inStream)
          The sole constructor for this class, which takes any kind of InputStream as a parameter.
 
Method Summary
protected  void appendToBuffer(byte b)
          Internal method which appends a byte to the internal buffer
protected  void assertEvent(int e)
          An assertion method
 java.lang.String getBoundary()
          Get the boundary that we're breaking the message up on
 java.lang.String getCurrentBoundary()
          Returns the current boundary
 int getEventType()
          Returns the type of the current event
 byte[] getHeader()
          Get the header of the current message part that we're looking at
 java.lang.String getHeaderValue(java.lang.String entry)
          Gets the specified value from a specified header, or null if the entry does not exist
 byte[] getPart()
          Gets the data contained in the current message part as a byte array (this will return an empty byte array if you've already got the data from this message part)
protected  boolean isBoundary()
          Method which returns whether or not we've actually hit a boundary
protected  boolean isEndBoundary()
          Method which returns whether or not we've actually hit a boundary
static void main(java.lang.String[] args)
          A simple main method, in case you want to test the basic functionality of this class by running it stand-alone.
 int next()
          Advances to the next part of the message, if there is a next part.
protected  void parseBoundary()
          Internal method for parsing a MIME boundary
protected  void parseHeader()
          A private method to get the next header block on the InputStream.
protected  void parsePart()
          Internal method which parses and stores the given part.
protected  java.lang.String retrieveBoundary()
          A private method to attempt to read the MIME boundary from the Content-Type entry in the first header it finds.
protected  boolean startsWith(byte[] array, java.lang.String s)
          Internal method for startsWith
protected  void storeBoundary()
          Internal method which grabs the MIME boundary from the current line in the buffer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

START_DOCUMENT

public static final int START_DOCUMENT
static flags reporting the type of event just hit

See Also:
Constant Field Values

START_HEADERS_PART

public static final int START_HEADERS_PART
See Also:
Constant Field Values

START_MULTIPART

public static final int START_MULTIPART
See Also:
Constant Field Values

END_MULTIPART

public static final int END_MULTIPART
See Also:
Constant Field Values

END_DOCUMENT

public static final int END_DOCUMENT
See Also:
Constant Field Values

SINGLE_PART

public static final int SINGLE_PART
See Also:
Constant Field Values

BUFFER_SIZE

public static final int BUFFER_SIZE
The initial size of the buffer to use

See Also:
Constant Field Values

in

protected java.io.InputStream in
Where we are reading the data from


event

protected int event
The current event


buffer

protected byte[] buffer
The internal buffer used to store the parsed data


bufferLength

protected int bufferLength

header

protected byte[] header
The parsed header, part, and boundary data, if it exists


part

protected byte[] part

boundary

protected byte[] boundary

boundaries

protected java.util.Stack boundaries
The stack of all boundaries

Constructor Detail

MimeParser

public MimeParser(java.io.InputStream inStream)
The sole constructor for this class, which takes any kind of InputStream as a parameter.

Parameters:
inStream - an InputStream that contains a Multi-part MIME message
Method Detail

getEventType

public int getEventType()
Returns the type of the current event

Returns:
The type of the current event

next

public int next()
         throws MimeException
Advances to the next part of the message, if there is a next part. When you create an instance of a MimeParser, you need to call nextPart() before you start getting data.

Returns:
true if there is a next part, false if there isn't (which generally means you're at the end of the message)
Throws:
MimeException

getCurrentBoundary

public java.lang.String getCurrentBoundary()
Returns the current boundary

Returns:
The current boundary

getBoundary

public java.lang.String getBoundary()
                             throws MimeException
Get the boundary that we're breaking the message up on

Returns:
a String containing the message boundary, or an empty String if the boundary isn't available
Throws:
MimeException

getHeader

public byte[] getHeader()
                 throws MimeException
Get the header of the current message part that we're looking at

Returns:
a String containing the current part's header, or an empty String if the header isn't available
Throws:
MimeException

getPart

public byte[] getPart()
               throws MimeException
Gets the data contained in the current message part as a byte array (this will return an empty byte array if you've already got the data from this message part)

Returns:
a byte array containing the data in this message part, or an empty byte array if you've already read this data
Throws:
MimeException

getHeaderValue

public java.lang.String getHeaderValue(java.lang.String entry)
                                throws MimeException
Gets the specified value from a specified header, or null if the entry does not exist

Parameters:
header - the header to look at
entry - the name of the entry you're looking for
Returns:
a String containing the value you're looking for, or null if the entry cannot be found
Throws:
MimeException

assertEvent

protected void assertEvent(int e)
                    throws MimeException
An assertion method

Parameters:
the - expected event
Throws:
MimeException

isBoundary

protected boolean isBoundary()
Method which returns whether or not we've actually hit a boundary


isEndBoundary

protected boolean isEndBoundary()
Method which returns whether or not we've actually hit a boundary


retrieveBoundary

protected java.lang.String retrieveBoundary()
                                     throws MimeException
A private method to attempt to read the MIME boundary from the Content-Type entry in the first header it finds. This should be called once, when the class is first instantiated.

Throws:
MimeException

parseBoundary

protected void parseBoundary()
                      throws MimeException
Internal method for parsing a MIME boundary

Throws:
MimeException

storeBoundary

protected void storeBoundary()
Internal method which grabs the MIME boundary from the current line in the buffer.


parseHeader

protected void parseHeader()
                    throws MimeException
A private method to get the next header block on the InputStream. For our purposes, a header is a block of text that ends with a blank line.

Throws:
MimeException

parsePart

protected void parsePart()
                  throws MimeException
Internal method which parses and stores the given part. NOTE: this method also parses the MIME boundary following the part and stores the result into the boundary field.

Throws:
MimeException

startsWith

protected boolean startsWith(byte[] array,
                             java.lang.String s)
Internal method for startsWith

Parameters:
byte[] - THe array
s - The stirng

appendToBuffer

protected void appendToBuffer(byte b)
Internal method which appends a byte to the internal buffer

Parameters:
b - The byte

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
A simple main method, in case you want to test the basic functionality of this class by running it stand-alone.

Throws:
java.lang.Exception

ePOST API

Copyright © 2001-2005 - Rice Pastry.