Search in sources :

Example 1 with StartOutOfBoundsException

use of com.zimbra.common.util.StartOutOfBoundsException in project zm-mailbox by Zimbra.

the class ImapPartSpecifier method getContent.

Pair<Long, InputStream> getContent(Object obj) throws IOException, BinaryDecodingException, ServiceException {
    Pair<Long, InputStream> contents;
    if (obj instanceof MimeMessage) {
        contents = getContent((MimeMessage) obj);
    } else if (obj instanceof MailItem) {
        if (!isEntireMessage()) {
            throw ServiceException.FAILURE("called writeMessage on non-toplevel part", null);
        }
        contents = ImapMessage.getContent((MailItem) obj);
    } else {
        throw ServiceException.FAILURE("called write() with unexpected argument: " + (obj == null ? "null" : obj.getClass().getSimpleName()), null);
    }
    if (octetStart >= 0 && contents != null) {
        // if there is a "partial" octet start/length constraint on this
        // part specifier, apply it here
        InputStream is = contents.getSecond();
        long statedLength = contents.getFirst();
        long realLength = Math.max(0, Math.min(statedLength < 0 ? Integer.MAX_VALUE : statedLength, octetEnd) - octetStart);
        try {
            int start = octetStart;
            // don't do skip() correctly
            if (is instanceof com.sun.mail.util.BASE64DecoderStream || is instanceof com.sun.mail.util.QPDecoderStream) {
                ByteUtil.skip(is, start);
                start = 0;
            }
            is = ByteUtil.SegmentInputStream.create(is, start, start + realLength);
        } catch (StartOutOfBoundsException e) {
            //return empty string {0} when start is out of range
            ZimbraLog.imap.warn("IMAP part requested start out of range", e);
            is = new ByteArrayInputStream(new byte[0]);
            statedLength = realLength = 0;
        } catch (IOException ioe) {
            ByteUtil.closeStream(is);
            throw ioe;
        }
        contents = new Pair<Long, InputStream>(statedLength < 0 ? -1 : realLength, is);
    }
    return contents;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) MailItem(com.zimbra.cs.mailbox.MailItem) MimeMessage(javax.mail.internet.MimeMessage) ByteArrayInputStream(java.io.ByteArrayInputStream) StartOutOfBoundsException(com.zimbra.common.util.StartOutOfBoundsException)

Aggregations

StartOutOfBoundsException (com.zimbra.common.util.StartOutOfBoundsException)1 MailItem (com.zimbra.cs.mailbox.MailItem)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MimeMessage (javax.mail.internet.MimeMessage)1