Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4395415

DataFlavor.selectBestTextFlavor works incorrectly

XMLWordPrintable

    • sparc
    • solaris_7



      Name: sdR10048 Date: 12/06/2000


      The method
      java.awt.datatransfer.DataFlavor.selectBestTextFlavor(DataFlavor[] availableFlavors)
      starts to compare charsets when MIME types are different.
      Say, in case when 'availableFlavors' contains two flavors:
      {
       new DataFlavor("text/plain;charset=UTF-16"),
       new DataFlavor("text/sgml;charset=a")
      }

      it returns DataFlavor("text/plain;charset=UTF-16") but should return
      DataFlavor("text/sgml;charset=a") as javadoc says:
      ---
      For example, "text/sgml" will be selected over "text/html", and
      "text/enriched" will be selected over "text/richtext". If there are
      two or more text flavors with the best MIME subtype in the array,
      their charsets are compared.

      The full javadoc for this method is here:
      ---
      public static final DataFlavor selectBestTextFlavor(DataFlavor[] availableFlavors)

      Selects the best text DataFlavor from an array of DataFlavors. Only
      DataFlavor.stringFlavor, and equivalent flavors, and flavors that have
      a primary MIME type of "text", are considered for selection. If the
      array contains stringFlavor , or an equivalent flavor (a serialized
      java.lang.String ), then this flavor is selected. Flavors having
      "text" as their primary MIME type are sorted by their secondary MIME
      type in the following order: "plain", "calendar", "css", "directory",
      "rfc822-header", "tab-separated-values", "uri-list", "richtext",
      "enriched", "rtf", "html", "xml", "sgml". For example, "text/sgml"
      will be selected over "text/html", and "text/enriched" will be
      selected over "text/richtext". If there are two or more text flavors
      with the best MIME subtype in the array, their charsets are
      compared. Unicode charsets, such as "UTF-16", "UTF-8", "UTF-16BE" and
      "UTF-16LE", are considered best. After them, platform default charset
      is selected. "US-ASCII" is worst. All other charsets are chosen in
      alphabetical order.

      Parameters:
         availableFlavors - an array of available DataFlavors
      Returns:
         the best (highest fidelity) flavor in an encoding supported by this
         implementation of the Java platform, or null if none can be found.
      ---
      Minimized test demonstrating this bug:

      import java.awt.datatransfer.*;

      public class Test {
          public static void main(String[] args) {
              try {
                  DataFlavor[] dfs = {
                      new DataFlavor("text/plain;charset=UTF-16"),
                      new DataFlavor("text/sgml;charset=a")
                  };
                  System.out.println("Best DF: " +
                      DataFlavor.selectBestTextFlavor(dfs));
              } catch (ClassNotFoundException e) {
                  System.out.println("unexpected "+e);
              }
          }
      }
      ---
      Test output:

      ] dsv@falcon ~/tmp
      ] java -version
      java version "1.4.0beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0beta-b40)
      Java HotSpot(TM) Client VM (build 1.4beta-B40, mixed mode)

      ] dsv@falcon ~/tmp
      ] java Test
      Best DF: java.awt.datatransfer.DataFlavor[representationclass=java.io.InputStream;mimetype=text/plain]
      ---
      ======================================================================

            dmendenhsunw David Mendenhall (Inactive)
            dsvsunw Dsv Dsv (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: