Class DataFlavor in Java

digitization, transformation, binary

Class DataFlavor in Java

Objects of this type define a data type or format for the purposes of data transfer. A DataFlavor is characterized by two values. The first is a descriptive human-readable name, passed to a DataFlavor() constructor or set with setHumanPresentableName().

The second component of a DataFlavor specifies the type of data to be transferred in a more machine-readable way. The two DataFlavor() constructors allow two distinct ways to specify this data type. One way is by directly specifying the representation class of the data.

Ads code goes here

This is a Class object that defines the Java type that the recipient of a data transfer is passed. The other way to specify the data type represented by a DataFlavor is to pass a string that specifies the MIME type of the data to be transferred.

When we construct a DataFlavor object by specifying the representation type of the data, the MIME type of the DataFlavor is automatically set to:

application/x-java-serialized-object class=classname

This indicates that the object is to be transferred using the data format of the Java object serialization protocol. When we pass a MIME type string to the DataFlavor() constructor, on the other hand, the representation class of the DataFlavor is automatically set to the Class object for java.io.InputStream.

This means that the recipient of the data transfer is given an InputStream object from which it can read and parse data in the specified MIME format.

READ  Class Modifier in Java

Because the same MIME type can be specified with a number of slightly different strings, the DataFlavor class converts the MIME type to a canonical form so that it can be uniquely identified and compared. Use isMimeTypeEqual() to compare the MIME type of a DataFlavor object with another MIME type, or with the MIME type of another DataFlavor.

Because textual data is so often transferred, the DataFlavor class defines constants for two commonly-used data flavors.

stringFlavor represents text transferred as a String object, while plainTextFlavor represents text transferred through an InputStream, using the text/plain MIME type.

In short, The DataFlavor class encapsulates information about data formats.

The structure of the class DataFlavor is given by

public class java.awt.datatransfer.DataFlavor extends java.lang.Object {
// Public Constructors
public DataFlavor(Class representationClass, String humanPresentableName);
public DataFlavor(String mimeType, String humanPresentableName);
// Class Variables
public static DataFlavor plainTextFlavor;
public static DataFlavor stringFlavor;
// Public Instance Methods
public boolean equals(DataFlavor dataFlavor);
public String getHumanPresentableName();
public String getMimeType();
public Class getRepresentationClass();
public boolean isMimeTypeEqual(String mimeType);
public final boolean isMimeTypeEqual(DataFlavor dataFlavor);
public void setHumanPresentableName(String humanPresentableName);
// Protected Instance Methods
protected String normalizeMimeType(String mimeType);
protected String normalizeMimeTypeParameter(String parameterName, String parameterValue);
}

The details of the class structure are given as follows:

public static DataFlavor plainTextFlavor;

public static DataFlavor plainTextFlavor represents a preset DataFlavor object representing plain text.

public static DataFlavor stringFlavor;

public static DataFlavor stringFlavor represents a preset DataFlavor object representing a Java String.

public DataFlavor(Class representationClass,String humanPresentableName);

public DataFlavor(Class representationClass,String humanPresentableName) constructs a DataFlavor object with the given characteristics. The MIME type for this DataFlavor is application/x-java-serialized-object .

Parameter
representationClass – The Java class that represents data in this flavor.
humanPresentableName – A name for this flavor that humans will recognize.

public DataFlavor(String MIMEType, String humanPresentableName);

public DataFlavor(String MIMEType, String humanPresentableName) constructs a DataFlavor object with the given characteristics. The representation class used for this DataFlavor is java.io.InputStream.

READ  How to Resolve NoSuchSessionException in selenium free?

Parameter
MIMEType – The MIME type string this DataFlavor represents.
humanPresentableName – A name for this flavor that humans will recognize.

public boolean equals(DataFlavor dataFlavor);

public boolean equals(DataFlavor dataFlavor) method compares two different DataFlavor instances for equivalence.

This method returns true if dataFlavor is equivalent to this DataFlavor, false otherwise.

Parameter
dataFlavor – The flavor to compare.

public String getHumanPresentableName();

public String getHumanPresentableName() method returns the name of this flavor.

This method returns the name of this flavor.

public String getMIMEType();

public String getMIMEType() method returns the MIME type string for this flavor.

This method returns the MIME type string for this flavor.

public Class getRepresentationClass();

public Class getRepresentationClass() method returns the Java class that will be used to represent data in this flavor.

This method returns the Java class that will be used to represent data in this flavor.

public boolean isMIMETypeEqual(String MIMEType);

public boolean isMIMETypeEqual(String MIMEType) method compares two different DataFlavor MIME types for equivalence.

This method returns true if the given MIME-type is the same as this DataFlavor’s MIME type; false otherwise.

Parameter
MIMEType – The type to compare.

public final boolean isMIMETypeEqual(DataFlavor dataFlavor);

public final boolean isMIMETypeEqual(DataFlavor dataFlavor) method compares two different DataFlavor MIME types for equivalence.

This method returns true if DataFlavor’s MIME-type is the same as this DataFlavor’s MIME type; false otherwise.

Parameter
dataFlavor – The flavor to compare.

public void setHumanPresentableName(String humanPresentableName);

public void setHumanPresentableName(String humanPresentableName) method changes the name of the DataFlavor.

Parameter
humanPresentableName – A name for this flavor that humans will recognize.

protected String normalizeMIMEType(String MIMEType);

protected String normalizeMIMEType(String MIMEType) method is called for each MIME type string. Subclasses can override this method to add default parameter/value pairs to MIME strings.

READ  Learn How to Use Jacoco Java code Coverage Effectively in 30 Minutes

This method returns the normalized MIME type string.

Parameter
MIMEType – The MIME type string to normalize.

protected String normalizeMIMETypeParameter(String parameterName,String parameterValue);

protected String normalizeMIMETypeParameter(String parameterName,String parameterValue) method is called for each MIME type parameter string. Subclasses can override this method to handle special parameters, such as those that are case-insensitive.

This method returns the normalized MIME type parameter string.

Parameter
parameterName – The MIME type parameter to normalize.
parameterValue – The corresponding value.

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *