Created
July 27, 2018 21:02
-
-
Save swedishborgie/3220d4da62cc74e05ff22b88390ee39c to your computer and use it in GitHub Desktop.
Reproducing Copy-Paste issue from Firefox to Java on Ubuntu
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.datatransfer.UnsupportedFlavorException; | |
import java.io.*; | |
import java.awt.Toolkit; | |
import java.awt.datatransfer.Clipboard; | |
import java.awt.datatransfer.DataFlavor; | |
import java.util.Arrays; | |
import java.util.Optional; | |
public class Main { | |
public static void main(String[] args) throws IOException, UnsupportedFlavorException { | |
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); | |
Optional<DataFlavor> flavor = Arrays.stream(clipboard.getAvailableDataFlavors()) | |
.filter((flv) -> "java.io.Reader".equals(flv.getParameter("class"))) | |
.filter((flv) -> "text/html".equals(flv.getHumanPresentableName())) | |
.findFirst(); | |
if(!flavor.isPresent()) { | |
throw new IOException("No Reader containing text/html found."); | |
} | |
try (PrintWriter writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream("test.txt"), "UTF-8")); | |
BufferedReader reader = new BufferedReader((Reader) clipboard.getData(flavor.get()))) { | |
String line; | |
while ((line = reader.readLine()) != null) { | |
writer.println(line); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment