Revision: 5155 http://sourceforge.net/p/tigervnc/code/5155 Author: bphinz Date: 2014-01-11 23:07:42 +0000 (Sat, 11 Jan 2014) Log Message: ----------- r5138 did not completely resolve the problem with clipboard data consuming too much heap space. Large amounts of clipboard data could still cause the heap size to grow to huge sizes. This patch tries to address the problem by opening a Reader to the underlying IO stream and then reading only up to MaxCutText characters. The garbage collector is invoked manually rather than waiting for the JVM to do it in order to prevent the heap size from growing in between JVM invoked garbage collections.
Revision Links: -------------- http://sourceforge.net/p/tigervnc/code/5138 Modified Paths: -------------- trunk/java/com/tigervnc/vncviewer/ClipboardDialog.java trunk/java/com/tigervnc/vncviewer/DesktopWindow.java trunk/java/com/tigervnc/vncviewer/VncViewer.java Modified: trunk/java/com/tigervnc/vncviewer/ClipboardDialog.java =================================================================== --- trunk/java/com/tigervnc/vncviewer/ClipboardDialog.java 2014-01-10 04:08:20 UTC (rev 5154) +++ trunk/java/com/tigervnc/vncviewer/ClipboardDialog.java 2014-01-11 23:07:42 UTC (rev 5155) @@ -57,6 +57,11 @@ pack(); } + public boolean compareContentsTo(String str) { + return str.equals(textArea.getText()); + + } + public void setContents(String str) { textArea.setText(str); } Modified: trunk/java/com/tigervnc/vncviewer/DesktopWindow.java =================================================================== --- trunk/java/com/tigervnc/vncviewer/DesktopWindow.java 2014-01-10 04:08:20 UTC (rev 5154) +++ trunk/java/com/tigervnc/vncviewer/DesktopWindow.java 2014-01-11 23:07:42 UTC (rev 5155) @@ -35,6 +35,8 @@ import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.Transferable; import java.awt.datatransfer.Clipboard; +import java.io.BufferedReader; +import java.nio.CharBuffer; import javax.swing.*; import com.tigervnc.rfb.*; @@ -362,28 +364,28 @@ try { if (sm != null) sm.checkSystemClipboardAccess(); Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard(); - if (cb == null) return; - Transferable t = cb.getContents(null); - if ((t != null) && t.isDataFlavorSupported(DataFlavor.stringFlavor)) { - try { - String newContents = new String(""); - if (t.getTransferData(DataFlavor.stringFlavor) != null) { - int len = Math.min(cc.viewer.maxCutText.getValue(), - ((String)t.getTransferData(DataFlavor.stringFlavor)).length()); - newContents = - ((String)t.getTransferData(DataFlavor.stringFlavor)).substring(0, len); - } - if (!newContents.equals(cc.clipboardDialog.getContents())) { - if (cc.viewer.sendClipboard.getValue()) - cc.writeClientCutText(newContents, newContents.length()); - cc.clipboardDialog.setContents(newContents); - } - } catch(java.lang.Exception e) { - vlog.debug("Exception getting clipboard data: " + e.getMessage()); + if (cb != null) { + Transferable t = cb.getContents(null); + if (t == null) return; + DataFlavor flavor = + DataFlavor.selectBestTextFlavor(t.getTransferDataFlavors()); + if (flavor == null) return; + BufferedReader br = new BufferedReader(flavor.getReaderForText(t)); + CharBuffer cbuf = + CharBuffer.allocate(VncViewer.maxCutText.getValue()); + br.read(cbuf); + cbuf.flip(); + String newContents = cbuf.toString(); + if (!cc.clipboardDialog.compareContentsTo(newContents)) { + cc.clipboardDialog.setContents(newContents); + if (cc.viewer.sendClipboard.getValue()) + cc.writeClientCutText(newContents, newContents.length()); } + br.close(); + System.gc(); } - } catch(SecurityException e) { - vlog.debug("Cannot access the system clipboard"); + } catch(java.lang.Exception e) { + vlog.debug("Exception getting clipboard data: " + e.getMessage()); } } Modified: trunk/java/com/tigervnc/vncviewer/VncViewer.java =================================================================== --- trunk/java/com/tigervnc/vncviewer/VncViewer.java 2014-01-10 04:08:20 UTC (rev 5154) +++ trunk/java/com/tigervnc/vncviewer/VncViewer.java 2014-01-11 23:07:42 UTC (rev 5155) @@ -561,7 +561,7 @@ = new BoolParameter("SendClipboard", "Send clipboard changes to the server", true); - IntParameter maxCutText + static IntParameter maxCutText = new IntParameter("MaxCutText", "Maximum permitted length of an outgoing clipboard update", 262144); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. ------------------------------------------------------------------------------ CenturyLink Cloud: The Leader in Enterprise Cloud Services. Learn Why More Businesses Are Choosing CenturyLink Cloud For Critical Workloads, Development Environments & Everything In Between. Get a Quote or Start a Free Trial Today. http://pubads.g.doubleclick.net/gampad/clk?id=119420431&iu=/4140/ostg.clktrk _______________________________________________ Tigervnc-commits mailing list Tigervnc-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/tigervnc-commits