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

Repeated attempts to retrieve data from Clipboard fail

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.3.1
    • 1.4.0
    • client-libs
    • None
    • ladybird
    • x86
    • windows_nt

        ###@###.###

        The following test reproduces the problem:
        --------------------------------------------------------
        import java.awt.*;
        import java.awt.event.*;
        import java.awt.datatransfer.DataFlavor;
        import java.awt.datatransfer.Transferable;
        import java.io.InputStream;
        import java.io.InputStreamReader;

        public class ClipboardTest extends WindowAdapter {

            final Frame frame = new Frame();
            final Panel panel = new TestPanel();
            
            public static void main(String[] args) {
                ClipboardTest test = new ClipboardTest();
            }
             
            public ClipboardTest() {
                frame.setLayout(new FlowLayout());
                frame.add(panel);
                frame.pack();
                frame.addWindowListener(this);
                frame.setVisible(true);
            }
             
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        }
         
        class TestPanel extends Panel implements ActionListener {

            static final DataFlavor textFlavor =
                new DataFlavor("text/plain; class=java.io.InputStream", "Text");
                
            final Button button1 = new Button("Text Flavor");
            final Button button2 = new Button("String Flavor");
            final TextField textfield = new TextField();
            
            static Transferable transferable = null;
            
            TestPanel() {
                textfield.setEditable(false);
                button1.addActionListener(this);
                button2.addActionListener(this);
                
                setLayout(new GridLayout(3,1));
                add(textfield);
                add(button1);
                add(button2);
            }
             
            public void actionPerformed(ActionEvent e) {
                if (transferable == null) {
                    transferable =
                        Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
                }
                if (e.getSource() == button1) {
                    try {
                        InputStream stream = (InputStream)transferable.getTransferData(textFlavor);
                        InputStreamReader reader = new InputStreamReader(stream);
                        StringBuffer buffer = new StringBuffer();
                        for (int c = reader.read(); c != -1; c = reader.read()) {
                            buffer.append((char)c);
                        }
                        reader.close();
                        textfield.setText(buffer.toString());
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                } else if (e.getSource() == button2) {
                    try {
                        String string = (String)transferable.getTransferData(DataFlavor.stringFlavor);
                        textfield.setText(string);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }
        --------------------------------------------------------

        Compile and run the test case.
        The frame appears with a text field and two buttons.
        Click both buttons several times.
        Note that the text can be retrieved in text flavor
        only once, while string flavor works correctly.
        -------------------------------------------------------
        Z:\bug\newbug>Z:\jdk1.4\win32\bin\java_g -version
        java version "1.4.0beta"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0beta-b33)
        Java HotSpot(TM) Client VM (build B22-debug, mixed mode)
        -------------------------------------------------------

              dassunw Das Das (Inactive)
              son Oleg Sukhodolsky (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: