getting clipboard content as rawdata

Discussion in 'Java' started by oliv@linuxmail.org, Mar 22, 2007.

  1. Guest

    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
     
    , Mar 22, 2007
    #1
    1. Advertisements

  2. wrote:
    > 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=
    // 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...

    --
    Thomas
     
    Thomas Fritsch, Mar 22, 2007
    #2
    1. Advertisements

  3. Thomas Fritsch wrote:
    > // 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=
    > // 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...
    >

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

    --
    Thomas
     
    Thomas Fritsch, Mar 23, 2007
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marc Van Laer

    Jtable content to system clipboard

    Marc Van Laer, Mar 11, 2005, in forum: Java
    Replies:
    2
    Views:
    6,738
    Marc Van Laer
    Mar 11, 2005
  2. hazz
    Replies:
    6
    Views:
    52,074
    SkyUCHC
    Jun 9, 2010
  3. Replies:
    1
    Views:
    320
    Fredrik Lundh
    Apr 6, 2006
  4. Replies:
    2
    Views:
    956
  5. Dan Bishop
    Replies:
    0
    Views:
    471
    Dan Bishop
    Aug 26, 2008
  6. Replies:
    4
    Views:
    523
    Randy Webb
    Dec 14, 2004
  7. Mahsha

    Clipboard - Copy Image To Clipboard

    Mahsha, Jun 1, 2009, in forum: Javascript
    Replies:
    1
    Views:
    945
    Thomas 'PointedEars' Lahn
    Jun 1, 2009
  8. Mahsha

    Clipboard - Copy Text To Clipboard

    Mahsha, Jun 1, 2009, in forum: Javascript
    Replies:
    3
    Views:
    946
    Thomas 'PointedEars' Lahn
    Jun 1, 2009
Loading...