📄 dataflavor.java
字号:
/* DataFlavor.java -- A type of data to transfer via the clipboard. Copyright (C) 1999, 2001 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package java.awt.datatransfer;import java.io.ByteArrayInputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.io.IOException;import java.io.ObjectOutput;import java.io.ObjectInput;import java.io.Reader;import java.io.StringReader;import java.io.UnsupportedEncodingException;/** * This class represents a particular data format used for transferring * data via the clipboard. * * @author Aaron M. Renn (arenn@urbanophile.com) */public class DataFlavor implements java.io.Externalizable, Cloneable{ static final long serialVersionUID = 8367026044764648243L;// FIXME: Serialization: Need to write methods for./* * Static Variables *//** * This is the data flavor used for tranferring plain text. The MIME * type is "text/plain; charset=unicode". The representation class * is <code>java.io.InputStream</code>. * * @deprecated The charset unicode is platform specific and InputStream * deals with bytes not chars. Use <code>getRederForText()</code>. */public static final DataFlavor plainTextFlavor;/** * This is the data flavor used for transferring Java strings. The * MIME type is "application/x-java-serialized-object" and the * representation class is <code>java.lang.String</code>. */public static final DataFlavor stringFlavor;/** * This is a data flavor used for transferring lists of files. The * representation type is a <code>java.util.List</code>, with each element of * the list being a <code>java.io.File</code>. */public static final DataFlavor javaFileListFlavor;public static final DataFlavor imageFlavor;/** * This is the MIME type used for transferring a serialized object. * The representation class is the type of object be deserialized. */public static final String javaSerializedObjectMimeType = "application/x-java-serialized-object";/** * This is the MIME type used to transfer a Java object reference within * the same JVM. The representation class is the class of the object * being transferred. */public static final String javaJVMLocalObjectMimeType = "application/x-java-jvm-local-object";/** * This is the MIME type used to transfer a link to a remote object. * The representation class is the type of object being linked to. */public static final String javaRemoteObjectMimeType = "application/x-java-remote-object";static{ plainTextFlavor = new DataFlavor(java.io.InputStream.class, "text/plain; charset=unicode", "plain unicode text"); stringFlavor = new DataFlavor(java.lang.String.class, "Java Unicode String"); javaFileListFlavor = new DataFlavor(java.util.List.class, "Java File List"); // javaFileListFlavor.mimeType = "application/x-java-file-list"; imageFlavor = new DataFlavor(java.awt.Image.class, "Java Image");}/*************************************************************************//* * Instance Variables */// The MIME type for this flavorprivate final String mimeType;// The representation class for this flavorprivate final Class representationClass;// The human readable name of this flavorprivate String humanPresentableName;/*************************************************************************//* * Static Methods *//** * This method attempts to load the named class. The following class * loaders are searched in order: the bootstrap class loader, the * system class loader, the context class loader (if it exists), and * the specified fallback class loader. * * @param className The name of the class to load. * @param classLoader The class loader to use if all others fail, which * may be <code>null</code>. * * @exception ClassNotFoundException If the class cannot be loaded. */protected static final ClasstryToLoadClass(String className, ClassLoader classLoader) throws ClassNotFoundException{ try { return(Class.forName(className)); } catch(Exception e) { ; } // Commented out for Java 1.1 /* try { return(className.getClass().getClassLoader().findClass(className)); } catch(Exception e) { ; } try { return(ClassLoader.getSystemClassLoader().findClass(className)); } catch(Exception e) { ; } */ // FIXME: What is the context class loader? /* try { } catch(Exception e) { ; } */ if (classLoader != null) return(classLoader.loadClass(className)); else throw new ClassNotFoundException(className);}/*************************************************************************//* * Constructors *//** * Empty public constructor needed for externalization. * Should not be used for normal instantiation. */publicDataFlavor(){ mimeType = null; representationClass = null; humanPresentableName = null;}/*************************************************************************//** * Private constructor. */privateDataFlavor(Class representationClass, String mimeType, String humanPresentableName){ this.representationClass = representationClass; this.mimeType = mimeType; if (humanPresentableName != null) this.humanPresentableName = humanPresentableName; else this.humanPresentableName = mimeType;}/*************************************************************************//** * Initializes a new instance of <code>DataFlavor</code>. The class * and human readable name are specified, the MIME type will be * "application/x-java-serialized-object". If the human readable name * is not specified (<code>null</code>) then the human readable name * will be the same as the MIME type. * * @param representationClass The representation class for this object. * @param humanPresentableName The display name of the object. */publicDataFlavor(Class representationClass, String humanPresentableName){ this(representationClass, "application/x-java-serialized-object" + "; class=" + representationClass.getName(), humanPresentableName);}/*************************************************************************//** * Initializes a new instance of <code>DataFlavor</code> with the * specified MIME type and description. If the MIME type has a * "class=<rep class>" parameter then the representation class will * be the class name specified. Otherwise the class defaults to * <code>java.io.InputStream</code>. If the human readable name * is not specified (<code>null</code>) then the human readable name * will be the same as the MIME type. * * @param mimeType The MIME type for this flavor. * @param humanPresentableName The display name of this flavor. * @param classLoader The class loader for finding classes if the default * class loaders do not work. * * @exception IllegalArgumentException If the representation class * specified cannot be loaded. * @exception ClassNotFoundException If the class is not loaded. */publicDataFlavor(String mimeType, String humanPresentableName, ClassLoader classLoader) throws ClassNotFoundException{ this(getRepresentationClassFromMime(mimeType, classLoader), mimeType, humanPresentableName);}private static ClassgetRepresentationClassFromMime(String mimeString, ClassLoader classLoader){ String classname = getParameter("class", mimeString); if (classname != null) { try { return tryToLoadClass(classname, classLoader); } catch(Exception e) { throw new IllegalArgumentException("classname: " + e.getMessage()); } } else { return java.io.InputStream.class; }}/*************************************************************************//** * Initializes a new instance of <code>DataFlavor</code> with the * specified MIME type and description. If the MIME type has a * "class=<rep class>" parameter then the representation class will * be the class name specified. Otherwise the class defaults to * <code>java.io.InputStream</code>. If the human readable name * is not specified (<code>null</code>) then the human readable name * will be the same as the MIME type. This is the same as calling * <code>new DataFlavor(mimeType, humanPresentableName, null)</code>. * * @param mimeType The MIME type for this flavor. * @param humanPresentableName The display name of this flavor. * @param classLoader The class loader for finding classes. * * @exception IllegalArgumentException If the representation class * specified cannot be loaded. */publicDataFlavor(String mimeType, String humanPresentableName) throws ClassNotFoundException{ this(mimeType, humanPresentableName, null);}/*************************************************************************//** * Initializes a new instance of <code>DataFlavor</code> with the specified * MIME type. This type can have a "class=" parameter to specify the * representation class, and then the class must exist or an exception will * be thrown. If there is no "class=" parameter then the representation class * will be <code>java.io.InputStream</code>. This is the same as calling * <code>new DataFlavor(mimeType, null)</code>. * * @param mimeType The MIME type for this flavor. * * @exception IllegalArgumentException If a class is not specified in * the MIME type. * @exception ClassNotFoundException If the class cannot be loaded. */publicDataFlavor(String mimeType) throws ClassNotFoundException{ this(mimeType, null);}/*************************************************************************//** * Returns the MIME type of this flavor. * * @return The MIME type for this flavor. */public StringgetMimeType(){ return(mimeType);}/*************************************************************************//** * Returns the representation class for this flavor. * * @return The representation class for this flavor. */public ClassgetRepresentationClass(){ return(representationClass);}/*************************************************************************//** * Returns the human presentable name for this flavor. * * @return The human presentable name for this flavor. */public StringgetHumanPresentableName(){ return(humanPresentableName);} /*************************************************************************//** * Returns the primary MIME type for this flavor. * * @return The primary MIME type for this flavor. */public StringgetPrimaryType(){ int idx = mimeType.indexOf("/"); if (idx == -1) return(mimeType); return(mimeType.substring(0, idx));}/*************************************************************************//** * Returns the MIME subtype for this flavor. * * @return The MIME subtype for this flavor. */public StringgetSubType(){ int idx = mimeType.indexOf("/"); if (idx == -1) return(""); String subtype = mimeType.substring(idx + 1); idx = subtype.indexOf(" "); if (idx == -1) return(subtype); else return(subtype.substring(0, idx));}/*************************************************************************//** * Returns the value of the named MIME type parameter, or <code>null</code> * if the parameter does not exist. Given the parameter name and the mime * string. * * @param paramName The name of the parameter. * @param mimeString The mime string from where the name should be found. * * @return The value of the parameter or null. */private static StringgetParameter(String paramName, String mimeString){ int idx = mimeString.indexOf(paramName + "="); if (idx == -1) return(null); String value = mimeString.substring(idx + paramName.length() + 2); idx = value.indexOf(" "); if (idx == -1) return(value); else return(value.substring(0, idx));}/*************************************************************************//** * Returns the value of the named MIME type parameter, or <code>null</code> * if the parameter does not exist. * * @param paramName The name of the paramter. * * @return The value of the parameter. */public StringgetParameter(String paramName){ return getParameter(paramName, mimeType);}/*************************************************************************//** * Sets the human presentable name to the specified value. * * @param humanPresentableName The new display name. */public voidsetHumanPresentableName(String humanPresentableName){ this.humanPresentableName = humanPresentableName;}/*************************************************************************//** * Tests the MIME type of this object for equality against the specified * MIME type. * * @param mimeType The MIME type to test against.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -