dataflavor.java
来自「linux下建立JAVA虚拟机的源码KAFFE」· Java 代码 · 共 982 行 · 第 1/2 页
JAVA
982 行
{ if ("humanPresentableName".equals(paramName)) return getHumanPresentableName(); return getParameter(paramName, mimeType); } /** * Sets the human presentable name to the specified value. * * @param humanPresentableName The new display name. */ public void setHumanPresentableName(String humanPresentableName) { this.humanPresentableName = humanPresentableName; } /** * Tests the MIME type of this object for equality against the specified * MIME type. Ignores parameters. * * @param mimeType The MIME type to test against. * * @return <code>true</code> if the MIME type is equal to this object's * MIME type (ignoring parameters), <code>false</code> otherwise. * * @exception NullPointerException If mimeType is null. */ public boolean isMimeTypeEqual(String mimeType) { String mime = getMimeType(); int i = mime.indexOf(";"); if (i != -1) mime = mime.substring(0, i); i = mimeType.indexOf(";"); if (i != -1) mimeType = mimeType.substring(0, i); return mime.equals(mimeType); } /** * Tests the MIME type of this object for equality against the specified * data flavor's MIME type * * @param flavor The flavor to test against. * * @return <code>true</code> if the flavor's MIME type is equal to this * object's MIME type, <code>false</code> otherwise. */ public final boolean isMimeTypeEqual(DataFlavor flavor) { return isMimeTypeEqual(flavor.getMimeType()); } /** * Tests whether or not this flavor represents a serialized object. * * @return <code>true</code> if this flavor represents a serialized * object, <code>false</code> otherwise. */ public boolean isMimeTypeSerializedObject() { return mimeType.startsWith(javaSerializedObjectMimeType); } /** * Tests whether or not this flavor has a representation class of * <code>java.io.InputStream</code>. * * @return <code>true</code> if the representation class of this flavor * is <code>java.io.InputStream</code>, <code>false</code> otherwise. */ public boolean isRepresentationClassInputStream() { return representationClass.getName().equals("java.io.InputStream"); } /** * Tests whether the representation class for this flavor is * serializable. * * @return <code>true</code> if the representation class is serializable, * <code>false</code> otherwise. */ public boolean isRepresentationClassSerializable() { Class[] interfaces = representationClass.getInterfaces(); int i = 0; while (i < interfaces.length) { if (interfaces[i].getName().equals("java.io.Serializable")) return true; ++i; } return false; } /** * Tests whether the representation class for his flavor is remote. * * @return <code>true</code> if the representation class is remote, * <code>false</code> otherwise. */ public boolean isRepresentationClassRemote() { return Remote.class.isAssignableFrom (representationClass); } /** * Tests whether or not this flavor represents a serialized object. * * @return <code>true</code> if this flavor represents a serialized * object, <code>false</code> otherwise. */ public boolean isFlavorSerializedObjectType() { // FIXME: What is the diff between this and isMimeTypeSerializedObject? return(mimeType.startsWith(javaSerializedObjectMimeType)); } /** * Tests whether or not this flavor represents a remote object. * * @return <code>true</code> if this flavor represents a remote object, * <code>false</code> otherwise. */ public boolean isFlavorRemoteObjectType() { return(mimeType.startsWith(javaRemoteObjectMimeType)); } /** * Tests whether or not this flavor represents a list of files. * * @return <code>true</code> if this flavor represents a list of files, * <code>false</code> otherwise. */ public boolean isFlavorJavaFileListType() { if (mimeType.equals(javaFileListFlavor.mimeType) && representationClass.equals(javaFileListFlavor.representationClass)) return true; return false ; } /** * Returns a copy of this object. * * @return A copy of this object. * * @exception CloneNotSupportedException If the object's class does not support * the Cloneable interface. Subclasses that override the clone method can also * throw this exception to indicate that an instance cannot be cloned. */ public Object clone () throws CloneNotSupportedException { // FIXME - This cannot be right. try { return super.clone(); } catch(Exception e) { return null; } } /** * This method test the specified <code>DataFlavor</code> for equality * against this object. This will be true if the MIME type and * representation type are the equal. * * @param flavor The <code>DataFlavor</code> to test against. * * @return <code>true</code> if the flavor is equal to this object, * <code>false</code> otherwise. */ public boolean equals(DataFlavor flavor) { if (flavor == null) return false; if (! this.mimeType.toLowerCase().equals(flavor.mimeType.toLowerCase())) return false; if (! this.representationClass.equals(flavor.representationClass)) return false; return true; } /** * This method test the specified <code>Object</code> for equality * against this object. This will be true if the following conditions * are met: * <p> * <ul> * <li>The object is not <code>null</code>.</li> * <li>The object is an instance of <code>DataFlavor</code>.</li> * <li>The object's MIME type and representation class are equal to * this object's.</li> * </ul> * * @param obj The <code>Object</code> to test against. * * @return <code>true</code> if the flavor is equal to this object, * <code>false</code> otherwise. */ public boolean equals(Object obj) { if (! (obj instanceof DataFlavor)) return false; return equals((DataFlavor) obj); } /** * Tests whether or not the specified string is equal to the MIME type * of this object. * * @param str The string to test against. * * @return <code>true</code> if the string is equal to this object's MIME * type, <code>false</code> otherwise. * * @deprecated Not compatible with <code>hashCode()</code>. * Use <code>isMimeTypeEqual()</code> */ public boolean equals(String str) { return isMimeTypeEqual(str); } /** * Returns the hash code for this data flavor. * The hash code is based on the (lower case) mime type and the * representation class. */ public int hashCode() { return mimeType.toLowerCase().hashCode() ^ representationClass.hashCode(); } /** * Returns <code>true</code> when the given <code>DataFlavor</code> * matches this one. */ public boolean match(DataFlavor dataFlavor) { // XXX - How is this different from equals? return equals(dataFlavor); } /** * This method exists for backward compatibility. It simply returns * the same name/value pair passed in. * * @param name The parameter name. * @param value The parameter value. * * @return The name/value pair. * * @deprecated */ protected String normalizeMimeTypeParameter(String name, String value) { return name + "=" + value; } /** * This method exists for backward compatibility. It simply returns * the MIME type string unchanged. * * @param type The MIME type. * * @return The MIME type. * * @deprecated */ protected String normalizeMimeType(String type) { return type; } /** * Serialize this class. * * @param stream The <code>ObjectOutput</code> stream to serialize to. * * @exception IOException If an error occurs. */ public void writeExternal(ObjectOutput stream) throws IOException { // FIXME: Implement me } /** * De-serialize this class. * * @param stream The <code>ObjectInput</code> stream to deserialize from. * * @exception IOException If an error ocurs. * @exception ClassNotFoundException If the class for an object being restored * cannot be found. */ public void readExternal(ObjectInput stream) throws IOException, ClassNotFoundException { // FIXME: Implement me } /** * Returns a string representation of this DataFlavor. Including the * representation class name, MIME type and human presentable name. */ public String toString() { return (getClass().getName() + "[representationClass=" + getRepresentationClass().getName() + ",mimeType=" + getMimeType() + ",humanPresentableName=" + getHumanPresentableName() + "]"); } /** * XXX - Currently returns <code>java.io.InputStream</code>. * * @since 1.3 */ public final Class getDefaultRepresentationClass() { return java.io.InputStream.class; } /** * XXX - Currently returns <code>java.io.InputStream</code>. */ public final String getDefaultRepresentationClassAsString() { return getDefaultRepresentationClass().getName(); } /** * Creates a <code>Reader</code> for a given <code>Transferable</code>. * * If the representation class is a (subclass of) <code>Reader</code> * then an instance of the representation class is returned. If the * representatation class is a <code>String</code> then a * <code>StringReader</code> is returned. And if the representation class * is a (subclass of) <code>InputStream</code> and the primary MIME type * is "text" then a <code>InputStreamReader</code> for the correct charset * encoding is returned. * * @param transferable The <code>Transferable</code> for which a text * <code>Reader</code> is requested. * * @exception IllegalArgumentException If the representation class is not one * of the seven listed above or the Transferable has null data. * @exception NullPointerException If the Transferable is null. * @exception UnsupportedFlavorException when the transferable doesn't * support this <code>DataFlavor</code>. Or if the representable class * isn't a (subclass of) <code>Reader</code>, <code>String</code>, * <code>InputStream</code> and/or the primary MIME type isn't "text". * @exception IOException when any IOException occurs. * @exception UnsupportedEncodingException if the "charset" isn't supported * on this platform. */ public Reader getReaderForText(Transferable transferable) throws UnsupportedFlavorException, IOException { if (!transferable.isDataFlavorSupported(this)) throw new UnsupportedFlavorException(this); if (Reader.class.isAssignableFrom(representationClass)) return (Reader)transferable.getTransferData(this); if (String.class.isAssignableFrom(representationClass)) return new StringReader((String)transferable.getTransferData(this)); if (InputStream.class.isAssignableFrom(representationClass) && "text".equals(getPrimaryType())) { InputStream in = (InputStream)transferable.getTransferData(this); String encoding = getParameter("charset"); if (encoding == null) encoding = "us-ascii"; return new InputStreamReader(in, encoding); } throw new UnsupportedFlavorException(this); } /** * Returns whether the representation class for this DataFlavor is * @see java.nio.ByteBuffer or a subclass thereof. * * @since 1.4 */ public boolean isRepresentationClassByteBuffer() { return ByteBuffer.class.isAssignableFrom(representationClass); } /** * Returns whether the representation class for this DataFlavor is * @see java.nio.CharBuffer or a subclass thereof. * * @since 1.4 */ public boolean isRepresentationClassCharBuffer() { return CharBuffer.class.isAssignableFrom(representationClass); } /** * Returns whether the representation class for this DataFlavor is * @see java.io.Reader or a subclass thereof. * * @since 1.4 */ public boolean isRepresentationClassReader() { return Reader.class.isAssignableFrom(representationClass); } /** * Returns whether this <code>DataFlavor</code> is a valid text flavor for * this implementation of the Java platform. Only flavors equivalent to * <code>DataFlavor.stringFlavor</code> and <code>DataFlavor</code>s with * a primary MIME type of "text" can be valid text flavors. * <p> * If this flavor supports the charset parameter, it must be equivalent to * <code>DataFlavor.stringFlavor</code>, or its representation must be * <code>java.io.Reader</code>, <code>java.lang.String</code>, * <code>java.nio.CharBuffer</code>, <code>java.io.InputStream</code> or * <code>java.nio.ByteBuffer</code>, * If the representation is <code>java.io.InputStream</code> or * <code>java.nio.ByteBuffer</code>, then this flavor's <code>charset</code> * parameter must be supported by this implementation of the Java platform. * If a charset is not specified, then the platform default charset, which * is always supported, is assumed. * <p> * If this flavor does not support the charset parameter, its * representation must be <code>java.io.InputStream</code>, * <code>java.nio.ByteBuffer</code>. * <p> * See <code>selectBestTextFlavor</code> for a list of text flavors which * support the charset parameter. * * @return <code>true</code> if this <code>DataFlavor</code> is a valid * text flavor as described above; <code>false</code> otherwise * @see #selectBestTextFlavor * @since 1.4 */ public boolean isFlavorTextType() { // FIXME: I'm not 100% sure if this implementation does the same like sun's does if(equals(DataFlavor.stringFlavor) || getPrimaryType().equals("text")) { String charset = getParameter("charset"); Class c = getRepresentationClass(); if(charset != null) { if(Reader.class.isAssignableFrom(c) || CharBuffer.class.isAssignableFrom(c) || String.class.isAssignableFrom(c)) { return true; } else if(InputStream.class.isAssignableFrom(c) || ByteBuffer.class.isAssignableFrom(c)) { return Charset.isSupported(charset); } } else if(InputStream.class.isAssignableFrom(c) || ByteBuffer.class.isAssignableFrom(c)) { return true; } } return false; }} // class DataFlavor
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?