Java Code Examples for javax.activation.DataSource#getInputStream()

The following examples show how to use javax.activation.DataSource#getInputStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AttachmentImpl.java    From tomee with Apache License 2.0 6 votes vote down vote up
public String stringFromDataSource(DataSource source) {

        try {
            InputStream inStr = source.getInputStream();
            int size = inStr.available();
            byte[] data = new byte[size];
            inStr.read(data);
            inStr.close();
            return new String(data);

        } catch (IOException e) {
            e.printStackTrace();

        }
        return "";

    }
 
Example 2
Source File: XmlDataContentHandler.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create an object from the input stream
 */
public Object getContent(DataSource ds) throws IOException {
    String ctStr = ds.getContentType();
    String charset = null;
    if (ctStr != null) {
        ContentType ct = new ContentType(ctStr);
        if (!isXml(ct)) {
            throw new IOException(
                "Cannot convert DataSource with content type \""
                        + ctStr + "\" to object in XmlDataContentHandler");
        }
        charset = ct.getParameter("charset");
    }
    return (charset != null)
            ? new StreamSource(new InputStreamReader(ds.getInputStream()), charset)
            : new StreamSource(ds.getInputStream());
}
 
Example 3
Source File: DispositionDataContentHandler.java    From OpenAs2App with BSD 2-Clause "Simplified" License 6 votes vote down vote up
public Object getContent(DataSource ds) throws IOException {
    byte[] buf = new byte[4096];
    BufferedInputStream bIn = new BufferedInputStream(ds.getInputStream());
    ByteArrayOutputStream baOut = new ByteArrayOutputStream();
    BufferedOutputStream bOut = new BufferedOutputStream(baOut);
    int count = bIn.read(buf);

    while (count > -1) {
        bOut.write(buf, 0, count);
        count = bIn.read(buf);
    }

    bIn.close();
    bOut.close();

    return baOut.toByteArray();
}
 
Example 4
Source File: XMLHTTPBindingCodec.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private ContentType encode(MessageDataSource mds, OutputStream out) {
    try {
        final boolean isFastInfoset = XMLMessage.isFastInfoset(
                mds.getDataSource().getContentType());
        DataSource ds = transformDataSource(mds.getDataSource(),
                isFastInfoset, useFastInfosetForEncoding, features);

        InputStream is = ds.getInputStream();
        byte[] buf = new byte[1024];
        int count;
        while((count=is.read(buf)) != -1) {
            out.write(buf, 0, count);
        }
        return new ContentTypeImpl(ds.getContentType());
    } catch(IOException ioe) {
        throw new WebServiceException(ioe);
    }
}
 
Example 5
Source File: XMLHTTPBindingCodec.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
private ContentType encode(MessageDataSource mds, OutputStream out) {
    try {
        final boolean isFastInfoset = XMLMessage.isFastInfoset(
                mds.getDataSource().getContentType());
        DataSource ds = transformDataSource(mds.getDataSource(),
                isFastInfoset, useFastInfosetForEncoding, features);

        InputStream is = ds.getInputStream();
        byte[] buf = new byte[1024];
        int count;
        while((count=is.read(buf)) != -1) {
            out.write(buf, 0, count);
        }
        return new ContentTypeImpl(ds.getContentType());
    } catch(IOException ioe) {
        throw new WebServiceException(ioe);
    }
}
 
Example 6
Source File: XMLHTTPBindingCodec.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private ContentType encode(MessageDataSource mds, OutputStream out) {
    try {
        final boolean isFastInfoset = XMLMessage.isFastInfoset(
                mds.getDataSource().getContentType());
        DataSource ds = transformDataSource(mds.getDataSource(),
                isFastInfoset, useFastInfosetForEncoding, features);

        InputStream is = ds.getInputStream();
        byte[] buf = new byte[1024];
        int count;
        while((count=is.read(buf)) != -1) {
            out.write(buf, 0, count);
        }
        return new ContentTypeImpl(ds.getContentType());
    } catch(IOException ioe) {
        throw new WebServiceException(ioe);
    }
}
 
