Skip to content

Commit a913719

Browse files
committed
Fix #37 - Protect against NPEs when trying to convert attachments.
1 parent 64a048c commit a913719

File tree

1 file changed

+37
-14
lines changed

1 file changed

+37
-14
lines changed

src/main/java/pt/cjmach/pstconv/PstConverter.java

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ public PstConvertResult convert(PSTFile pstFile, File outputDirectory, MailMessa
256256
* @throws IOException
257257
* @throws MessagingException
258258
*/
259-
int convert(PSTFolder pstFolder, Folder mailFolder, String path, Charset charset) throws PSTException, IOException, MessagingException {
260-
int messageCount = 0;
259+
long convert(PSTFolder pstFolder, Folder mailFolder, String path, Charset charset) throws PSTException, IOException, MessagingException {
260+
long messageCount = 0;
261261
if (pstFolder.getContentCount() > 0) {
262262
PSTObject child = pstFolder.getNextChild();
263263

@@ -312,7 +312,7 @@ int convert(PSTFolder pstFolder, Folder mailFolder, String path, Charset charset
312312
Folder mboxSubFolder = mailFolder.getFolder(folderName);
313313
if (!mboxSubFolder.exists()) {
314314
if (!mboxSubFolder.create(Folder.HOLDS_FOLDERS | Folder.HOLDS_MESSAGES)) {
315-
logger.warn("Failed to create mail sub folder {}", subPath);
315+
logger.warn("Failed to create mail sub folder {}.", subPath);
316316
continue;
317317
}
318318
}
@@ -442,19 +442,29 @@ void convertAttachments(PSTMessage message, MimeMultipart rootMultipart) throws
442442

443443
if (attachment != null) {
444444
byte[] data = getAttachmentBytes(attachment);
445+
if (data.length == 0) {
446+
logger.warn("Failed to extract bytes of attachment {} from message {}.",
447+
attachment.getDescriptorNodeId(), message.getDescriptorNodeId());
448+
// try to add the attachment, which may still be useful even without its contents.
449+
}
450+
445451
MimeBodyPart attachmentBodyPart = new MimeBodyPart();
452+
try {
453+
String mimeTag = getAttachmentMimeTag(attachment);
454+
DataSource source = new ByteArrayDataSource(data, mimeTag);
455+
attachmentBodyPart.setDataHandler(new DataHandler(source));
446456

447-
String mimeTag = getAttachmentMimeTag(attachment);
448-
DataSource source = new ByteArrayDataSource(data, mimeTag);
449-
attachmentBodyPart.setDataHandler(new DataHandler(source));
450-
451-
attachmentBodyPart.setContentID(attachment.getContentId());
457+
attachmentBodyPart.setContentID(attachment.getContentId());
452458

453-
String fileName = coalesce("attachment-" + attachment.getDescriptorNodeId(), // NOI18N
454-
attachment.getLongFilename(), attachment.getDisplayName(), attachment.getFilename());
455-
attachmentBodyPart.setFileName(fileName);
459+
String fileName = coalesce("attachment-" + attachment.getDescriptorNodeId(), // NOI18N
460+
attachment.getLongFilename(), attachment.getDisplayName(), attachment.getFilename());
461+
attachmentBodyPart.setFileName(fileName);
456462

457-
rootMultipart.addBodyPart(attachmentBodyPart);
463+
rootMultipart.addBodyPart(attachmentBodyPart);
464+
} catch (NullPointerException ex) {
465+
logger.warn("Failed to convert attachment {} from message {}.",
466+
attachment.getDescriptorNodeId(), message.getDescriptorNodeId());
467+
}
458468
}
459469
}
460470
}
@@ -481,7 +491,13 @@ static boolean isMimeTypeKnown(String mime) {
481491
* stream.
482492
*/
483493
static byte[] getAttachmentBytes(PSTAttachment attachment) throws PSTException, IOException {
484-
try (InputStream input = attachment.getFileInputStream()) {
494+
InputStream input;
495+
try {
496+
input = attachment.getFileInputStream();
497+
} catch (NullPointerException ex) {
498+
return new byte[0];
499+
}
500+
try {
485501
int nread;
486502
byte[] buffer = new byte[4096];
487503
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
@@ -492,11 +508,18 @@ static byte[] getAttachmentBytes(PSTAttachment attachment) throws PSTException,
492508
byte[] result = output.toByteArray();
493509
return result;
494510
}
511+
} finally {
512+
try {
513+
input.close();
514+
} catch (IOException ignore) { }
495515
}
496516
}
497517

498518
static String getAttachmentMimeTag(PSTAttachment attachment) {
499-
String mimeTag = attachment.getMimeTag();
519+
String mimeTag = null;
520+
try {
521+
mimeTag = attachment.getMimeTag();
522+
} catch (NullPointerException ignore) { }
500523
// mimeTag should contain a valid mime type, but sometimes it doesn't.
501524
// To prevent throwing exceptions when the MimeMessage is validated, the
502525
// mimeTag value is first checked with isMimeTypeKnown(). If it's not

0 commit comments

Comments
 (0)