/*
 * Copyright 2003-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package groovy.ui;

import groovy.lang.Closure;

import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.PrintStream;

/**
 * Intercepts System.out. Implementation helper for Console.groovy.
 */
class SystemOutputInterceptor extends FilterOutputStream {

	private Closure callback;

	/**
	 * Constructor
	 * 
	 * @param callback
	 *            accepts a string to be sent to std out and returns a Boolean.
	 *            If the return value is true, output will be sent to
	 *            System.out, otherwise it will not.
	 */
	public SystemOutputInterceptor(Closure callback) {
		super(System.out);
		this.callback = callback;
	}

	/**
	 * Starts intercepting System.out
	 */
	public void start() {
		System.setOut(new PrintStream(this));
	}

	/**
	 * Stops intercepting System.out, sending output to whereever it was
	 * going when this interceptor was created.
	 */
	public void stop() {
		System.setOut((PrintStream) out);
	}

	/**
	 * Intercepts output - moret common case of byte[]
	 */
	public void write(byte[] b, int off, int len) throws IOException {
		Boolean result = (Boolean) callback.call(new String(b, off, len));
		if (result.booleanValue()) {
			out.write(b, off, len);
		}
	}

	/**
	 * Intercepts output - single characters
	 */
	public void write(int b) throws IOException {
		Boolean result = (Boolean) callback.call(String.valueOf((char) b));
		if (result.booleanValue()) {
			out.write(b);
		}
	}
}

@@ -827,16 +827,36 @@
 groovy.%0A
+ *%0A * @version $Id$%0A
  */%0Aclas
@@ -911,17 +911,20 @@
 ream %7B%0A%0A
-%09
+    
 private 
@@ -942,22 +942,28 @@
 lback;%0A%0A
-%09/**%0A%09
+    /**%0A    
  * Const
@@ -973,14 +973,20 @@
 tor%0A
-%09 * %0A%09
+     * %0A    
  * @
@@ -1000,17 +1000,20 @@
 allback%0A
-%09
+    
  *      
@@ -1080,17 +1080,20 @@
 oolean.%0A
-%09
+    
  *      
@@ -1150,17 +1150,20 @@
 sent to%0A
-%09
+    
  *      
@@ -1203,22 +1203,28 @@
 ll not.%0A
-%09 */%0A%09
+     */%0A    
 public S
@@ -1246,16 +1246,22 @@
 rceptor(
+final 
 Closure 
@@ -1272,18 +1272,24 @@
 back) %7B%0A
-%09%09
+        
 super(Sy
@@ -1299,18 +1299,75 @@
 m.out);%0A
-%09%09
+        %0A        assert callback != null;%0A        %0A        
 this.cal
@@ -1384,26 +1384,35 @@
 llback;%0A
-%09%7D%0A%0A%09/**%0A%09
+    %7D%0A%0A    /**%0A    
  * Start
@@ -1437,22 +1437,28 @@
 tem.out%0A
-%09 */%0A%09
+     */%0A    
 public v
@@ -1467,26 +1467,32 @@
 d start() %7B%0A
-%09%09
+        
 System.setOu
@@ -1517,26 +1517,35 @@
 this));%0A
-%09%7D%0A%0A%09/**%0A%09
+    %7D%0A%0A    /**%0A    
  * Stops
@@ -1605,17 +1605,20 @@
  it was%0A
-%09
+    
  * going
@@ -1653,22 +1653,28 @@
 reated.%0A
-%09 */%0A%09
+     */%0A    
 public v
@@ -1686,18 +1686,24 @@
 top() %7B%0A
-%09%09
+        
 System.s
@@ -1728,26 +1728,35 @@
 ) out);%0A
-%09%7D%0A%0A%09/**%0A%09
+    %7D%0A%0A    /**%0A    
  * Inter
@@ -1798,22 +1798,28 @@
  byte%5B%5D%0A
-%09 */%0A%09
+     */%0A    
 public v
@@ -1869,34 +1869,40 @@
 s IOException %7B%0A
-%09%09
+        
 Boolean result =
@@ -1948,26 +1948,32 @@
 off, len));%0A
-%09%09
+        
 if (result.b
@@ -1981,35 +1981,44 @@
 oleanValue()) %7B%0A
-%09%09%09
+            
 out.write(b, off
@@ -2029,22 +2029,37 @@
 n);%0A
-%09%09%7D%0A%09%7D%0A%0A%09/**%0A%09
+        %7D%0A    %7D%0A%0A    /**%0A    
  * I
@@ -2099,14 +2099,20 @@
 ers%0A
-%09 */%0A%09
+     */%0A    
 publ
@@ -2153,18 +2153,24 @@
 ption %7B%0A
-%09%09
+        
 Boolean 
@@ -2229,18 +2229,24 @@
 r) b));%0A
-%09%09
+        
 if (resu
@@ -2270,11 +2270,20 @@
 ) %7B%0A
-%09%09%09
+            
 out.
@@ -2296,13 +2296,22 @@
 b);%0A
-%09%09%7D%0A%09
+        %7D%0A    
 %7D%0A%7D%0A

Fix formatting, assert callback is non-null