public BinaryBody buildBinary() throws IOException { Charset cs = this.charset != null ? this.charset : Charsets.DEFAULT_CHARSET; if (this.bodyFactory != null) { if (this.text != null) { return this.bodyFactory.binaryBody(InputStreams.create(this.text, cs)); } else if (this.bin != null) { return this.bodyFactory.binaryBody(InputStreams.create(this.bin)); } else { return this.bodyFactory.binaryBody((InputStreams.create(new byte[]{}))); } } else { if (this.bin != null) { return BasicBodyFactory.INSTANCE.binaryBody(this.bin); } else if (this.text != null) { return BasicBodyFactory.INSTANCE.binaryBody(this.text, cs); } else { return BasicBodyFactory.INSTANCE.binaryBody(new byte [] {}); } } }
/** * Sets binary content of this message with the given MIME type. * * @param body * the body. * @param mimeType * the MIME media type of the specified body * ("type/subtype"). */ public BodyPartBuilder setBody(byte[] bin, String mimeType) throws IOException { setField(Fields.contentType(mimeType != null ? mimeType : "application/octet-stream")); Body binBody; if (bodyFactory != null) { binBody = bodyFactory.binaryBody(InputStreams.create(bin)); } else { binBody = BasicBodyFactory.INSTANCE.binaryBody(bin); } return setBody(binBody); }
/** * Sets binary content of this message with the given MIME type. * * @param bin * the body. * @param mimeType * the MIME media type of the specified body * ("type/subtype"). */ public Builder setBody(byte[] bin, String mimeType) throws IOException { setField(Fields.contentType(mimeType != null ? mimeType : "application/octet-stream")); Body binBody; if (bodyFactory != null) { binBody = bodyFactory.binaryBody(InputStreams.create(bin)); } else { binBody = BasicBodyFactory.INSTANCE.binaryBody(bin); } return setBody(binBody); }
public MultipartBuilder addBinaryPart(byte[] bin, String mimeType) throws IOException { BinaryBody body = bodyFactory != null ? bodyFactory.binaryBody(InputStreams.create(bin)) : BasicBodyFactory.INSTANCE.binaryBody(bin); BodyPart bodyPart = BodyPartBuilder.create() .setBody(body) .setContentType(mimeType != null ? mimeType : "application/octet-stream") .setContentTransferEncoding("base64") .build(); return addBodyPart(bodyPart); }
private BodyPart attachmentBodyPart(MessageAttachment att) throws IOException { BodyPartBuilder builder = BodyPartBuilder.create() .use(bodyFactory) .setBody(new BasicBodyFactory().binaryBody(ByteStreams.toByteArray(att.getAttachment().getStream()))) .setField(contentTypeField(att)) .setField(contentDispositionField(att.isInline())) .setContentTransferEncoding(BASE64); contentId(builder, att); return builder.build(); }
@Test void extractShouldReturnEmptyWhenBinaryContentOnly() throws IOException { Message message = Message.Builder.of() .setBody(BasicBodyFactory.INSTANCE.binaryBody(BINARY_CONTENT, StandardCharsets.UTF_8)) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).isEmpty(); assertThat(actual.getHtmlBody()).isEmpty(); }
@Test public void extractShouldReturnEmptyWhenBinaryContentOnly() throws IOException { Message message = Message.Builder.of() .setBody(BasicBodyFactory.INSTANCE.binaryBody(BINARY_CONTENT, StandardCharsets.UTF_8)) .build(); MessageContent actual = testee.extract(message); assertThat(actual.getTextBody()).isEmpty(); assertThat(actual.getHtmlBody()).isEmpty(); }