Merge from mainline.
[official-gcc.git] / libjava / classpath / java / awt / datatransfer / DataFlavor.java
blob788ae6d6a52d7fd72ba854ec787647f12db35256
1 /* DataFlavor.java -- A type of data to transfer via the clipboard.
2 Copyright (C) 1999, 2001, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.awt.datatransfer;
41 import java.io.ByteArrayInputStream;
42 import java.io.IOException;
43 import java.io.InputStream;
44 import java.io.InputStreamReader;
45 import java.io.ObjectInput;
46 import java.io.ObjectOutput;
47 import java.io.Reader;
48 import java.io.StringReader;
49 import java.io.UnsupportedEncodingException;
50 import java.nio.ByteBuffer;
51 import java.nio.CharBuffer;
52 import java.nio.charset.Charset;
53 import java.rmi.Remote;
55 /**
56 * This class represents a particular data format used for transferring
57 * data via the clipboard.
59 * @author Aaron M. Renn (arenn@urbanophile.com)
61 public class DataFlavor implements java.io.Externalizable, Cloneable
63 static final long serialVersionUID = 8367026044764648243L;
65 // FIXME: Serialization: Need to write methods for.
67 /**
68 * This is the data flavor used for tranferring plain text. The MIME
69 * type is "text/plain; charset=unicode". The representation class
70 * is <code>java.io.InputStream</code>.
72 * @deprecated The charset unicode is platform specific and InputStream
73 * deals with bytes not chars. Use <code>getRederForText()</code>.
75 public static final DataFlavor plainTextFlavor =
76 new DataFlavor(java.io.InputStream.class,
77 "text/plain; charset=unicode",
78 "plain unicode text");
80 /**
81 * This is the data flavor used for transferring Java strings. The
82 * MIME type is "application/x-java-serialized-object" and the
83 * representation class is <code>java.lang.String</code>.
85 public static final DataFlavor stringFlavor =
86 new DataFlavor(java.lang.String.class, "Java Unicode String");
88 /**
89 * This is a data flavor used for transferring lists of files. The
90 * representation type is a <code>java.util.List</code>, with each
91 * element of the list being a <code>java.io.File</code>.
93 public static final DataFlavor javaFileListFlavor =
94 new DataFlavor(java.util.List.class,
95 "application/x-java-file-list; class=java.util.List",
96 "Java File List");
98 /**
99 * This is an image flavor used for transferring images. The
100 * representation type is a <code>java.awt.Image</code>.
102 public static final DataFlavor imageFlavor =
103 new DataFlavor(java.awt.Image.class, "Java Image");
106 * This is the MIME type used for transferring a serialized object.
107 * The representation class is the type of object be deserialized.
109 public static final String javaSerializedObjectMimeType =
110 "application/x-java-serialized-object";
113 * This is the MIME type used to transfer a Java object reference within
114 * the same JVM. The representation class is the class of the object
115 * being transferred.
117 public static final String javaJVMLocalObjectMimeType =
118 "application/x-java-jvm-local-objectref";
121 * This is the MIME type used to transfer a link to a remote object.
122 * The representation class is the type of object being linked to.
124 public static final String javaRemoteObjectMimeType =
125 "application/x-java-remote-object";
128 * Instance Variables
131 // The MIME type for this flavor
132 private final String mimeType;
134 // The representation class for this flavor
135 private final Class representationClass;
137 // The human readable name of this flavor
138 private String humanPresentableName;
141 * Static Methods
145 * This method attempts to load the named class. The following class
146 * loaders are searched in order: the bootstrap class loader, the
147 * system class loader, the context class loader (if it exists), and
148 * the specified fallback class loader.
150 * @param className The name of the class to load.
151 * @param classLoader The class loader to use if all others fail, which
152 * may be <code>null</code>.
154 * @exception ClassNotFoundException If the class cannot be loaded.
156 protected static final Class tryToLoadClass(String className,
157 ClassLoader classLoader)
158 throws ClassNotFoundException
160 // Bootstrap
163 return Class.forName(className);
165 catch(ClassNotFoundException cnfe)
167 // Ignored.
170 // System
173 ClassLoader loader = ClassLoader.getSystemClassLoader();
174 return Class.forName(className, true, loader);
176 catch(ClassNotFoundException cnfe)
178 // Ignored.
181 // Context
184 ClassLoader loader = Thread.currentThread().getContextClassLoader();
185 return Class.forName(className, true, loader);
187 catch(ClassNotFoundException cnfe)
189 // Ignored.
192 if (classLoader != null)
193 return Class.forName(className, true, classLoader);
195 throw new ClassNotFoundException(className);
198 private static Class getRepresentationClassFromMime(String mimeString,
199 ClassLoader classLoader)
201 String classname = getParameter("class", mimeString);
202 if (classname != null)
206 return tryToLoadClass(classname, classLoader);
208 catch(Exception e)
210 IllegalArgumentException iae;
211 iae = new IllegalArgumentException("mimeString: "
212 + mimeString
213 + " classLoader: "
214 + classLoader);
215 iae.initCause(e);
216 throw iae;
219 else
220 return java.io.InputStream.class;
224 * Returns the value of the named MIME type parameter, or <code>null</code>
225 * if the parameter does not exist. Given the parameter name and the mime
226 * string.
228 * @param paramName The name of the parameter.
229 * @param mimeString The mime string from where the name should be found.
231 * @return The value of the parameter or null.
233 private static String getParameter(String paramName, String mimeString)
235 int idx = mimeString.indexOf(paramName + "=");
236 if (idx == -1)
237 return(null);
239 String value = mimeString.substring(idx + paramName.length() + 1);
241 idx = value.indexOf(" ");
242 if (idx == -1)
243 return(value);
244 else
245 return(value.substring(0, idx));
249 * XXX - Currently returns <code>plainTextFlavor</code>.
251 public static final DataFlavor getTextPlainUnicodeFlavor()
253 return plainTextFlavor;
257 * Selects the best supported text flavor on this implementation.
258 * Returns <code>null</code> when none of the given flavors is liked.
260 * The <code>DataFlavor</code> returned the first data flavor in the
261 * array that has either a representation class which is (a subclass of)
262 * <code>Reader</code> or <code>String</code>, or has a representation
263 * class which is (a subclass of) <code>InputStream</code> and has a
264 * primary MIME type of "text" and has an supported encoding.
266 public static final DataFlavor
267 selectBestTextFlavor(DataFlavor[] availableFlavors)
269 for(int i = 0; i < availableFlavors.length; i++)
271 DataFlavor df = availableFlavors[i];
272 Class c = df.representationClass;
274 // A Reader or String is good.
275 if ((Reader.class.isAssignableFrom(c))
276 || (String.class.isAssignableFrom(c)))
277 return df;
279 // A InputStream is good if the mime primary type is "text"
280 if ((InputStream.class.isAssignableFrom(c))
281 && ("text".equals(df.getPrimaryType())))
283 String encoding = availableFlavors[i].getParameter("charset");
284 if (encoding == null)
285 encoding = "us-ascii";
286 Reader r = null;
289 // Try to construct a dummy reader with the found encoding
290 r = new InputStreamReader
291 (new ByteArrayInputStream(new byte[0]), encoding);
293 catch(UnsupportedEncodingException uee) { /* ignore */ }
295 if (r != null)
296 return df;
300 // Nothing found
301 return null;
306 * Constructors
310 * Empty public constructor needed for externalization.
311 * Should not be used for normal instantiation.
313 public DataFlavor()
315 mimeType = null;
316 representationClass = null;
317 humanPresentableName = null;
321 * Private constructor.
323 private DataFlavor(Class representationClass,
324 String mimeType,
325 String humanPresentableName)
327 this.representationClass = representationClass;
328 this.mimeType = mimeType;
329 if (humanPresentableName != null)
330 this.humanPresentableName = humanPresentableName;
331 else
332 this.humanPresentableName = mimeType;
336 * Initializes a new instance of <code>DataFlavor</code>. The class
337 * and human readable name are specified, the MIME type will be
338 * "application/x-java-serialized-object". If the human readable name
339 * is not specified (<code>null</code>) then the human readable name
340 * will be the same as the MIME type.
342 * @param representationClass The representation class for this object.
343 * @param humanPresentableName The display name of the object.
345 public DataFlavor(Class representationClass, String humanPresentableName)
347 this(representationClass,
348 "application/x-java-serialized-object"
349 + "; class="
350 + representationClass.getName(),
351 humanPresentableName);
355 * Initializes a new instance of <code>DataFlavor</code> with the
356 * specified MIME type and description. If the MIME type has a
357 * "class=&lt;rep class&gt;" parameter then the representation class will
358 * be the class name specified. Otherwise the class defaults to
359 * <code>java.io.InputStream</code>. If the human readable name
360 * is not specified (<code>null</code>) then the human readable name
361 * will be the same as the MIME type.
363 * @param mimeType The MIME type for this flavor.
364 * @param humanPresentableName The display name of this flavor.
365 * @param classLoader The class loader for finding classes if the default
366 * class loaders do not work.
368 * @exception IllegalArgumentException If the representation class
369 * specified cannot be loaded.
370 * @exception ClassNotFoundException If the class is not loaded.
372 public DataFlavor(String mimeType, String humanPresentableName,
373 ClassLoader classLoader)
374 throws ClassNotFoundException
376 this(getRepresentationClassFromMime(mimeType, classLoader),
377 mimeType, humanPresentableName);
381 * Initializes a new instance of <code>DataFlavor</code> with the
382 * specified MIME type and description. If the MIME type has a
383 * "class=&lt;rep class&gt;" parameter then the representation class will
384 * be the class name specified. Otherwise the class defaults to
385 * <code>java.io.InputStream</code>. If the human readable name
386 * is not specified (<code>null</code>) then the human readable name
387 * will be the same as the MIME type. This is the same as calling
388 * <code>new DataFlavor(mimeType, humanPresentableName, null)</code>.
390 * @param mimeType The MIME type for this flavor.
391 * @param humanPresentableName The display name of this flavor.
393 * @exception IllegalArgumentException If the representation class
394 * specified cannot be loaded.
396 public DataFlavor(String mimeType, String humanPresentableName)
398 this(getRepresentationClassFromMime (mimeType, null),
399 mimeType, humanPresentableName);
403 * Initializes a new instance of <code>DataFlavor</code> with the specified
404 * MIME type. This type can have a "class=" parameter to specify the
405 * representation class, and then the class must exist or an exception will
406 * be thrown. If there is no "class=" parameter then the representation class
407 * will be <code>java.io.InputStream</code>. This is the same as calling
408 * <code>new DataFlavor(mimeType, null)</code>.
410 * @param mimeType The MIME type for this flavor.
412 * @exception IllegalArgumentException If a class is not specified in
413 * the MIME type.
414 * @exception ClassNotFoundException If the class cannot be loaded.
416 public DataFlavor(String mimeType) throws ClassNotFoundException
418 this(mimeType, null);
422 * Returns the MIME type of this flavor.
424 * @return The MIME type for this flavor.
426 public String getMimeType()
428 return(mimeType);
432 * Returns the representation class for this flavor.
434 * @return The representation class for this flavor.
436 public Class getRepresentationClass()
438 return(representationClass);
442 * Returns the human presentable name for this flavor.
444 * @return The human presentable name for this flavor.
446 public String getHumanPresentableName()
448 return(humanPresentableName);
452 * Returns the primary MIME type for this flavor.
454 * @return The primary MIME type for this flavor.
456 public String getPrimaryType()
458 int idx = mimeType.indexOf("/");
459 if (idx == -1)
460 return(mimeType);
462 return(mimeType.substring(0, idx));
466 * Returns the MIME subtype for this flavor.
468 * @return The MIME subtype for this flavor.
470 public String getSubType()
472 int start = mimeType.indexOf("/");
473 if (start == -1)
474 return "";
476 int end = mimeType.indexOf(";", start + 1);
477 if (end == -1)
478 return mimeType.substring(start + 1);
479 else
480 return mimeType.substring(start + 1, end);
484 * Returns the value of the named MIME type parameter, or <code>null</code>
485 * if the parameter does not exist.
487 * @param paramName The name of the paramter.
489 * @return The value of the parameter.
491 public String getParameter(String paramName)
493 if ("humanPresentableName".equals(paramName))
494 return getHumanPresentableName();
496 return getParameter(paramName, mimeType);
500 * Sets the human presentable name to the specified value.
502 * @param humanPresentableName The new display name.
504 public void setHumanPresentableName(String humanPresentableName)
506 this.humanPresentableName = humanPresentableName;
510 * Tests the MIME type of this object for equality against the specified
511 * MIME type. Ignores parameters.
513 * @param mimeType The MIME type to test against.
515 * @return <code>true</code> if the MIME type is equal to this object's
516 * MIME type (ignoring parameters), <code>false</code> otherwise.
518 * @exception NullPointerException If mimeType is null.
520 public boolean isMimeTypeEqual(String mimeType)
522 String mime = getMimeType();
523 int i = mime.indexOf(";");
524 if (i != -1)
525 mime = mime.substring(0, i);
527 i = mimeType.indexOf(";");
528 if (i != -1)
529 mimeType = mimeType.substring(0, i);
531 return mime.equals(mimeType);
535 * Tests the MIME type of this object for equality against the specified
536 * data flavor's MIME type
538 * @param flavor The flavor to test against.
540 * @return <code>true</code> if the flavor's MIME type is equal to this
541 * object's MIME type, <code>false</code> otherwise.
543 public final boolean isMimeTypeEqual(DataFlavor flavor)
545 return isMimeTypeEqual(flavor.getMimeType());
549 * Tests whether or not this flavor represents a serialized object.
551 * @return <code>true</code> if this flavor represents a serialized
552 * object, <code>false</code> otherwise.
554 public boolean isMimeTypeSerializedObject()
556 return mimeType.startsWith(javaSerializedObjectMimeType);
560 * Tests whether or not this flavor has a representation class of
561 * <code>java.io.InputStream</code>.
563 * @return <code>true</code> if the representation class of this flavor
564 * is <code>java.io.InputStream</code>, <code>false</code> otherwise.
566 public boolean isRepresentationClassInputStream()
568 return representationClass.getName().equals("java.io.InputStream");
572 * Tests whether the representation class for this flavor is
573 * serializable.
575 * @return <code>true</code> if the representation class is serializable,
576 * <code>false</code> otherwise.
578 public boolean isRepresentationClassSerializable()
580 Class[] interfaces = representationClass.getInterfaces();
582 int i = 0;
583 while (i < interfaces.length)
585 if (interfaces[i].getName().equals("java.io.Serializable"))
586 return true;
587 ++i;
590 return false;
594 * Tests whether the representation class for his flavor is remote.
596 * @return <code>true</code> if the representation class is remote,
597 * <code>false</code> otherwise.
599 public boolean isRepresentationClassRemote()
601 return Remote.class.isAssignableFrom (representationClass);
605 * Tests whether or not this flavor represents a serialized object.
607 * @return <code>true</code> if this flavor represents a serialized
608 * object, <code>false</code> otherwise.
610 public boolean isFlavorSerializedObjectType()
612 // FIXME: What is the diff between this and isMimeTypeSerializedObject?
613 return(mimeType.startsWith(javaSerializedObjectMimeType));
617 * Tests whether or not this flavor represents a remote object.
619 * @return <code>true</code> if this flavor represents a remote object,
620 * <code>false</code> otherwise.
622 public boolean isFlavorRemoteObjectType()
624 return(mimeType.startsWith(javaRemoteObjectMimeType));
628 * Tests whether or not this flavor represents a list of files.
630 * @return <code>true</code> if this flavor represents a list of files,
631 * <code>false</code> otherwise.
633 public boolean isFlavorJavaFileListType()
635 if (mimeType.equals(javaFileListFlavor.mimeType)
636 && representationClass.equals(javaFileListFlavor.representationClass))
637 return true;
639 return false ;
643 * Returns a copy of this object.
645 * @return A copy of this object.
647 * @exception CloneNotSupportedException If the object's class does not support
648 * the Cloneable interface. Subclasses that override the clone method can also
649 * throw this exception to indicate that an instance cannot be cloned.
651 public Object clone () throws CloneNotSupportedException
653 // FIXME - This cannot be right.
656 return super.clone();
658 catch(Exception e)
660 return null;
665 * This method test the specified <code>DataFlavor</code> for equality
666 * against this object. This will be true if the MIME type and
667 * representation type are the equal.
669 * @param flavor The <code>DataFlavor</code> to test against.
671 * @return <code>true</code> if the flavor is equal to this object,
672 * <code>false</code> otherwise.
674 public boolean equals(DataFlavor flavor)
676 if (flavor == null)
677 return false;
679 if (! this.mimeType.toLowerCase().equals(flavor.mimeType.toLowerCase()))
680 return false;
682 if (! this.representationClass.equals(flavor.representationClass))
683 return false;
685 return true;
689 * This method test the specified <code>Object</code> for equality
690 * against this object. This will be true if the following conditions
691 * are met:
692 * <p>
693 * <ul>
694 * <li>The object is not <code>null</code>.</li>
695 * <li>The object is an instance of <code>DataFlavor</code>.</li>
696 * <li>The object's MIME type and representation class are equal to
697 * this object's.</li>
698 * </ul>
700 * @param obj The <code>Object</code> to test against.
702 * @return <code>true</code> if the flavor is equal to this object,
703 * <code>false</code> otherwise.
705 public boolean equals(Object obj)
707 if (! (obj instanceof DataFlavor))
708 return false;
710 return equals((DataFlavor) obj);
714 * Tests whether or not the specified string is equal to the MIME type
715 * of this object.
717 * @param str The string to test against.
719 * @return <code>true</code> if the string is equal to this object's MIME
720 * type, <code>false</code> otherwise.
722 * @deprecated Not compatible with <code>hashCode()</code>.
723 * Use <code>isMimeTypeEqual()</code>
725 public boolean equals(String str)
727 return isMimeTypeEqual(str);
731 * Returns the hash code for this data flavor.
732 * The hash code is based on the (lower case) mime type and the
733 * representation class.
735 public int hashCode()
737 return mimeType.toLowerCase().hashCode() ^ representationClass.hashCode();
741 * Returns <code>true</code> when the given <code>DataFlavor</code>
742 * matches this one.
744 public boolean match(DataFlavor dataFlavor)
746 // XXX - How is this different from equals?
747 return equals(dataFlavor);
751 * This method exists for backward compatibility. It simply returns
752 * the same name/value pair passed in.
754 * @param name The parameter name.
755 * @param value The parameter value.
757 * @return The name/value pair.
759 * @deprecated
761 protected String normalizeMimeTypeParameter(String name, String value)
763 return name + "=" + value;
767 * This method exists for backward compatibility. It simply returns
768 * the MIME type string unchanged.
770 * @param type The MIME type.
772 * @return The MIME type.
774 * @deprecated
776 protected String normalizeMimeType(String type)
778 return type;
782 * Serialize this class.
784 * @param stream The <code>ObjectOutput</code> stream to serialize to.
786 * @exception IOException If an error occurs.
788 public void writeExternal(ObjectOutput stream) throws IOException
790 // FIXME: Implement me
795 * De-serialize this class.
797 * @param stream The <code>ObjectInput</code> stream to deserialize from.
799 * @exception IOException If an error ocurs.
800 * @exception ClassNotFoundException If the class for an object being restored
801 * cannot be found.
803 public void readExternal(ObjectInput stream)
804 throws IOException, ClassNotFoundException
806 // FIXME: Implement me
810 * Returns a string representation of this DataFlavor. Including the
811 * representation class name, MIME type and human presentable name.
813 public String toString()
815 return (getClass().getName()
816 + "[representationClass=" + getRepresentationClass().getName()
817 + ",mimeType=" + getMimeType()
818 + ",humanPresentableName=" + getHumanPresentableName()
819 + "]");
823 * XXX - Currently returns <code>java.io.InputStream</code>.
825 * @since 1.3
827 public final Class getDefaultRepresentationClass()
829 return java.io.InputStream.class;
833 * XXX - Currently returns <code>java.io.InputStream</code>.
835 public final String getDefaultRepresentationClassAsString()
837 return getDefaultRepresentationClass().getName();
841 * Creates a <code>Reader</code> for a given <code>Transferable</code>.
843 * If the representation class is a (subclass of) <code>Reader</code>
844 * then an instance of the representation class is returned. If the
845 * representatation class is a <code>String</code> then a
846 * <code>StringReader</code> is returned. And if the representation class
847 * is a (subclass of) <code>InputStream</code> and the primary MIME type
848 * is "text" then a <code>InputStreamReader</code> for the correct charset
849 * encoding is returned.
851 * @param transferable The <code>Transferable</code> for which a text
852 * <code>Reader</code> is requested.
854 * @exception IllegalArgumentException If the representation class is not one
855 * of the seven listed above or the Transferable has null data.
856 * @exception NullPointerException If the Transferable is null.
857 * @exception UnsupportedFlavorException when the transferable doesn't
858 * support this <code>DataFlavor</code>. Or if the representable class
859 * isn't a (subclass of) <code>Reader</code>, <code>String</code>,
860 * <code>InputStream</code> and/or the primary MIME type isn't "text".
861 * @exception IOException when any IOException occurs.
862 * @exception UnsupportedEncodingException if the "charset" isn't supported
863 * on this platform.
865 public Reader getReaderForText(Transferable transferable)
866 throws UnsupportedFlavorException, IOException
868 if (!transferable.isDataFlavorSupported(this))
869 throw new UnsupportedFlavorException(this);
871 if (Reader.class.isAssignableFrom(representationClass))
872 return (Reader)transferable.getTransferData(this);
874 if (String.class.isAssignableFrom(representationClass))
875 return new StringReader((String)transferable.getTransferData(this));
877 if (InputStream.class.isAssignableFrom(representationClass)
878 && "text".equals(getPrimaryType()))
880 InputStream in = (InputStream)transferable.getTransferData(this);
881 String encoding = getParameter("charset");
882 if (encoding == null)
883 encoding = "us-ascii";
884 return new InputStreamReader(in, encoding);
887 throw new UnsupportedFlavorException(this);
891 * Returns whether the representation class for this DataFlavor is
892 * @see java.nio.ByteBuffer or a subclass thereof.
894 * @since 1.4
896 public boolean isRepresentationClassByteBuffer()
898 return ByteBuffer.class.isAssignableFrom(representationClass);
902 * Returns whether the representation class for this DataFlavor is
903 * @see java.nio.CharBuffer or a subclass thereof.
905 * @since 1.4
907 public boolean isRepresentationClassCharBuffer()
909 return CharBuffer.class.isAssignableFrom(representationClass);
913 * Returns whether the representation class for this DataFlavor is
914 * @see java.io.Reader or a subclass thereof.
916 * @since 1.4
918 public boolean isRepresentationClassReader()
920 return Reader.class.isAssignableFrom(representationClass);
924 * Returns whether this <code>DataFlavor</code> is a valid text flavor for
925 * this implementation of the Java platform. Only flavors equivalent to
926 * <code>DataFlavor.stringFlavor</code> and <code>DataFlavor</code>s with
927 * a primary MIME type of "text" can be valid text flavors.
928 * <p>
929 * If this flavor supports the charset parameter, it must be equivalent to
930 * <code>DataFlavor.stringFlavor</code>, or its representation must be
931 * <code>java.io.Reader</code>, <code>java.lang.String</code>,
932 * <code>java.nio.CharBuffer</code>, <code>java.io.InputStream</code> or
933 * <code>java.nio.ByteBuffer</code>,
934 * If the representation is <code>java.io.InputStream</code> or
935 * <code>java.nio.ByteBuffer</code>, then this flavor's <code>charset</code>
936 * parameter must be supported by this implementation of the Java platform.
937 * If a charset is not specified, then the platform default charset, which
938 * is always supported, is assumed.
939 * <p>
940 * If this flavor does not support the charset parameter, its
941 * representation must be <code>java.io.InputStream</code>,
942 * <code>java.nio.ByteBuffer</code>.
943 * <p>
944 * See <code>selectBestTextFlavor</code> for a list of text flavors which
945 * support the charset parameter.
947 * @return <code>true</code> if this <code>DataFlavor</code> is a valid
948 * text flavor as described above; <code>false</code> otherwise
949 * @see #selectBestTextFlavor
950 * @since 1.4
952 public boolean isFlavorTextType() {
953 // FIXME: I'm not 100% sure if this implementation does the same like sun's does
954 if(equals(DataFlavor.stringFlavor) || getPrimaryType().equals("text"))
956 String charset = getParameter("charset");
957 Class c = getRepresentationClass();
958 if(charset != null)
960 if(Reader.class.isAssignableFrom(c)
961 || CharBuffer.class.isAssignableFrom(c)
962 || String.class.isAssignableFrom(c))
964 return true;
966 else if(InputStream.class.isAssignableFrom(c)
967 || ByteBuffer.class.isAssignableFrom(c))
969 return Charset.isSupported(charset);
972 else if(InputStream.class.isAssignableFrom(c)
973 || ByteBuffer.class.isAssignableFrom(c))
975 return true;
978 return false;
980 } // class DataFlavor