dataflavor.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 1,007 行 · 第 1/2 页

JAVA
1,007
字号
  *
  * @return <code>true</code> if the MIME type is equal to this object's
  * MIME type, <code>false</code> otherwise.
  *
  * @exception NullPointerException If mimeType is null.
  */
public final boolean
isMimeTypeEqual(String mimeType)
{
  // FIXME: Need to handle default attributes and parameters

  return(this.mimeType.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 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>.
  *
  * @param <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.
  *
  * @param <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()
{
  // FIXME: Implement
  throw new RuntimeException("Not implemented");
}

/*************************************************************************/

/**
  * 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 (this.mimeType.equals(javaFileListFlavor.mimeType) &&
      this.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()
{
  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>The object is an instance of <code>DataFlavor</code>.
  * <li>The object's MIME type and representation class are equal to
  * this object's.
  * </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 == null)
    return(false);

  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("DataFlavor[representationClass="
         + representationClass.getName()
         + ",mimeType="
         + mimeType
         + "humanPresentableName="
         + humanPresentableName);
}

/*************************************************************************/

/**
  * XXX - Currently returns <code>plainTextFlavor</code>.
  */
public static final DataFlavor
getTextPlainUnicodeFlavor()
{
  return(plainTextFlavor);
}

/*************************************************************************/

/**
  * 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());
}

/*************************************************************************/

/**
  * Selects the best supported text flavor on this implementation.
  * Returns <code>null</code> when none of the given flavors is liked.
  *
  * The <code>DataFlavor</code> returned the first data flavor in the
  * array that has either a representation class which is (a subclass of)
  * <code>Reader</code> or <code>String</code>, or has a representation
  * class which is (a subclass of) <code>InputStream</code> and has a
  * primary MIME type of "text" and has an supported encoding.
  */
public static final DataFlavor
selectBestTextFlavor(DataFlavor[] availableFlavors)
{
  for(int i=0; i<availableFlavors.length; i++)
    {
      DataFlavor df = availableFlavors[i];
      Class c = df.representationClass;

      // A Reader or String is good.
      if ((Reader.class.isAssignableFrom(c))
	  || (String.class.isAssignableFrom(c)))
	{
	  return df;
	}

      // A InputStream is good if the mime primary type is "text"
      if ((InputStream.class.isAssignableFrom(c))
	  && ("text".equals(df.getPrimaryType())))
        {
          String encoding = availableFlavors[i].getParameter("charset");
          if (encoding == null)
            encoding = "us-ascii";
          Reader r = null;
          try
            {
              // Try to construct a dummy reader with the found encoding
              r = new InputStreamReader
                    (new ByteArrayInputStream(new byte[0]), encoding);
            }
          catch(UnsupportedEncodingException uee) { /* ignore */ }
          if (r != null)
            return df;
        }
    }

  // Nothing found
  return(null);
}

/*************************************************************************/

/**
  * 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,
                                                   UnsupportedEncodingException
{
    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);
}

} // class DataFlavor

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?