Get Clipboard String Encoding (java)

2019-07-17 00:43发布

问题:

I was wondering if theres a way to use Java to get the encoding type of a string on the clipboard. (I'd give more details but the question is fairly straight forward).

Ex. I go into a unicode program, copy text, use the Java program to decipher it, and the java program spits out "UTF-16"

回答1:

To access the clipboard, you can use the awt datatransfer classes. To detect the charset, you can use the CharsetDetector from ICU project.

Here is the code :

public static String getClipboardCharset () throws UnsupportedCharsetException, UnsupportedFlavorException, IOException {
    String clipText = null;
    final Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    final Transferable contents = clipboard.getContents(null);
    if ((contents != null) && contents.isDataFlavorSupported(DataFlavor.stringFlavor))
        clipText = (String) contents.getTransferData(DataFlavor.stringFlavor);

    if (contents!=null && clipText!=null) {
        final CharsetDetector cd = new CharsetDetector();
        cd.setText(clipText.getBytes());
        final CharsetMatch cm = cd.detect();

        if (cm != null)
            return cm.getName();
    }

    throw new UnsupportedCharsetException("Unknown");
}

Here are the imports needed :

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.nio.charset.UnsupportedCharsetException;

import com.ibm.icu.text.CharsetDetector;
import com.ibm.icu.text.CharsetMatch;