Added some null checks so the users of this library don't have to

This commit is contained in:
Christian Basler 2016-12-16 07:30:02 +01:00
parent 0d67701735
commit 702ac6cb82

View File

@ -132,31 +132,44 @@ public class Message implements ExtendedEncoding.ExtendedType {
} }
public Builder addParent(Plaintext parent) { 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; return this;
} }
public Builder addParent(InventoryVector iv) { public Builder addParent(InventoryVector iv) {
parents.add(iv); if (iv != null) {
parents.add(iv);
}
return this; return this;
} }
public Builder addFile(File file, Attachment.Disposition disposition) { public Builder addFile(File file, Attachment.Disposition disposition) {
try { if (file != null) {
files.add(new Attachment.Builder() try {
.name(file.getName()) files.add(new Attachment.Builder()
.disposition(disposition) .name(file.getName())
.type(URLConnection.guessContentTypeFromStream(new FileInputStream(file))) .disposition(disposition)
.data(Files.readAllBytes(file.toPath())) .type(URLConnection.guessContentTypeFromStream(new FileInputStream(file)))
.build()); .data(Files.readAllBytes(file.toPath()))
} catch (IOException e) { .build());
LOG.error(e.getMessage(), e); } catch (IOException e) {
LOG.error(e.getMessage(), e);
}
} }
return this; return this;
} }
public Builder addFile(Attachment file) { public Builder addFile(Attachment file) {
files.add(file); if (file != null) {
files.add(file);
}
return this; return this;
} }