0

Hello I'm trying make a simple program for saving and reusing clipboard content but i cannot realize how to get clipboard content as byte array without using casting to some Dataflavor because i need save and set content without changing. how i can achive this result?

Had tried do like that but this working only for text data.

Clipboard cl = Toolkit.getDefaultToolkit().getSystemClipboard();
InputStream s = (InputStream)cl.getData(newDataFlavor("text/rtf;class=java.io.InputStream"));   
Minepolz320
  • 123
  • 4
  • What kind of content are you trying to retrieve? Image data? – VGR May 09 '19 at 18:58
  • @VGR not matter image or text data i need way save them as byte[] – Minepolz320 May 09 '19 at 19:16
  • Representing an image as a byte array requires defining the image format, such as PNG or GIF or JPEG. You can try something like `new DataFlavor("image/png", "PNG Image")` but I doubt it will work, as the underlying native clipboard would need to support it. You can always use `DataFlavor.imageFlavor`, wait for the received Image to load, draw it into a BufferedImage, and use ImageIO to write it to a ByteArrayOutputStream in a format of your choice. – VGR May 09 '19 at 19:27
  • @VGR Thanks for the hint, but this method will only work for images or DataFlavor.stringFlavor only for strings, but what if the clipboard contains data that is not supported by Java. that's why I need to get raw data so that I can work with them https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/windows/classes/sun/awt/windows/WClipboard.java need a method like byte [] getClipboardData (long format) – Minepolz320 May 09 '19 at 19:56
  • Other than text, images, and files, what other types are there? – VGR May 09 '19 at 20:16
  • @VGR For example, this case https://stackoverflow.com/questions/24844971/getting-binary-data-directly-off-the-windows-clipboard – Minepolz320 May 09 '19 at 20:20
  • I see. Have you tried looping through the array returned by [getAvailableDataFlavors()](https://docs.oracle.com/en/java/javase/12/docs/api/java.datatransfer/java/awt/datatransfer/Clipboard.html#getAvailableDataFlavors%28%29) looking for a flavor with a representation class of InputStream or ByteBuffer or `byte[]`? – VGR May 09 '19 at 20:28

0 Answers0