[r33x] Bug 259107 - [console] Console Deadlock when too much information written
diff --git a/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/IOConsolePartitioner.java b/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/IOConsolePartitioner.java
index 10e7744..3603fb2 100644
--- a/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/IOConsolePartitioner.java
+++ b/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/IOConsolePartitioner.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2006 IBM Corporation and others.
+ * Copyright (c) 2000, 2009 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -29,6 +29,7 @@
 import org.eclipse.jface.text.ITypedRegion;
 import org.eclipse.jface.text.Region;
 import org.eclipse.swt.custom.StyleRange;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.console.ConsolePlugin;
 import org.eclipse.ui.console.IConsoleDocumentPartitioner;
 import org.eclipse.ui.console.IOConsole;
@@ -461,11 +462,19 @@
                 }
 			}
             
-            if (fBuffer > 160000) { 
-                try {
-                    pendingPartitions.wait();
-                } catch (InterruptedException e) {
-                }
+            if (fBuffer > 160000) {
+            	if(Display.getCurrent() == null){
+					try {
+						pendingPartitions.wait();
+					} catch (InterruptedException e) {
+					}
+            	} else {
+					/*
+					 * if we are in UI thread we cannot lock it, so process
+					 * queued output.
+					 */
+            		processQueue();
+            	}
             }
 		}
 	}
@@ -505,47 +514,7 @@
          * @see org.eclipse.core.internal.jobs.InternalJob#run(org.eclipse.core.runtime.IProgressMonitor)
          */
         public IStatus runInUIThread(IProgressMonitor monitor) {
-        	synchronized (overflowLock) {
-        		ArrayList pendingCopy = new ArrayList();
-        		StringBuffer buffer = null;
-        		boolean consoleClosed = false;
-        		while (pendingPartitions.size() > 0) {
-        			synchronized(pendingPartitions) {
-        				pendingCopy.addAll(pendingPartitions);
-        				pendingPartitions.clear();
-        				fBuffer = 0;
-        				pendingPartitions.notifyAll();
-        			}
-
-        			buffer = new StringBuffer();
-        			for (Iterator i = pendingCopy.iterator(); i.hasNext(); ) {
-        				PendingPartition pp = (PendingPartition) i.next();
-        				if (pp != consoleClosedPartition) { 
-        					buffer.append(pp.text);
-        				} else {
-        					consoleClosed = true;
-        				}
-        			}
-        		}
-
-        		if (connected) {
-        			setUpdateInProgress(true);
-        			updatePartitions = pendingCopy;
-        			firstOffset = document.getLength();
-        			try {
-        				document.replace(firstOffset, 0, buffer.toString());
-        			} catch (BadLocationException e) {
-        			}
-        			updatePartitions = null;
-        			setUpdateInProgress(false);
-        		}
-        		if (consoleClosed) {
-        			console.partitionerFinished();
-        		}
-        		checkBufferSize();
-
-        	}
-
+        	processQueue();
         	return Status.OK_STATUS;
         }        
 		
@@ -563,7 +532,55 @@
 	}
 
  
-    
+	void processQueue() {
+    	synchronized (overflowLock) {
+    		ArrayList pendingCopy = new ArrayList();
+    		StringBuffer buffer = null;
+    		boolean consoleClosed = false;
+    		while (pendingPartitions.size() > 0) {
+    			synchronized(pendingPartitions) {
+    				pendingCopy.addAll(pendingPartitions);
+    				pendingPartitions.clear();
+    				fBuffer = 0;
+    				pendingPartitions.notifyAll();
+    			}
+    			// determine buffer size
+    			int size = 0;
+    			for (Iterator i = pendingCopy.iterator(); i.hasNext(); ) {
+    				PendingPartition pp = (PendingPartition) i.next();
+    				if (pp != consoleClosedPartition) { 
+    					size+= pp.text.length();
+    				} 
+    			}
+    			buffer = new StringBuffer(size);
+    			for (Iterator i = pendingCopy.iterator(); i.hasNext(); ) {
+    				PendingPartition pp = (PendingPartition) i.next();
+    				if (pp != consoleClosedPartition) { 
+    					buffer.append(pp.text);
+    				} else {
+    					consoleClosed = true;
+    				}
+    			}
+    		}
+
+    		if (connected) {
+    			setUpdateInProgress(true);
+    			updatePartitions = pendingCopy;
+    			firstOffset = document.getLength();
+    			try {
+    				document.replace(firstOffset, 0, buffer.toString());
+    			} catch (BadLocationException e) {
+    			}
+    			updatePartitions = null;
+    			setUpdateInProgress(false);
+    		}
+    		if (consoleClosed) {
+    			console.partitionerFinished();
+    		}
+    		checkBufferSize();
+    	}
+
+	}