getting clipboard content as rawdata

O

oliv

I am within a java application (swing), and
I would like to get the content of user clipboard as rawdata (binary
byte array) without trying to figure out what flavor it is. I just
need to get the bytes and post them to a server.
(It can part of word doc or xls - server will try to convert that into
an image)
Does that question makes any sens (since I haven't seen a way to do
that within java.awt.datatransfer api) ?


thanks
 
T

Thomas Fritsch

I am within a java application (swing), and
I would like to get the content of user clipboard as rawdata (binary
byte array) without trying to figure out what flavor it is. I just
need to get the bytes and post them to a server.
(It can part of word doc or xls - server will try to convert that into
an image)
Does that question makes any sens (since I haven't seen a way to do
that within java.awt.datatransfer api) ?

// get the clipboard contents
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable transferable = clipboard.getContents(null);

// dump out all available flavors
DataFlavor[] flavors = transferable.getTransferDataFlavors();
for (int i = 0; i < flavors.length; i++) {
System.out.println(flavors);
}
// If the clipboard contains part of a Word-doc or an Excel-Sheet,
// you'll get dozens of different flavors here, for example:
// DataFlavor[mimetype=text/rtf;representationclass=java.io.InputStream]
// DataFlavor[mimetype=text/rtf;representationclass=[B]
// DataFlavor[mimetype=text/plain;representationclass=[B;charset=Cp1252]

// get the contents in one of the flavors (RTF InputStream)
DataFlavor flavor = new
DataFlavor("text/rtf;class=java.io.InputStream");
InputStream stream = (InputStream) transferable.getTransferData(flavor);
// ... read the bytes from the stream...[/B]
 
T

Thomas Fritsch

Thomas said:
// If the clipboard contains part of a Word-doc or an Excel-Sheet,
// you'll get dozens of different flavors here, for example:
// DataFlavor[mimetype=text/rtf;representationclass=java.io.InputStream]
// DataFlavor[mimetype=text/rtf;representationclass=[B]
// DataFlavor[mimetype=text/plain;representationclass=[B;charset=Cp1252]

// get the contents in one of the flavors (RTF InputStream)
DataFlavor flavor = new
DataFlavor("text/rtf;class=java.io.InputStream");
InputStream stream = (InputStream) transferable.getTransferData(flavor);
// ... read the bytes from the stream...
[/B]

It is especially cumbersome to get the byte[] and char[] flavors. You
have to specify them as "[B" or "[C", respectively. For example:
DataFlavor flavor = new
DataFlavor("text/plain;class=\"[B\";charset=Cp1252");
byte[] b = (byte[]) transferable.getTransferData(flavor);
(Ask Sun why they didn't write about that in the API docs of DataFlavor.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top