From 702ac6cb82d45a8fca1190ab5a1e22eef63b09d8 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Fri, 16 Dec 2016 07:30:02 +0100 Subject: [PATCH] Added some null checks so the users of this library don't have to --- .../entity/valueobject/extended/Message.java | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/extended/Message.java b/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/extended/Message.java index 98c84bd..b738d42 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/extended/Message.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/extended/Message.java @@ -132,31 +132,44 @@ public class Message implements ExtendedEncoding.ExtendedType { } public Builder addParent(Plaintext parent) { - parents.add(parent.getInventoryVector()); + if (parent != null) { + InventoryVector iv = parent.getInventoryVector(); + if (iv == null) { + LOG.debug("Ignored parent without IV"); + } else { + parents.add(iv); + } + } return this; } public Builder addParent(InventoryVector iv) { - parents.add(iv); + if (iv != null) { + parents.add(iv); + } return this; } public Builder addFile(File file, Attachment.Disposition disposition) { - try { - files.add(new Attachment.Builder() - .name(file.getName()) - .disposition(disposition) - .type(URLConnection.guessContentTypeFromStream(new FileInputStream(file))) - .data(Files.readAllBytes(file.toPath())) - .build()); - } catch (IOException e) { - LOG.error(e.getMessage(), e); + if (file != null) { + try { + files.add(new Attachment.Builder() + .name(file.getName()) + .disposition(disposition) + .type(URLConnection.guessContentTypeFromStream(new FileInputStream(file))) + .data(Files.readAllBytes(file.toPath())) + .build()); + } catch (IOException e) { + LOG.error(e.getMessage(), e); + } } return this; } public Builder addFile(Attachment file) { - files.add(file); + if (file != null) { + files.add(file); + } return this; }