JDK-4283047 : getReaderForText() throws IllegalArgumentException for PlainText dataflavor.
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.3.0
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 1999-10-20
  • Updated: 1999-12-06
  • Resolved: 1999-11-06
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.
Other
1.3.0 kestrelFixed
Related Reports
Relates :  
Relates :  
Description
IllegalArgumentException is thrown when getReaderForText()  is called on the plaintext flavor by passing a StringSelection transferable object. 

The getReaderForText() method internally checks if the object returned by the getTransferData(DataFlavor) called on a transferable object is an instance of String or
InputStream. But the StringSelection object returns a StringReader when the getTransferData(DataFlavor) is called by passing the plaintext flavor . And since
StringReader is not an instance of String or InputStream, the getReaderForText() method throws the IllrgalArgumentException. 

This exception is not thrown when a user defined transferable object is used. In the user defined transferable object the getTransferableObject(DataFlavor)
returns a ByteArrayInputStream object when called by passing a plaintext flavor. 
As the ByteArrayInputStream is an instance of InputStream, the getReaderForText() method doesn't throw the exception. 

In Windows:  When getReaderForText()  is called on the plaintext flavor by passing a StringSelection transferable object, the IllegalArgumentException is thrown. 

In Solaris:  When getReaderForText()  is called on the plaintext flavor by passing a StringSelection transferable object, the IllegalArgumentException is thrown. 

Testcase that reproduces this bug: 

1)getReaderExample.java  : This is a sample test case which contains a inner class(TextTransferable) that implements the Transferable interface. 

To Run the sample program : 

1) Run the getReaderExample.class 
2) Check the output at the terminal. 

Note: Run the test case using StringSelection object  as well as  TextTransferable object ( by commenting and uncommenting the necessary line in the
getReaderExample() constructor ). 
  

Suggestion: 

The getTransferData() of the StringSelection class should return an object which is an instance of  InputStream, instead of returning the StringReader object. 

Code taken from java.awt.datatransfer.StringSelection 

---------------------- 
--------------------- 
--------------------- 

public synchronized Object getTransferData(DataFlavor dataflavor) 
        throws UnsupportedFlavorException, IOException 
    { 
        if(dataflavor.equals(flavors[0])) 
            return data; 
        if(dataflavor.equals(flavors[1])) 
            return new StringReader(data); 

            /*****************************************************************/ 
            /** Instead of returning the StringReader, an ByteArrayInputStream can be returned **/ 

                byte[] textBytes = null; 
                String encoding = dataflavor.getParameter("charset"); 

                if (encoding == null) { 
                    textBytes = text.getBytes(); 
                } else { 
                    textBytes = text.getBytes(encoding); 
                } 

                ByteArrayInputStream sbis = new ByteArrayInputStream(textBytes); 
                return sbis; 

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

        else 
            throw new UnsupportedFlavorException(dataflavor); 
    } 

###@###.### 1999-10-21

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: kestrel FIXED IN: kestrel INTEGRATED IN: kestrel VERIFIED IN: kestrel
14-06-2004

EVALUATION Name: dtC58261 Date: 10/22/99 ###@###.###: This bug is related to 4147507 StringSelection plainTextFlavor returns a StringReader which was decided not to be fixed in Kestrel. The problem is that in DataFlavor's getReaderForText we're checking for InputStream and after that throwing NPE. But actually representation class for plainTextFlavor is now StringReader, not InputStream (which is the essence of bug 4147507). As I mentioned above we can't fix (either fix the doc or come back to InputStream) 4147507 in Kestrel but I think we can fix getReaderForText to handle this situation. I'd suggest in case of representation class being StringReader just return it itself as a result of getReaderForText and mention this in docs. ======================================================================
11-06-2004