protected void parse()

in geronimo-mail_2.1_spec/src/main/java/jakarta/mail/internet/MimeMultipart.java [265:335]


    protected void parse() throws MessagingException {
        if (parsed) {
            return;
        }
                
        initializeProperties();

        try {
            final ContentType cType = new ContentType(contentType);
            final String boundaryString = cType.getParameter("boundary");
            
            if(!ignoreMissingBoundaryParameter && boundaryString  == null) {
                throw new MessagingException("Missing boundary parameter in content-type");
            }           
                        
            final InputStream is = new BufferedInputStream(ds.getInputStream());
            BufferedInputStream pushbackInStream = null;
            boolean boundaryFound = false;
            
            byte[] boundary = null;
            if (boundaryString == null || ignoreExistingBoundaryParameter) {
                pushbackInStream = new BufferedInputStream(is, 1200);
                // read until we find something that looks like a boundary string
                boundary = readTillFirstBoundary(pushbackInStream);
                boundaryFound = boundary != null;
            }
            else {
                boundary = ("--" + boundaryString).getBytes("ISO8859-1");
                pushbackInStream = new BufferedInputStream(is, boundary.length + 1000);
                boundaryFound = readTillFirstBoundary(pushbackInStream, boundary);
            }
            
            
            
            if(allowEmpty && !boundaryFound) {
            	parsed = true;
                return;
            }
            
            if(!allowEmpty && !boundaryFound) {
                throw new MessagingException("Multipart content with no body parts is not allowed");
            }
            

            while (true) {
                MimeBodyPartInputStream partStream;
                partStream = new MimeBodyPartInputStream(pushbackInStream, boundary);
                addBodyPart(new MimeBodyPart(partStream));

                // terminated by an EOF rather than a proper boundary?
                if (!partStream.boundaryFound) {
                	
                    if (!ignoreMissingEndBoundary) {
                        throw new MessagingException("Missing Multi-part end boundary");
                    }
                    complete = false;
                    break;
                }
                // if we hit the final boundary, stop processing this
                if (partStream.finalBoundaryFound) {
                    break;
                }
            }
            
           
            
        } catch (final Exception e){
            throw new MessagingException(e.toString(),e);
        }
        parsed = true;
    }