Example usage for javax.mail.internet MimePart getLineCount

List of usage examples for javax.mail.internet MimePart getLineCount

Introduction

In this page you can find the example usage for javax.mail.internet MimePart getLineCount.

Prototype

public int getLineCount() throws MessagingException;

Source Link

Document

Return the number of lines in the content of this part.

Usage

From source file:com.zimbra.cs.imap.ImapMessage.java

private static int getLineCount(MimePart mp) {
    // if the MimePart implementation counts lines, use its count
    try {/*w w w. ja  va2s .co  m*/
        int lines = mp.getLineCount();
        if (lines > 0) {
            return lines;
        }
    } catch (MessagingException e) {
    }

    InputStream is = null;
    try {
        if (mp instanceof MimeBodyPart) {
            is = ((MimeBodyPart) mp).getRawInputStream();
        } else if (mp instanceof MimeMessage) {
            is = ((MimeMessage) mp).getRawInputStream();
        } else {
            return 0;
        }

        int lines = 0, c;
        boolean complete = false;
        while ((c = is.read()) != -1) {
            if ((complete = (c == '\n')) == true) {
                lines++;
            }
        }
        return complete ? lines : lines + 1;
    } catch (MessagingException e) {
        return 0;
    } catch (IOException e) {
        return 0;
    } finally {
        ByteUtil.closeStream(is);
    }
}