Author: krosenvold
Date: Wed May  4 08:30:59 2011
New Revision: 1099367

URL: http://svn.apache.org/viewvc?rev=1099367&view=rev
Log:
o Tweaked stream output

Added:
    
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/internal/StreamUtils.java
   (with props)
Modified:
    
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/booter/ForkingRunListener.java
    
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/internal/StringUtils.java

Modified: 
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/booter/ForkingRunListener.java
URL: 
http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/booter/ForkingRunListener.java?rev=1099367&r1=1099366&r2=1099367&view=diff
==============================================================================
--- 
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/booter/ForkingRunListener.java
 (original)
+++ 
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/booter/ForkingRunListener.java
 Wed May  4 08:30:59 2011
@@ -19,17 +19,19 @@ package org.apache.maven.surefire.booter
  * under the License.
  */
 
-import java.io.PrintStream;
-import java.util.Enumeration;
-import java.util.Properties;
 import org.apache.maven.surefire.report.ConsoleOutputReceiver;
 import org.apache.maven.surefire.report.DirectConsoleReporter;
 import org.apache.maven.surefire.report.ReportEntry;
 import org.apache.maven.surefire.report.RunListener;
 import org.apache.maven.surefire.report.StackTraceWriter;
 import org.apache.maven.surefire.util.internal.ByteBuffer;
+import org.apache.maven.surefire.util.internal.StreamUtils;
 import org.apache.maven.surefire.util.internal.StringUtils;
 
+import java.io.PrintStream;
+import java.util.Enumeration;
+import java.util.Properties;
+
 /**
  * Encodes the full output of the test run to the stdout stream
  *
@@ -147,7 +149,7 @@ public class ForkingRunListener
                 {
                     value = "null";
                 }
-                target.print( toPropertyString( key, value ) );
+                toPropertyString( key, value );
             }
         }
     }
@@ -155,7 +157,8 @@ public class ForkingRunListener
     public void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
     {
         byte[] header = stdout ? stdOutHeader : stdErrHeader;
-        byte[] content = new byte[buf.length * 6 + 1]; // Unicode escapes can 
be up to 6 times length of regular char. Yuck.
+        byte[] content =
+            new byte[buf.length * 6 + 1]; // Unicode escapes can be up to 6 
times length of regular char. Yuck.
         int i = StringUtils.escapeJavaStyleString( content, 0, buf, off, len );
         content[i++] = (byte) '\n';
 
@@ -199,7 +202,7 @@ public class ForkingRunListener
     public void writeMessage( String message )
     {
         byte[] buf = message.getBytes();
-        ByteBuffer byteBuffer = new ByteBuffer( buf.length * 6);
+        ByteBuffer byteBuffer = new ByteBuffer( buf.length * 6 );
         byteBuffer.append( BOOTERCODE_CONSOLE );
         byteBuffer.comma();
         byteBuffer.append( testSetChannelId );
@@ -212,16 +215,16 @@ public class ForkingRunListener
         target.flush();
     }
 
-    private String toPropertyString( String key, String value )
+    private void toPropertyString( String key, String value )
     {
-        StringBuffer stringBuffer = new StringBuffer();
-        append( stringBuffer, BOOTERCODE_SYSPROPS ).comma( stringBuffer );
-        append( stringBuffer, Integer.toHexString( testSetChannelId.intValue() 
) ).comma( stringBuffer );
-        StringUtils.escapeJavaStyleString( stringBuffer, key );
-        append( stringBuffer, "," );
-        StringUtils.escapeJavaStyleString( stringBuffer, value );
-        stringBuffer.append( "\n" );
-        return stringBuffer.toString();
+        target.write( BOOTERCODE_SYSPROPS );
+        target.write( ',' );
+        StreamUtils.toHex( target, testSetChannelId );
+        target.write( ',' );
+        StringUtils.escapeJavaStyleString( target, key );
+        target.write( ',' );
+        StringUtils.escapeJavaStyleString( target, value );
+        target.write( '\n' );
     }
 
     private String toString( byte operationCode, ReportEntry reportEntry, 
Integer testSetChannelId )

Added: 
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/internal/StreamUtils.java
URL: 
http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/internal/StreamUtils.java?rev=1099367&view=auto
==============================================================================
--- 
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/internal/StreamUtils.java
 (added)
+++ 
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/internal/StreamUtils.java
 Wed May  4 08:30:59 2011
@@ -0,0 +1,70 @@
+package org.apache.maven.surefire.util.internal;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import org.apache.maven.surefire.util.NestedRuntimeException;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+public class StreamUtils
+{
+    public static void toHex( OutputStream target, Integer i )
+    {
+        if ( i != null )
+        {
+            toHex( target, i.intValue() );
+        }
+    }
+
+    /**
+     * Convert the integer to an unsigned number.
+     *
+     * @param target The stream that will receive the encoded value
+     * @param i      the value
+     */
+    public static void toHex( OutputStream target, int i )
+    {
+        byte[] buf = new byte[32];
+        int charPos = 32;
+        int radix = 1 << 4;
+        int mask = radix - 1;
+        do
+        {
+            buf[--charPos] = (byte) digits[i & mask];
+            i >>>= 4;
+        }
+        while ( i != 0 );
+
+        try
+        {
+            target.write( buf, charPos, ( 32 - charPos ) );
+        } catch (IOException e)
+        {
+            throw new NestedRuntimeException(e);
+        }
+    }
+
+    private final static char[] digits =
+        { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 
'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
+            'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 
'z' };
+
+
+}

