Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

getting clipboard content as rawdata

Thread view: 
oliv@linuxmail.org - 22 Mar 2007 17:30 GMT
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
Thomas Fritsch - 22 Mar 2007 20:23 GMT
> I am within a java application (swing), and
> I would like to get the content of user clipboard as rawdata (binary
[quoted text clipped - 4 lines]
> 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[i]);
}
// 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...

Signature

Thomas

Thomas Fritsch - 23 Mar 2007 09:22 GMT
> // If the clipboard contains part of a Word-doc or an Excel-Sheet,
> // you'll get dozens of different flavors here, for example:
[quoted text clipped - 7 lines]
> InputStream stream = (InputStream) transferable.getTransferData(flavor);
> // ... read the bytes from the stream...

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.)

Signature

Thomas



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.