Example 7
Source File: XMLHTTPBindingCodec.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private ContentType encode(MessageDataSource mds, OutputStream out) {
    try {
        final boolean isFastInfoset = XMLMessage.isFastInfoset(
                mds.getDataSource().getContentType());
        DataSource ds = transformDataSource(mds.getDataSource(),
                isFastInfoset, useFastInfosetForEncoding, features);

        InputStream is = ds.getInputStream();
        byte[] buf = new byte[1024];
        int count;
        while((count=is.read(buf)) != -1) {
            out.write(buf, 0, count);
        }
        return new ContentTypeImpl(ds.getContentType());
    } catch(IOException ioe) {
        throw new WebServiceException(ioe);
    }
}
 
Example 8
Source File: XmlDataContentHandler.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create an object from the input stream
 */
public Object getContent(DataSource ds) throws IOException {
    String ctStr = ds.getContentType();
    String charset = null;
    if (ctStr != null) {
        ContentType ct = new ContentType(ctStr);
        if (!isXml(ct)) {
            throw new IOException(
                "Cannot convert DataSource with content type \""
                        + ctStr + "\" to object in XmlDataContentHandler");
        }
        charset = ct.getParameter("charset");
    }
    return (charset != null)
            ? new StreamSource(new InputStreamReader(ds.getInputStream()), charset)
            : new StreamSource(ds.getInputStream());
}
 
Example 9
Source File: QueueOutputFileServlet.java    From unitime with Apache License 2.0 6 votes vote down vote up
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	String q = request.getParameter("q");
	if (q == null) {
		response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Q parameter not provided.");
		return;
	}
	DataSource ds = getQueueProcessor().getFile(QueryEncoderBackend.decode(q, false));
	if (ds != null) {
		response.setContentType(ds.getContentType());
		response.setHeader( "Content-Disposition", "attachment; filename=\"" + ds.getName() + "\"" );
		OutputStream out = response.getOutputStream();
		InputStream in = ds.getInputStream();
		try {
			IOUtils.copy(ds.getInputStream(), out);
			out.flush();
		} finally {
			in.close();	
			out.close();
		}
	} else {
		response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Output file is not available.");
	}
}
 
Example 10
Source File: XmlDataContentHandler.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Create an object from the input stream
 */
public Object getContent(DataSource ds) throws IOException {
    String ctStr = ds.getContentType();
    String charset = null;
    if (ctStr != null) {
        ContentType ct = new ContentType(ctStr);
        if (!isXml(ct)) {
            throw new IOException(
                "Cannot convert DataSource with content type \""
                        + ctStr + "\" to object in XmlDataContentHandler");
        }
        charset = ct.getParameter("charset");
    }
    return (charset != null)
            ? new StreamSource(new InputStreamReader(ds.getInputStream()), charset)
            : new StreamSource(ds.getInputStream());
}
 
Example 11
Source File: MailAccountServiceImpl.java    From axelor-open-suite with GNU Affero General Public License v3.0 5 votes vote down vote up
private void addAttachments(Message message, List<DataSource> attachments) {

    if (attachments == null) {
      return;
    }

    for (DataSource source : attachments) {
      try {
        InputStream stream = source.getInputStream();
        metaFiles.attach(stream, source.getName(), message);
      } catch (IOException e) {
        TraceBackService.trace(e);
      }
    }
  }
 
Example 12
Source File: AttachmentDeserializerTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testCXF3383() throws Exception {
    String contentType = "multipart/related; type=\"application/xop+xml\";"
        + " boundary=\"uuid:7a555f51-c9bb-4bd4-9929-706899e2f793\"; start="
        + "\"<[email protected]>\"; start-info=\"text/xml\"";

    Message message = new MessageImpl();
    message.put(Message.CONTENT_TYPE, contentType);
    message.setContent(InputStream.class, getClass().getResourceAsStream("cxf3383.data"));
    message.put(AttachmentDeserializer.ATTACHMENT_DIRECTORY, System
            .getProperty("java.io.tmpdir"));
    message.put(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD, String
            .valueOf(AttachmentDeserializer.THRESHOLD));


    AttachmentDeserializer ad
        = new AttachmentDeserializer(message,
                                     Collections.singletonList("multipart/related"));

    ad.initializeAttachments();


    for (int x = 1; x < 50; x++) {
        String cid = "1882f79d-e20a-4b36-a222-7a75518cf395-" + x + "@cxf.apache.org";
        DataSource ds = AttachmentUtil.getAttachmentDataSource(cid, message.getAttachments());
        byte[] bts = new byte[1024];

        InputStream ins = ds.getInputStream();
        int count = 0;
        int sz = ins.read(bts, 0, bts.length);
        while (sz != -1) {
            count += sz;
            // We do not expect the data to fill up the buffer:
            assertTrue(count < bts.length);
            sz = ins.read(bts, count, bts.length - count);
        }
        assertEquals(x + 1, count);
        ins.close();
    }
}
 