Propchange: 
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/internal/StreamUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/internal/StringUtils.java
URL: 
http://svn.apache.org/viewvc/maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/internal/StringUtils.java?rev=1099367&r1=1099366&r2=1099367&view=diff
==============================================================================
--- 
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/internal/StringUtils.java
 (original)
+++ 
maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/util/internal/StringUtils.java
 Wed May  4 08:30:59 2011
@@ -20,6 +20,7 @@ package org.apache.maven.surefire.util.i
  */
 
 import java.io.IOException;
+import java.io.PrintStream;
 import java.io.StringWriter;
 import java.io.Writer;
 import java.util.StringTokenizer;
@@ -520,6 +521,100 @@ public class StringUtils
         }
     }
 
+    public static void escapeJavaStyleString( PrintStream out, byte[] str, int 
off, int len )
+    {
+        if ( out == null )
+        {
+            throw new IllegalArgumentException( "The Writer must not be null" 
);
+        }
+        final int inputLength = str.length;
+        if ( str == null || inputLength == 0 )
+        {
+            return;
+        }
+        int outputPos = 0;
+        int end = off + len;
+        for ( int i = off; i < end; i++ )
+        {
+            char ch = (char) str[i];
+
+            // handle unicode
+            if ( ch > 0xfff )
+            {
+                outputPos = writeOut( out, outputPos, "\\u" + hex( ch ) );
+            }
+            else if ( ch > 0xff )
+            {
+                outputPos = writeOut( out, outputPos, "\\u0" + hex( ch ) );
+            }
+            else if ( ch > 0x7f || ch == ',' )
+            {    // Kr - this line modified from commons
+                outputPos = writeOut( out, outputPos, "\\u00" + hex( ch ) );
+            }
+            else if ( ch < 32 )
+            {
+                switch ( ch )
+                {
+                    case '\b':
+                        out.append( '\\' );
+                        out.append( 'b' );
+                        break;
+                    case '\n':
+                        out.append( '\\' );
+                        out.append( 'n' );
+                        break;
+                    case '\t':
+                        out.append( '\\' );
+                        out.append( 't' );
+                        break;
+                    case '\f':
+                        out.append( '\\' );
+                        out.append( 'f' );
+                        break;
+                    case '\r':
+                        out.append( '\\' );
+                        out.append( 'r' );
+                        break;
+                    default:
+                        if ( ch > 0xf )
+                        {
+                            outputPos = writeOut( out, outputPos, "\\u00" + 
hex( ch ) );
+                        }
+                        else
+                        {
+                            outputPos = writeOut( out, outputPos, "\\u000" + 
hex( ch ) );
+                        }
+                        break;
+                }
+            }
+            else
+            {
+                switch ( ch )
+                {
+                    case '\'':
+                        out.append( '\\' );
+                        out.append( '\'' );
+                        break;
+                    case '"':
+                        out.append( '\\' );
+                        out.append( '"' );
+                        break;
+                    case '\\':
+                        out.append( '\\' );
+                        out.append( '\\' );
+                        break;
+                    case '/':
+                        out.append( '\\' );
+                        out.append( '/' );
+                        break;
+                    default:
+                        out.append( ch );
+                        break;
+                }
+            }
+        }
+    }
+
     public static int escapeJavaStyleString( byte[] out, int outoff, byte[] 
str, int off, int len )
     {
         if ( out == null )
@@ -625,6 +720,16 @@ public class StringUtils
         return outputPos;
     }
 
+    private static int writeOut( PrintStream out, int outputPos, final String 
msg )
+    {
+        byte[] bytes = msg.getBytes();
+        for ( int cnt = 0; cnt < bytes.length; cnt++ )
+        {
+            out.write( bytes[cnt] );
+        }
+        return outputPos;
+    }
+
 
     private static int writeOut( byte[] out, int outputPos, final String msg )
     {
@@ -666,5 +771,24 @@ public class StringUtils
             ioe.printStackTrace();
         }
     }
+
+    public static void escapeJavaStyleString( PrintStream target, String str )
+    {
+        if ( str == null )
+        {
+            return;
+        }
+        try
+        {
+            StringWriter writer = new StringWriter( str.length() * 2 );
+            escapeJavaStyleString( writer, str, true );
+            target.append( writer.toString() ); // todo: be bit smarter
+        }
+        catch ( IOException ioe )
+        {
+            // this should never ever happen while writing to a StringWriter
+            ioe.printStackTrace();
+        }
+    }
 }
 


Reply via email to