public class DataHandler extends Object implements Transferable
DataHandler and the Transferable Interface
DataHandler implements the Transferable interface so that data can be used in AWT data transfer operations, such as cut and paste and drag and drop. The implementation of the Transferable interface relies on the availability of an installed DataContentHandler object corresponding to the MIME type of the data represented in the specific instance of the DataHandler.
DataHandler and CommandMaps
The DataHandler keeps track of the current CommandMap that it uses to
service requests for commands (
Each instance of a DataHandler may have a CommandMap associated with
it using the
setCommandMap method. If a CommandMap was
not set, DataHandler calls the
method in CommandMap and uses the value it returns. See
CommandMap for more information.
DataHandler and URLs
The current DataHandler implementation creates a private instance of URLDataSource when it is constructed with a URL.
|Constructor and Description|
|Modifier and Type||Method and Description|
Return all the commands for this type of data.
A convenience method that takes a CommandInfo object and instantiates the corresponding command, usually a JavaBean component.
Get the command cmdName.
Return the data in its preferred Object form.
Return the MIME type of this object as retrieved from the source object.
Return the DataSource associated with this instance of DataHandler.
Get the InputStream for this object.
Return the name of the data object.
Get an OutputStream for this DataHandler to allow overwriting the underlying data.
Return the preferred commands for this type of data.
Returns an object that represents the data to be transferred.
Return the DataFlavors in which this data is available.
Returns whether the specified data flavor is supported for this object.
Set the CommandMap for use by this DataHandler.
Sets the DataContentHandlerFactory.
Write the data to an
public DataHandler(DataSource ds)
DataHandlerinstance referencing the specified DataSource. The data exists in a byte stream form. The DataSource will provide an InputStream to access the data.
ds- the DataSource
DataHandlerinstance representing an object of this MIME type. This constructor is used when the application already has an in-memory representation of the data in the form of a Java Object.
obj- the Java Object
mimeType- the MIME type of the object
public DataHandler(URL url)
DataHandlerinstance referencing a URL. The DataHandler internally creates a
URLDataSourceinstance to represent the URL.
url- a URL object
public DataSource getDataSource()
For DataHandlers that have been instantiated with a DataSource, this method returns the DataSource that was used to create the DataHandler object. In other cases the DataHandler constructs a DataSource from the data used to construct the DataHandler. DataSources created for DataHandlers not instantiated with a DataSource are cached for performance reasons.
public String getName()
DataSource.getNamemethod, otherwise it returns null.
public String getContentType()
public InputStream getInputStream() throws IOException
For DataHandlers instantiated with a DataSource, the DataHandler
DataSource.getInputStream method and
returns the result to the caller.
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
writeTo 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.
IOException- if an I/O error occurs
DataContentHandler.writeTo(java.lang.Object, java.lang.String, java.io.OutputStream),
public void writeTo(OutputStream os) throws IOException
If the DataHandler was created with a DataSource, writeTo retrieves the InputStream and copies the bytes from the InputStream to the OutputStream passed in.
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
writeTo method on the
os- the OutputStream to write to
IOException- if an I/O error occurs
public OutputStream getOutputStream() throws IOException
getOutputStreammethod is called. Otherwise,
public DataFlavor getTransferDataFlavors()
Returns an array of DataFlavor objects indicating the flavors the data can be provided in. The array is usually ordered according to preference for providing the data, from most richly descriptive to least richly descriptive.
The DataHandler attempts to find a DataContentHandler that
corresponds to the MIME type of the data. If one is located,
the DataHandler calls the DataContentHandler's
If a DataContentHandler can not be located, and if the
DataHandler was created with a DataSource (or URL), one
DataFlavor is returned that represents this object's MIME type
java.io.InputStream class. If the
DataHandler was created with an object and a MIME type,
getTransferDataFlavors returns one DataFlavor that represents
this object's MIME type and the object's class.
public boolean isDataFlavorSupported(DataFlavor flavor)
This method iterates through the DataFlavors returned from
getTransferDataFlavors, comparing each with
the specified flavor.
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException
For DataHandler's created with DataSources or URLs:
The DataHandler attempts to locate a DataContentHandler
for this MIME type. If one is found, the passed in DataFlavor
and the type of the data are passed to its
method. If the DataHandler fails to locate a DataContentHandler
and the flavor specifies this object's MIME type and the
java.io.InputStream class, this object's InputStream
Otherwise it throws an UnsupportedFlavorException.
For DataHandler's created with Objects:
The DataHandler attempts to locate a DataContentHandler for this MIME type. If one is found, the passed in DataFlavor and the type of the data are passed to its getTransferData method. If the DataHandler fails to locate a DataContentHandler and the flavor specifies this object's MIME type and its class, this DataHandler's referenced object is returned. Otherwise it throws an UnsupportedFlavorException.
public void setCommandMap(CommandMap commandMap)
nullcauses the CommandMap to revert to the CommandMap returned by the
CommandMap.getDefaultCommandMapmethod. Changing the CommandMap, or setting it to
null, clears out any data cached from the previous CommandMap.
commandMap- the CommandMap to use in this DataHandler
public CommandInfo getPreferredCommands()
getPreferredCommandsmethod in the CommandMap associated with this instance of DataHandler. This method returns an array that represents a subset of available commands. In cases where multiple commands for the MIME type represented by this DataHandler are present, the installed CommandMap chooses the appropriate commands.
public CommandInfo getAllCommands()
getAllCommandsmethod of the CommandMap associated with this DataHandler.
public CommandInfo getCommand(String cmdName)
getCommandmethod of the CommandMap associated with this DataHandler.
cmdName- the command name
public Object getContent() throws IOException
If the DataHandler was instantiated with an object, return the object.
If the DataHandler was instantiated with a DataSource,
this method uses a DataContentHandler to return the content
object for the data represented by this DataHandler. If no
DataContentHandler can be found for the
the type of this data, the DataHandler returns an
InputStream for the data.
IOException- if an IOException occurs during this operation.
public Object getBean(CommandInfo cmdinfo)
This method calls the CommandInfo's
method with the
ClassLoader used to load
javax.activation.DataHandler class itself.
cmdinfo- the CommandInfo corresponding to a command
public static void setDataContentHandlerFactory(DataContentHandlerFactory newFactory)
If the DataContentHandlerFactory has already been set, this method throws an Error.
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2015, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.