Example 13
Source File: MimeUtility.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the content-transfer-encoding that should be applied
 * to the input stream of this datasource, to make it mailsafe. <p>
 *
 * The algorithm used here is: <br>
 * <ul>
 * <li>
 * If the primary type of this datasource is "text" and if all
 * the bytes in its input stream are US-ASCII, then the encoding
 * is "7bit". If more than half of the bytes are non-US-ASCII, then
 * the encoding is "base64". If less than half of the bytes are
 * non-US-ASCII, then the encoding is "quoted-printable".
 * <li>
 * If the primary type of this datasource is not "text", then if
 * all the bytes of its input stream are US-ASCII, the encoding
 * is "7bit". If there is even one non-US-ASCII character, the
 * encoding is "base64".
 * </ul>
 *
 * @param   ds      DataSource
 * @return          the encoding. This is either "7bit",
 *                  "quoted-printable" or "base64"
 */
public static String getEncoding(DataSource ds) {
    ContentType cType = null;
    InputStream is = null;
    String encoding = null;

    try {
        cType = new ContentType(ds.getContentType());
        is = ds.getInputStream();
    } catch (Exception ex) {
        return "base64"; // what else ?!
    }

    boolean isText = cType.match("text/*");
    // if not text, stop processing when we see non-ASCII
    int i = checkAscii(is, ALL, !isText);
    switch (i) {
    case ALL_ASCII:
        encoding = "7bit"; // all ascii
        break;
    case MOSTLY_ASCII:
        encoding = "quoted-printable"; // mostly ascii
        break;
    default:
        encoding = "base64"; // mostly binary
        break;
    }

    // Close the input stream
    try {
        is.close();
    } catch (IOException ioex) { }

    return encoding;
}
 
Example 14
Source File: ByteDataSource.java    From serianalyzer with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @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();
    }
}
 
Example 15
Source File: ByteArrayDataSource.java    From unitime with Apache License 2.0 5 votes vote down vote up
public ByteArrayDataSource(DataSource ds) throws IOException {
	iName = ds.getName();
	iContentType = ds.getContentType();
	InputStream is = ds.getInputStream();
	try {
		iData = IOUtils.toByteArray(is);
	} finally {
		 is.close();
	}			
}
 
Example 16
Source File: MimeUtility.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the content-transfer-encoding that should be applied
 * to the input stream of this datasource, to make it mailsafe. <p>
 *
 * The algorithm used here is: <br>
 * <ul>
 * <li>
 * If the primary type of this datasource is "text" and if all
 * the bytes in its input stream are US-ASCII, then the encoding
 * is "7bit". If more than half of the bytes are non-US-ASCII, then
 * the encoding is "base64". If less than half of the bytes are
 * non-US-ASCII, then the encoding is "quoted-printable".
 * <li>
 * If the primary type of this datasource is not "text", then if
 * all the bytes of its input stream are US-ASCII, the encoding
 * is "7bit". If there is even one non-US-ASCII character, the
 * encoding is "base64".
 * </ul>
 *
 * @param   ds      DataSource
 * @return          the encoding. This is either "7bit",
 *                  "quoted-printable" or "base64"
 */
public static String getEncoding(DataSource ds) {
    ContentType cType = null;
    InputStream is = null;
    String encoding = null;

    try {
        cType = new ContentType(ds.getContentType());
        is = ds.getInputStream();
    } catch (Exception ex) {
        return "base64"; // what else ?!
    }

    boolean isText = cType.match("text/*");
    // if not text, stop processing when we see non-ASCII
    int i = checkAscii(is, ALL, !isText);
    switch (i) {
    case ALL_ASCII:
        encoding = "7bit"; // all ascii
        break;
    case MOSTLY_ASCII:
        encoding = "quoted-printable"; // mostly ascii
        break;
    default:
        encoding = "base64"; // mostly binary
        break;
    }

    // Close the input stream
    try {
        is.close();
    } catch (IOException ioex) { }

    return encoding;
}
 
Example 17
Source File: MultiPartEmail.java    From commons-email with Apache License 2.0 5 votes vote down vote up
/**
 * Attach a file specified as a DataSource interface.
 *
 * @param ds A DataSource interface for the file.
 * @param name The name field for the attachment.
 * @param description A description for the attachment.
 * @return A MultiPartEmail.
 * @throws EmailException see javax.mail.internet.MimeBodyPart
 *  for definitions
 * @since 1.0
 */
public MultiPartEmail attach(
    final DataSource ds,
    final String name,
    final String description)
    throws EmailException
{
    // verify that the DataSource is valid
    try
    {
        final InputStream is = ds != null ? ds.getInputStream() : null;
        if (is != null)
        {
            // close the input stream to prevent file locking on windows
            is.close();
        }

        if (is == null)
        {
            throw new EmailException("Invalid Datasource");
        }
    }
    catch (final IOException e)
    {
        throw new EmailException("Invalid Datasource", e);
    }

    return attach(ds, name, description, EmailAttachment.ATTACHMENT);
}
 
Example 18
Source File: DataSourceEntityProvider.java    From everrest with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void writeTo(DataSource dataSource,
                    Class<?> type,
                    Type genericType,
                    Annotation[] annotations,
                    MediaType mediaType,
                    MultivaluedMap<String, Object> httpHeaders,
                    OutputStream entityStream) throws IOException {
    if (httpHeaders.getFirst(CONTENT_TYPE) == null && !isNullOrEmpty(dataSource.getContentType())) {
        httpHeaders.putSingle(CONTENT_TYPE, dataSource.getContentType());
    }
    try (InputStream in = dataSource.getInputStream()) {
        ByteStreams.copy(in, entityStream);
    }
}
 
Example 19
Source File: AbstractDataSourceResolverTest.java    From commons-email with Apache License 2.0 5 votes vote down vote up
protected byte[] toByteArray(final DataSource dataSource) throws IOException
{
    if(dataSource != null)
    {
        final InputStream is = dataSource.getInputStream();
        try {
            return IOUtils.toByteArray(is);
        } finally {
            is.close();
        }
    }
    return null;
}
 
Example 20
Source File: AttachmentDeserializerTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testCXF3582() throws Exception {
    String contentType = "multipart/related; type=\"application/xop+xml\"; "
        + "boundary=\"uuid:906fa67b-85f9-4ef5-8e3d-52416022d463\"; "
        + "start=\"<[email protected]>\"; start-info=\"text/xml\"";


    Message message = new MessageImpl();
    message.put(Message.CONTENT_TYPE, contentType);
    message.setContent(InputStream.class, getClass().getResourceAsStream("cxf3582.data"));
    message.put(AttachmentDeserializer.ATTACHMENT_DIRECTORY, System
            .getProperty("java.io.tmpdir"));
    message.put(AttachmentDeserializer.ATTACHMENT_MEMORY_THRESHOLD, String
            .valueOf(AttachmentDeserializer.THRESHOLD));


    AttachmentDeserializer ad
        = new AttachmentDeserializer(message,
                                     Collections.singletonList("multipart/related"));

    ad.initializeAttachments();

    String cid = "[email protected]";
    DataSource ds = AttachmentUtil.getAttachmentDataSource(cid, message.getAttachments());
    byte[] bts = new byte[1024];
    InputStream ins = ds.getInputStream();
    int count = ins.read(bts, 0, bts.length);
    assertEquals(500, count);
    assertEquals(-1, ins.read(new byte[1000], 500, 500));

    cid = "[email protected]";
    ds = AttachmentUtil.getAttachmentDataSource(cid, message.getAttachments());
    bts = new byte[1024];
    ins = ds.getInputStream();
    count = ins.read(bts, 0, bts.length);
    assertEquals(1024, count);
    assertEquals(225, ins.read(new byte[1000], 500, 500));
    assertEquals(-1, ins.read(new byte[1000], 500, 500));

    ins.close();
}