Example usage for javax.mail.util SharedFileInputStream close

List of usage examples for javax.mail.util SharedFileInputStream close

Introduction

In this page you can find the example usage for javax.mail.util SharedFileInputStream close.

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:org.apache.james.mailbox.maildir.mail.model.MaildirMessage.java

/**
 * Parse message if needed//from   w  w w. j  a v  a  2 s .co m
 */
private synchronized void parseMessage() {
    if (parsed)
        return;
    SharedFileInputStream tmpMsgIn = null;
    try {
        tmpMsgIn = new SharedFileInputStream(messageName.getFile());

        bodyStartOctet = bodyStartOctet(tmpMsgIn);

        // Disable line length... This should be handled by the smtp server
        // component and not the parser itself
        // https://issues.apache.org/jira/browse/IMAP-122
        MimeConfig config = MimeConfig.custom().setMaxLineLen(-1).build();
        final MimeTokenStream parser = new MimeTokenStream(config, new DefaultBodyDescriptorBuilder());
        parser.setRecursionMode(RecursionMode.M_NO_RECURSE);
        parser.parse(tmpMsgIn.newStream(0, -1));

        EntityState next = parser.next();
        while (next != EntityState.T_BODY && next != EntityState.T_END_OF_STREAM
                && next != EntityState.T_START_MULTIPART) {
            next = parser.next();
        }
        final MaximalBodyDescriptor descriptor = (MaximalBodyDescriptor) parser.getBodyDescriptor();
        final String mediaType;
        final String mediaTypeFromHeader = descriptor.getMediaType();
        final String subType;
        if (mediaTypeFromHeader == null) {
            mediaType = "text";
            subType = "plain";
        } else {
            mediaType = mediaTypeFromHeader;
            subType = descriptor.getSubType();
        }
        propertyBuilder.setMediaType(mediaType);
        propertyBuilder.setSubType(subType);
        propertyBuilder.setContentID(descriptor.getContentId());
        propertyBuilder.setContentDescription(descriptor.getContentDescription());
        propertyBuilder.setContentLocation(descriptor.getContentLocation());
        propertyBuilder.setContentMD5(descriptor.getContentMD5Raw());
        propertyBuilder.setContentTransferEncoding(descriptor.getTransferEncoding());
        propertyBuilder.setContentLanguage(descriptor.getContentLanguage());
        propertyBuilder.setContentDispositionType(descriptor.getContentDispositionType());
        propertyBuilder.setContentDispositionParameters(descriptor.getContentDispositionParameters());
        propertyBuilder.setContentTypeParameters(descriptor.getContentTypeParameters());
        // Add missing types
        final String codeset = descriptor.getCharset();
        if (codeset == null) {
            if ("TEXT".equalsIgnoreCase(mediaType)) {
                propertyBuilder.setCharset("us-ascii");
            }
        } else {
            propertyBuilder.setCharset(codeset);
        }

        final String boundary = descriptor.getBoundary();
        if (boundary != null) {
            propertyBuilder.setBoundary(boundary);
        }
        if ("text".equalsIgnoreCase(mediaType)) {
            long lines = -1;
            final CountingInputStream bodyStream = new CountingInputStream(parser.getInputStream());
            try {
                bodyStream.readAll();
                lines = bodyStream.getLineCount();
            } finally {
                IOUtils.closeQuietly(bodyStream);
            }

            next = parser.next();
            if (next == EntityState.T_EPILOGUE) {
                final CountingInputStream epilogueStream = new CountingInputStream(parser.getInputStream());
                try {
                    epilogueStream.readAll();
                    lines += epilogueStream.getLineCount();
                } finally {
                    IOUtils.closeQuietly(epilogueStream);
                }
            }
            propertyBuilder.setTextualLineCount(lines);
        }
    } catch (IOException e) {
        // has successfully been parsen when appending, shouldn't give any
        // problems
    } catch (MimeException e) {
        // has successfully been parsen when appending, shouldn't give any
        // problems
    } finally {
        if (tmpMsgIn != null) {
            try {
                tmpMsgIn.close();
            } catch (IOException e) {
                // ignore on close
            }
        }
        parsed = true;
    }
}