/** Return the content. */ public Object getContent(DataSource ds) throws IOException { // create a new DeliveryStatus try { /* Session session; if (ds instanceof MessageAware) { javax.mail.MessageContext mc = ((MessageAware)ds).getMessageContext(); session = mc.getSession(); } else { // Hopefully a rare case. Also hopefully the application // has created a default Session that can just be returned // here. If not, the one we create here is better than // nothing, but overall not a really good answer. session = Session.getDefaultInstance(new Properties(), null); } return new DeliveryStatus(session, ds.getInputStream()); */ return new DeliveryStatus(ds.getInputStream()); } catch (MessagingException me) { throw new IOException( "Exception creating DeliveryStatus in " + "message/delivery-status DataContentHandler: " + me.toString()); } }
/** * Return the Transfer Data of type DataFlavor from InputStream. * * @param df the DataFlavor * @param ds the DataSource * @return the constructed Object */ public Object getTransferData(DataFlavor df, DataSource ds) throws UnsupportedFlavorException, IOException { if (dch != null) return dch.getTransferData(df, ds); else if (df.equals(getTransferDataFlavors()[0])) // only have one now return ds.getInputStream(); else throw new UnsupportedFlavorException(df); }
/** * Get the InputStream for this object. * * <p>For DataHandlers instantiated with a DataSource, the DataHandler calls the <code> * DataSource.getInputStream</code> method and returns the result to the caller. * * <p>For DataHandlers instantiated with an Object, the DataHandler first attempts to find a * DataContentHandler for the Object. If the DataHandler can not find a DataContentHandler for * this MIME type, it throws an UnsupportedDataTypeException. If it is successful, it creates a * pipe and a thread. The thread uses the DataContentHandler's <code>writeTo</code> method to * write the stream data into one end of the pipe. The other end of the pipe is returned to the * caller. Because a thread is created to copy the data, IOExceptions that may occur during the * copy can not be propagated back to the caller. The result is an empty stream. * * <p> * * @return the InputStream representing this data * @exception IOException if an I/O error occurs * @see javax.activation.DataContentHandler#writeTo * @see javax.activation.UnsupportedDataTypeException */ public InputStream getInputStream() throws IOException { InputStream ins = null; if (dataSource != null) { ins = dataSource.getInputStream(); } else { DataContentHandler dch = getDataContentHandler(); // we won't even try if we can't get a dch if (dch == null) throw new UnsupportedDataTypeException("no DCH for MIME type " + getBaseType()); if (dch instanceof ObjectDataContentHandler) { if (((ObjectDataContentHandler) dch).getDCH() == null) throw new UnsupportedDataTypeException("no object DCH for MIME type " + getBaseType()); } // there is none but the default^^^^^^^^^^^^^^^^ final DataContentHandler fdch = dch; // from bill s. // ce n'est pas une pipe! // // NOTE: This block of code needs to throw exceptions, but // can't because it is in another thread!!! ARG! // final PipedOutputStream pos = new PipedOutputStream(); PipedInputStream pin = new PipedInputStream(pos); new Thread( new Runnable() { public void run() { try { fdch.writeTo(object, objectMimeType, pos); } catch (IOException e) { } finally { try { pos.close(); } catch (IOException ie) { } } } }, "DataHandler.getInputStream") .start(); ins = pin; } return ins; }
/** * Write the data to an <code>OutputStream</code>. * * <p>If the DataHandler was created with a DataSource, writeTo retrieves the InputStream and * copies the bytes from the InputStream to the OutputStream passed in. * * <p>If the DataHandler was created with an object, writeTo retrieves the DataContentHandler for * the object's type. If the DataContentHandler was found, it calls the <code>writeTo</code> * method on the <code>DataContentHandler</code>. * * @param os the OutputStream to write to * @exception IOException if an I/O error occurs */ public void writeTo(OutputStream os) throws IOException { // for the DataSource case if (dataSource != null) { InputStream is = null; byte data[] = new byte[8 * 1024]; int bytes_read; is = dataSource.getInputStream(); try { while ((bytes_read = is.read(data)) > 0) { os.write(data, 0, bytes_read); } } finally { is.close(); is = null; } } else { // for the Object case DataContentHandler dch = getDataContentHandler(); dch.writeTo(object, objectMimeType, os); } }
public Object getContent(DataSource ds) throws IOException { if (dch != null) return dch.getContent(ds); else return ds.getInputStream(); }