JDK-8134612 : clipboard.getData(dataFlavor) can throw UnsupportedFlavorException for registered data flavor
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 9
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2015-08-27
  • Updated: 2017-01-05
  • Resolved: 2016-12-16
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9 b151Fixed
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Relates :  
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


Comments
This bug does not block JDK-8133719. JDK-8133719 should be fixed separately to avoid java.lang.InternalError. See the workaround given by Alex above. This test needs to be fixed to address - java.awt.datatransfer.UnsupportedFlavorException: text/plain at sun.awt.datatransfer.SunClipboard.getData(java.desktop@9-internal/SunClipboard.java:215) at ConstructFlavoredObjectTest.main(ConstructFlavoredObjectTest.java:80)
18-11-2016

RULE "java/awt/datatransfer/ConstructFlavoredObjectTest/ConstructFlavoredObjectTest.java" Exception java.io.IOException: Font transform has NaN position
13-09-2016

BTW on OS X 10.11 and jdk9 b130: RULE "java/awt/datatransfer/ConstructFlavoredObjectTest/ConstructFlavoredObjectTest.java" Exception java.awt.datatransfer.UnsupportedFlavorException: text/plain and the stack as in description: java.awt.datatransfer.UnsupportedFlavorException: text/plain at sun.awt.datatransfer.SunClipboard.getData(java.desktop@9-ea/SunClipboard.java:215) at ConstructFlavoredObjectTest.main(ConstructFlavoredObjectTest.java:58)
05-08-2016

Try to reproduce the issue on Mac OS X when something is copied to the clipboard.
02-06-2016

This bug is "In Progress" status now, so re-targeting back to Fix Version 9. If you are not working on this bug fix anymore, please update the bug from "In Progress" to just "Open"
13-11-2015