package javax.activation; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.UnsupportedFlavorException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.net.URL; public class DataHandler implements Transferable { private DataSource dataSource = null; private DataSource objDataSource = null; private Object object = null; private String objectMimeType = null; private CommandMap currentCommandMap = null; private static final DataFlavor[] emptyFlavors = new DataFlavor[0]; private DataFlavor[] transferFlavors = emptyFlavors; private DataContentHandler dataContentHandler = null; private DataContentHandler factoryDCH = null; private static DataContentHandlerFactory factory = null; private DataContentHandlerFactory oldFactory = null; private String shortType = null; public DataHandler(DataSource paramDataSource) { this.dataSource = paramDataSource; this.oldFactory = factory; } public DataHandler(Object paramObject, String paramString) { this.object = paramObject; this.objectMimeType = paramString; this.oldFactory = factory; } public DataHandler(URL paramURL) { this.dataSource = new URLDataSource(paramURL); this.oldFactory = factory; } private synchronized CommandMap getCommandMap() { if (this.currentCommandMap != null) return this.currentCommandMap; return CommandMap.getDefaultCommandMap(); } public DataSource getDataSource() { if (this.dataSource == null) { if (this.objDataSource == null) this.objDataSource = new DataHandlerDataSource(this); return this.objDataSource; } return this.dataSource; } public String getName() { if (this.dataSource != null) return this.dataSource.getName(); return null; } public String getContentType() { if (this.dataSource != null) return this.dataSource.getContentType(); return this.objectMimeType; } public InputStream getInputStream() throws IOException { InputStream inputStream = null; if (this.dataSource != null) { inputStream = this.dataSource.getInputStream(); } else { DataContentHandler dataContentHandler1 = getDataContentHandler(); if (dataContentHandler1 == null) throw new UnsupportedDataTypeException("no DCH for MIME type " + getBaseType()); if (dataContentHandler1 instanceof ObjectDataContentHandler && ( (ObjectDataContentHandler)dataContentHandler1).getDCH() == null) throw new UnsupportedDataTypeException("no object DCH for MIME type " + getBaseType()); final DataContentHandler fdch = dataContentHandler1; final PipedOutputStream pos = new PipedOutputStream(); PipedInputStream pipedInputStream = new PipedInputStream(pos); new Thread(new Runnable() { public void run() { try { fdch.writeTo(DataHandler.this.object, DataHandler.this.objectMimeType, pos); } catch (IOException e) { } finally { try { pos.close(); } catch (IOException e) {} } } }, "DataHandler.getInputStream").start(); inputStream = pipedInputStream; } return inputStream; } public void writeTo(OutputStream paramOutputStream) throws IOException { if (this.dataSource != null) { InputStream inputStream = null; byte[] arrayOfByte = new byte[8192]; inputStream = this.dataSource.getInputStream(); try { int i; while ((i = inputStream.read(arrayOfByte)) > 0) paramOutputStream.write(arrayOfByte, 0, i); } finally { inputStream.close(); inputStream = null; } } else { DataContentHandler dataContentHandler = getDataContentHandler(); dataContentHandler.writeTo(this.object, this.objectMimeType, paramOutputStream); } } public OutputStream getOutputStream() throws IOException { if (this.dataSource != null) return this.dataSource.getOutputStream(); return null; } public synchronized DataFlavor[] getTransferDataFlavors() { if (factory != this.oldFactory) this.transferFlavors = emptyFlavors; if (this.transferFlavors == emptyFlavors) this.transferFlavors = getDataContentHandler().getTransferDataFlavors(); return this.transferFlavors; } public boolean isDataFlavorSupported(DataFlavor paramDataFlavor) { DataFlavor[] arrayOfDataFlavor = getTransferDataFlavors(); for (int i = 0; i < arrayOfDataFlavor.length; i++) { if (arrayOfDataFlavor[i].equals(paramDataFlavor)) return true; } return false; } public Object getTransferData(DataFlavor paramDataFlavor) throws UnsupportedFlavorException, IOException { return getDataContentHandler().getTransferData(paramDataFlavor, this.dataSource); } public synchronized void setCommandMap(CommandMap paramCommandMap) { if (paramCommandMap != this.currentCommandMap || paramCommandMap == null) { this.transferFlavors = emptyFlavors; this.dataContentHandler = null; this.currentCommandMap = paramCommandMap; } } public CommandInfo[] getPreferredCommands() { if (this.dataSource != null) return getCommandMap().getPreferredCommands(getBaseType(), this.dataSource); return getCommandMap().getPreferredCommands(getBaseType()); } public CommandInfo[] getAllCommands() { if (this.dataSource != null) return getCommandMap().getAllCommands(getBaseType(), this.dataSource); return getCommandMap().getAllCommands(getBaseType()); } public CommandInfo getCommand(String paramString) { if (this.dataSource != null) return getCommandMap().getCommand(getBaseType(), paramString, this.dataSource); return getCommandMap().getCommand(getBaseType(), paramString); } public Object getContent() throws IOException { if (this.object != null) return this.object; return getDataContentHandler().getContent(getDataSource()); } public Object getBean(CommandInfo paramCommandInfo) { Object object = null; try { ClassLoader classLoader = null; classLoader = SecuritySupport.getContextClassLoader(); if (classLoader == null) classLoader = getClass().getClassLoader(); object = paramCommandInfo.getCommandObject(this, classLoader); } catch (IOException e) { } catch (ClassNotFoundException e) {} return object; } private synchronized DataContentHandler getDataContentHandler() { if (factory != this.oldFactory) { this.oldFactory = factory; this.factoryDCH = null; this.dataContentHandler = null; this.transferFlavors = emptyFlavors; } if (this.dataContentHandler != null) return this.dataContentHandler; String str = getBaseType(); if (this.factoryDCH == null && factory != null) this.factoryDCH = factory.createDataContentHandler(str); if (this.factoryDCH != null) this.dataContentHandler = this.factoryDCH; if (this.dataContentHandler == null) if (this.dataSource != null) { this.dataContentHandler = getCommandMap().createDataContentHandler(str, this.dataSource); } else { this.dataContentHandler = getCommandMap().createDataContentHandler(str); } if (this.dataSource != null) { this.dataContentHandler = new DataSourceDataContentHandler(this.dataContentHandler, this.dataSource); } else { this.dataContentHandler = new ObjectDataContentHandler(this.dataContentHandler, this.object, this.objectMimeType); } return this.dataContentHandler; } private synchronized String getBaseType() { if (this.shortType == null) { String str = getContentType(); try { MimeType mimeType = new MimeType(str); this.shortType = mimeType.getBaseType(); } catch (MimeTypeParseException e) { this.shortType = str; } } return this.shortType; } public static synchronized void setDataContentHandlerFactory(DataContentHandlerFactory paramDataContentHandlerFactory) { if (factory != null) throw new Error("DataContentHandlerFactory already defined"); SecurityManager securityManager = System.getSecurityManager(); if (securityManager != null) try { securityManager.checkSetFactory(); } catch (SecurityException e) { if (DataHandler.class.getClassLoader() != paramDataContentHandlerFactory.getClass().getClassLoader()) throw e; } factory = paramDataContentHandlerFactory; } }