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

clipboard.getData(dataFlavor) can throw UnsupportedFlavorException for registered data flavor

    XMLWordPrintable

    Details

    • Subcomponent:
    • Resolved In Build:
      b151

      Description

      Run the following sample:
      ------------------------------
      import java.awt.Toolkit;
      import java.awt.datatransfer.Clipboard;
      import java.awt.datatransfer.DataFlavor;
      import java.awt.datatransfer.SystemFlavorMap;
      import java.io.IOException;
      import java.io.Reader;
      import javax.swing.JLabel;
      import javax.swing.TransferHandler;

      public class ConstructFlavoredObjectTest {

          private static final String TEST_MIME_TYPE = "text/plain;class="
                  + MyStringReader.class.getName();

          public static void main(String[] args) throws Exception {

              final DataFlavor dataFlavor = new DataFlavor(TEST_MIME_TYPE);
              SystemFlavorMap systemFlavorMap = (SystemFlavorMap) SystemFlavorMap.
                      getDefaultFlavorMap();
              systemFlavorMap.addUnencodedNativeForFlavor(dataFlavor, "TEXT");
              systemFlavorMap.addFlavorForUnencodedNative("TEXT", dataFlavor);

              TransferHandler transferHandler = new TransferHandler("Test Handler");

              Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
              transferHandler.exportToClipboard(new JLabel("Test"), clipboard,
                      TransferHandler.COPY);

              Object clipboardData = clipboard.getData(dataFlavor);
              System.out.println("clipboardData: " + clipboardData);
              if (!(clipboardData instanceof MyStringReader)) {
                  throw new RuntimeException("Wrong clipboard data!");
              }
          }

          public static class MyStringReader extends Reader {

              public MyStringReader(Reader reader) {
              }

              @Override
              public int read(char[] cbuf, int off, int len) throws IOException {
                  throw new UnsupportedOperationException("Not supported yet.");
              }

              @Override
              public void close() throws IOException {
                  throw new UnsupportedOperationException("Not supported yet.");
              }
          }
      }
      ------------------------------

      The following exception is thrown from time to time:

      Exception in thread "main" java.awt.datatransfer.UnsupportedFlavorException: text/plain
              at sun.awt.datatransfer.SunClipboard.getData(SunClipboard.java:215)
              at datatransfer.ConstructFlavoredObjectTestPrev.main(ConstructFlavoredObjectTestPrev.java:30)
      Java Result: 1

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              aghaisas Ajit Ghaisas
              Reporter:
              alexsch Alexandr Scherbatiy
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: