private byte[] read(final DataSource dataSource) throws IOException { final ByteArrayOutputStream os = new ByteArrayOutputStream(); StreamUtil.copy(dataSource.getInputStream(), os); return os.toByteArray(); } }
/** * Creates {@link EmailAttachment}. * * @return {@link EmailAttachment}. * @throws MailException if issue with {@link DataSource}. */ public EmailAttachment<ByteArrayDataSource> buildByteArrayDataSource() throws MailException { try { final ByteArrayDataSource bads; if (dataSource instanceof ByteArrayDataSource) { bads = (ByteArrayDataSource) dataSource; } else { bads = new ByteArrayDataSource(dataSource.getInputStream(), dataSource.getContentType()); } checkDataSource(); return new EmailAttachment<>(name, contentId, isInline, bads).setEmbeddedMessage(targetMessage); } catch (final IOException ioexc) { throw new MailException(ioexc); } }
/** * 构建邮件信息主体 * * @param charset 编码 * @return 邮件信息主体 * @throws MessagingException 消息异常 */ private Multipart buildContent(Charset charset) throws MessagingException { final Multipart mainPart = new MimeMultipart(); // 正文 final BodyPart body = new MimeBodyPart(); body.setContent(content, StrUtil.format("text/{}; charset={}", isHtml ? "html" : "plain", charset)); mainPart.addBodyPart(body); // 附件 if (ArrayUtil.isNotEmpty(this.attachments)) { BodyPart bodyPart; for (DataSource attachment : attachments) { bodyPart = new MimeBodyPart(); bodyPart.setDataHandler(new DataHandler(attachment)); bodyPart.setFileName(InternalMailUtil.encodeText(attachment.getName(), charset)); mainPart.addBodyPart(bodyPart); } } return mainPart; }
FlowFile split = session.create(originalFlowFile); final Map<String, String> attributes = new HashMap<>(); if (StringUtils.isNotBlank(data.getName())) { attributes.put(CoreAttributes.FILENAME.key(), data.getName()); if (StringUtils.isNotBlank(data.getContentType())) { attributes.put(CoreAttributes.MIME_TYPE.key(), data.getContentType());
return; initializeProperties(); in = ds.getInputStream(); if (!(in instanceof ByteArrayInputStream) && !(in instanceof BufferedInputStream) && if (line.length() > 4 && allDashes(line)) { headers = createInternetHeaders(in); if (!in.markSupported()) throw new MessagingException("Stream doesn't support mark"); buf = new ByteArrayOutputStream(); else end = sin.getPosition(); in.mark(bl + 4 + 1000); // bnd + "--\r\n" + lots of LWSP eolLen = 0; inSize = readFully(in, inbuf, 0, bl); int b2 = in.read(); if (b2 == '-') { if (in.read() == '-') { buf.write(previnbuf, 0, prevSize);
public void echoData(Holder<String> text, Holder<DataHandler> data) { try { InputStream bis = null; bis = data.value.getDataSource().getInputStream(); byte[] b = new byte[6]; bis.read(b, 0, 6); String string = IOUtils.newStringFromBytes(b); ByteArrayDataSource source = new ByteArrayDataSource(("test" + string).getBytes(), "application/octet-stream"); data.value = new DataHandler(source); } catch (IOException e) { e.printStackTrace(); } }
@Override public byte[] getAttachmentAsByteArray(String contentId) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { InputStream is = AttachmentUtil.getAttachmentDataSource(contentId, attachments) .getInputStream(); IOUtils.copy(is, bos); is.close(); bos.close(); } catch (IOException e) { throw new Fault(new org.apache.cxf.common.i18n.Message("ATTACHMENT_READ_ERROR", LOG), e); } return bos.toByteArray(); }
/** * @param ds * @throws IOException */ public ByteDataSource ( DataSource ds ) throws IOException { try ( InputStream is = ds.getInputStream() ) { int read; ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte buffer[] = new byte[4096]; while ( ( read = is.read(buffer) ) > 0 ) { bos.write(buffer, 0, read); } this.data = bos.toByteArray(); } }
int bytes_read; is = dataSource.getInputStream(); while ((bytes_read = is.read(data)) > 0) { os.write(data, 0, bytes_read); is.close(); is = null; DataContentHandler dch = getDataContentHandler(); dch.writeTo(object, objectMimeType, os);
private static DataSource copyDataSource(DataSource source) throws MessagingException { try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); FileUtil.copyInputStream(source.getInputStream(), baos); ByteArrayDataSource bads = new ByteArrayDataSource(baos.toByteArray(), source.getContentType()); bads.setName(source.getName()); return bads; } catch (IOException e) { throw new MessagingException(e); } }
protected DataSource post(DataSource req) { String msg; try { LOG.info("content type: " + req.getContentType()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(req.getInputStream(), baos); LOG.info("body [" + new String(baos.toByteArray()) + "]"); try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) { msg = "<ok/>"; MimeMultipart multipart = DataSourceProviderTest.readAttachmentParts(req.getContentType(), bais); LOG.info("found " + multipart.getCount() + " parts"); } return new ByteArrayDataSource(baos.toByteArray(), req.getContentType()); } catch (Exception e) { e.printStackTrace(); msg = "<fail/>"; } return new ByteArrayDataSource(msg.getBytes(), "text/xml"); }
cType = new ContentType(ds.getContentType()); is = ds.getInputStream(); is.close(); } catch (IOException ioex) { }
@Override public void writeTo( final DataSource t, final Class<?> type, final Type genericType, final Annotation[] annotations, final MediaType mediaType, final MultivaluedMap<String, Object> httpHeaders, final OutputStream entityStream) throws IOException { final InputStream in = t.getInputStream(); try { writeTo(in, entityStream); } finally { in.close(); } } }
/** * Gets the raw data. The size of the byte array maybe larger than the actual length. */ public byte[] get() { if (data == null) { try { ByteArrayOutputStreamEx baos = new ByteArrayOutputStreamEx(1024); InputStream is = dataHandler.getDataSource().getInputStream(); baos.readFrom(is); is.close(); data = baos.getBuffer(); dataLen = baos.size(); } catch (IOException e) { // TODO: report the error to the unmarshaller dataLen = 0; // recover by assuming length-0 data } } return data; }
public Object getContent(DataSource ds) throws IOException { InputStream is = ds.getInputStream(); int pos = 0; int count; byte buf[] = new byte[1024]; while ((count = is.read(buf, pos, buf.length - pos)) != -1) { pos += count; if (pos >= buf.length) { int size = buf.length; if (size < 256*1024) size += size; else size += 256*1024; byte tbuf[] = new byte[size]; System.arraycopy(buf, 0, tbuf, 0, pos); buf = tbuf; } } Toolkit tk = Toolkit.getDefaultToolkit(); return tk.createImage(buf, 0, pos); }
protected Object getData(ActivationDataFlavor aFlavor, DataSource ds) throws IOException { if (aFlavor.getRepresentationClass() == String.class) return super.getContent(ds); else if (aFlavor.getRepresentationClass() == StreamSource.class) return new StreamSource(ds.getInputStream()); else return null; // XXX - should never happen }
/** * Creates the attachment part. * * @param attachment the attachment * @return the mime body part * @throws MessagingException the messaging exception */ private static MimeBodyPart createAttachmentPart(DataSource attachment) throws MessagingException { MimeBodyPart attachmentPart = new MimeBodyPart(); attachmentPart.setDataHandler(new DataHandler(attachment)); attachmentPart.setDisposition(Part.ATTACHMENT); attachmentPart.setFileName(attachment.getName()); return attachmentPart; }
name = ds.getName(); bodyPart.setFileName(MimeUtility.encodeText(name)); bodyPart.setDescription(description); bodyPart.setDataHandler(new DataHandler(ds)); getContainer().addBodyPart(bodyPart);
String dsct = ds.getContentType(); if(dsct!=null && (contentType==null || contentType.equals(dsct))) { bd.set(new DataHandler(ds)); return bd;
assertEquals(2, multipart.getCount()); MimeBodyPart mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(0); final MimeMultipart mimeMultipart = (MimeMultipart) mimeBodyPart.getContent(); assertEquals(2, mimeMultipart.getCount()); MimeBodyPart bodyPart = (MimeBodyPart) mimeMultipart.getBodyPart(0); MimeBodyPart htmlMimeBodyPart = (MimeBodyPart) htmlMessage.getBodyPart(0); assertEquals("<html><body><h1>Hey!</h1><img src='cid:c.png'></body></html>", htmlMimeBodyPart.getContent()); assertTrue(htmlMimeBodyPart.getDataHandler().getContentType().contains(MimeTypes.MIME_TEXT_HTML)); htmlMimeBodyPart = (MimeBodyPart) htmlMessage.getBodyPart(1); DataSource dataSource = htmlMimeBodyPart.getDataHandler().getDataSource(); assertEquals(IMAGE_PNG, dataSource.getContentType()); assertArrayEquals(BYTES_1_7, read(dataSource)); mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(1); dataSource = mimeBodyPart.getDataHandler().getDataSource(); assertEquals(APPLICATION_ZIP, dataSource.getContentType()); assertArrayEquals(BYTES_11_15, read(dataSource));