Coverage Report - org.apache.maven.surefire.util.internal.StringUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
StringUtils
26%
96/360
26%
55/206
11,143
 
 1  
 package org.apache.maven.surefire.util.internal;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *     http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.IOException;
 23  
 import java.io.PrintStream;
 24  
 import java.io.StringWriter;
 25  
 import java.io.Writer;
 26  
 import java.util.StringTokenizer;
 27  
 import org.apache.maven.surefire.util.NestedRuntimeException;
 28  
 
 29  
 /**
 30  
  * <p>Common <code>String</code> manipulation routines.</p>
 31  
  * <p/>
 32  
  * <p>Originally from
 33  
  * <a href="http://jakarta.apache.org/turbine/">Turbine</a> and the
 34  
  * GenerationJavaCore library.</p>
 35  
  *
 36  
  * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
 37  
  * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
 38  
  * @author <a href="mailto:gcoladonato@yahoo.com">Greg Coladonato</a>
 39  
  * @author <a href="mailto:bayard@generationjava.com">Henri Yandell</a>
 40  
  * @author <a href="mailto:ed@codehaus.org">Ed Korthof</a>
 41  
  * @author <a href="mailto:rand_mcneely@yahoo.com">Rand McNeely</a>
 42  
  * @author Stephen Colebourne
 43  
  * @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
 44  
  * @author Holger Krauth
 45  
  * @author <a href="mailto:alex@purpletech.com">Alexander Day Chaffee</a>
 46  
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
 47  
  * @version $Id: StringUtils.java 8001 2009-01-03 13:17:09Z vsiveton $
 48  
  * @noinspection JavaDoc
 49  
  * <p/>
 50  
  * A quick borrow from plexus-utils by Kristian Rosenvold, to restore jdk1.3 compat
 51  
  * Threw away all the unused stuff.
 52  
  * <p/>
 53  
  * NOTE: This class is not part of any api and is public purely for technical reasons !
 54  
  * @since 1.0
 55  
  */
 56  0
 public class StringUtils
 57  
 {
 58  
 
 59  
     // Splitting
 60  
     //--------------------------------------------------------------------------
 61  
 
 62  
     public static String[] split( String text, String separator )
 63  
     {
 64  2
         int max = -1;
 65  
         StringTokenizer tok;
 66  2
         if ( separator == null )
 67  
         {
 68  
             // Null separator means we're using StringTokenizer's default
 69  
             // delimiter, which comprises all whitespace characters.
 70  0
             tok = new StringTokenizer( text );
 71  
         }
 72  
         else
 73  
         {
 74  2
             tok = new StringTokenizer( text, separator );
 75  
         }
 76  
 
 77  2
         int listSize = tok.countTokens();
 78  2
         if ( ( max > 0 ) && ( listSize > max ) )
 79  
         {
 80  0
             listSize = max;
 81  
         }
 82  
 
 83  2
         String[] list = new String[listSize];
 84  2
         int i = 0;
 85  
         int lastTokenBegin;
 86  2
         int lastTokenEnd = 0;
 87  65
         while ( tok.hasMoreTokens() )
 88  
         {
 89  63
             if ( ( max > 0 ) && ( i == listSize - 1 ) )
 90  
             {
 91  
                 // In the situation where we hit the max yet have
 92  
                 // tokens left over in our input, the last list
 93  
                 // element gets all remaining text.
 94  0
                 String endToken = tok.nextToken();
 95  0
                 lastTokenBegin = text.indexOf( endToken, lastTokenEnd );
 96  0
                 list[i] = text.substring( lastTokenBegin );
 97  0
                 break;
 98  
             }
 99  
             else
 100  
             {
 101  63
                 list[i] = tok.nextToken();
 102  63
                 lastTokenBegin = text.indexOf( list[i], lastTokenEnd );
 103  63
                 lastTokenEnd = lastTokenBegin + list[i].length();
 104  
             }
 105  63
             i++;
 106  
         }
 107  2
         return list;
 108  
     }
 109  
 
 110  
 
 111  
     /**
 112  
      * <p>Checks if a (trimmed) String is <code>null</code> or blank.</p>
 113  
      *
 114  
      * @param str the String to check
 115  
      * @return <code>true</code> if the String is <code>null</code>, or
 116  
      *         length zero once trimmed
 117  
      */
 118  
     public static boolean isBlank( String str )
 119  
     {
 120  0
         return ( ( str == null ) || ( str.trim().length() == 0 ) );
 121  
     }
 122  
 
 123  
 
 124  
     // Ripped from commons-lang StringEscapeUtils. Maybe Use dependency instead
 125  
     public static void unescapeJava( StringWriter out, String str )
 126  
     {
 127  0
         if ( out == null )
 128  
         {
 129  0
             throw new IllegalArgumentException( "The Writer must not be null" );
 130  
         }
 131  0
         if ( str == null )
 132  
         {
 133  0
             return;
 134  
         }
 135  0
         int sz = str.length();
 136  0
         StringBuffer unicode = new StringBuffer( 4 );
 137  0
         boolean hadSlash = false;
 138  0
         boolean inUnicode = false;
 139  0
         for ( int i = 0; i < sz; i++ )
 140  
         {
 141  0
             char ch = str.charAt( i );
 142  0
             if ( inUnicode )
 143  
             {
 144  
                 // if in unicode, then we're reading unicode
 145  
                 // values in somehow
 146  0
                 unicode.append( ch );
 147  0
                 if ( unicode.length() == 4 )
 148  
                 {
 149  
                     // unicode now contains the four hex digits
 150  
                     // which represents our unicode character
 151  
                     try
 152  
                     {
 153  0
                         int value = Integer.parseInt( unicode.toString(), 16 );
 154  0
                         out.write( (char) value );
 155  0
                         unicode.setLength( 0 );
 156  0
                         inUnicode = false;
 157  0
                         hadSlash = false;
 158  
                     }
 159  0
                     catch ( NumberFormatException nfe )
 160  
                     {
 161  0
                         throw new NestedRuntimeException( "Unable to parse unicode value: " + unicode, nfe );
 162  0
                     }
 163  
                 }
 164  
                 continue;
 165  
             }
 166  0
             if ( hadSlash )
 167  
             {
 168  
                 // handle an escaped value
 169  0
                 hadSlash = false;
 170  0
                 switch ( ch )
 171  
                 {
 172  
                     case '\\':
 173  0
                         out.write( '\\' );
 174  0
                         break;
 175  
                     case '\'':
 176  0
                         out.write( '\'' );
 177  0
                         break;
 178  
                     case '\"':
 179  0
                         out.write( '"' );
 180  0
                         break;
 181  
                     case 'r':
 182  0
                         out.write( '\r' );
 183  0
                         break;
 184  
                     case 'f':
 185  0
                         out.write( '\f' );
 186  0
                         break;
 187  
                     case 't':
 188  0
                         out.write( '\t' );
 189  0
                         break;
 190  
                     case 'n':
 191  0
                         out.write( '\n' );
 192  0
                         break;
 193  
                     case 'b':
 194  0
                         out.write( '\b' );
 195  0
                         break;
 196  
                     case 'u':
 197  
                     {
 198  
                         // uh-oh, we're in unicode country....
 199  0
                         inUnicode = true;
 200  0
                         break;
 201  
                     }
 202  
                     default:
 203  0
                         out.write( ch );
 204  0
                         break;
 205  
                 }
 206  
                 continue;
 207  
             }
 208  0
             else if ( ch == '\\' )
 209  
             {
 210  0
                 hadSlash = true;
 211  0
                 continue;
 212  
             }
 213  0
             out.write( ch );
 214  
         }
 215  0
         if ( hadSlash )
 216  
         {
 217  
             // then we're in the weird case of a \ at the end of the
 218  
             // string, let's output it anyway.
 219  0
             out.write( '\\' );
 220  
         }
 221  0
     }
 222  
 
 223  
     // Ripped from commons-lang StringEscapeUtils. Maybe Use dependency instead
 224  
     public static int unescapeJava( byte[] out, String str )
 225  
     {
 226  2
         int outPos = 0;
 227  2
         if ( out == null )
 228  
         {
 229  0
             throw new IllegalArgumentException( "The Writer must not be null" );
 230  
         }
 231  2
         if ( str == null )
 232  
         {
 233  0
             return 0;
 234  
         }
 235  2
         int sz = str.length();
 236  2
         StringBuffer unicode = new StringBuffer( 4 );
 237  2
         boolean hadSlash = false;
 238  2
         boolean inUnicode = false;
 239  9
         for ( int i = 0; i < sz; i++ )
 240  
         {
 241  7
             char ch = str.charAt( i );
 242  7
             if ( inUnicode )
 243  
             {
 244  
                 // if in unicode, then we're reading unicode
 245  
                 // values in somehow
 246  0
                 unicode.append( ch );
 247  0
                 if ( unicode.length() == 4 )
 248  
                 {
 249  
                     // unicode now contains the four hex digits
 250  
                     // which represents our unicode character
 251  
                     try
 252  
                     {
 253  0
                         int value = Integer.parseInt( unicode.toString(), 16 );
 254  0
                         out[outPos++] = (byte) value;
 255  0
                         unicode.setLength( 0 );
 256  0
                         inUnicode = false;
 257  0
                         hadSlash = false;
 258  
                     }
 259  0
                     catch ( NumberFormatException nfe )
 260  
                     {
 261  0
                         throw new NestedRuntimeException( "Unable to parse unicode value: " + unicode, nfe );
 262  0
                     }
 263  
                 }
 264  
                 continue;
 265  
             }
 266  7
             if ( hadSlash )
 267  
             {
 268  
                 // handle an escaped value
 269  0
                 hadSlash = false;
 270  0
                 switch ( ch )
 271  
                 {
 272  
                     case '\\':
 273  0
                         out[outPos++] = '\\';
 274  0
                         break;
 275  
                     case '\'':
 276  0
                         out[outPos++] = '\'';
 277  0
                         break;
 278  
                     case '\"':
 279  0
                         out[outPos++] = '"';
 280  0
                         break;
 281  
                     case 'r':
 282  0
                         out[outPos++] = '\r';
 283  0
                         break;
 284  
                     case 'f':
 285  0
                         out[outPos++] = '\f';
 286  0
                         break;
 287  
                     case 't':
 288  0
                         out[outPos++] = '\t';
 289  0
                         break;
 290  
                     case 'n':
 291  0
                         out[outPos++] = '\n';
 292  0
                         break;
 293  
                     case 'b':
 294  0
                         out[outPos++] = '\b';
 295  0
                         break;
 296  
                     case 'u':
 297  
                     {
 298  
                         // uh-oh, we're in unicode country....
 299  0
                         inUnicode = true;
 300  0
                         break;
 301  
                     }
 302  
                     default:
 303  0
                         out[outPos++] = (byte) ch;
 304  0
                         break;
 305  
                 }
 306  
                 continue;
 307  
             }
 308  7
             else if ( ch == '\\' )
 309  
             {
 310  0
                 hadSlash = true;
 311  0
                 continue;
 312  
             }
 313  7
             out[outPos++] = (byte) ch;
 314  
         }
 315  2
         if ( hadSlash )
 316  
         {
 317  
             // then we're in the weird case of a \ at the end of the
 318  
             // string, let's output it anyway.
 319  0
             out[outPos++] = '\\';
 320  
         }
 321  2
         return outPos;
 322  
     }
 323  
 
 324  
     // Ripped from commons-lang StringEscapeUtils. With a minor modification, we unicode-quote commas
 325  
     // to avoid csv decoding problems ;)
 326  
 
 327  
     /**
 328  
      * @param out               write to receieve the escaped string
 329  
      * @param str               String to escape values in, may be null
 330  
      * @param escapeSingleQuote escapes single quotes if <code>true</code>
 331  
      * @throws java.io.IOException if an IOException occurs
 332  
      */
 333  
     public static void escapeJavaStyleString( Writer out, String str, boolean escapeSingleQuote )
 334  
         throws IOException
 335  
     {
 336  116
         if ( out == null )
 337  
         {
 338  0
             throw new IllegalArgumentException( "The Writer must not be null" );
 339  
         }
 340  116
         if ( str == null )
 341  
         {
 342  0
             return;
 343  
         }
 344  
         int sz;
 345  116
         sz = str.length();
 346  3916
         for ( int i = 0; i < sz; i++ )
 347  
         {
 348  3800
             char ch = str.charAt( i );
 349  
 
 350  
             // handle unicode
 351  3800
             if ( ch > 0xfff )
 352  
             {
 353  0
                 out.write( "\\u" + hex( ch ) );
 354  
             }
 355  3800
             else if ( ch > 0xff )
 356  
             {
 357  0
                 out.write( "\\u0" + hex( ch ) );
 358  
             }
 359  3800
             else if ( ch > 0x7f || ch == ',' )
 360  
             {    // Kr - this line modified from commons
 361  1
                 out.write( "\\u00" + hex( ch ) );
 362  
             }
 363  3799
             else if ( ch < 32 )
 364  
             {
 365  2
                 switch ( ch )
 366  
                 {
 367  
                     case '\b':
 368  0
                         out.write( '\\' );
 369  0
                         out.write( 'b' );
 370  0
                         break;
 371  
                     case '\n':
 372  1
                         out.write( '\\' );
 373  1
                         out.write( 'n' );
 374  1
                         break;
 375  
                     case '\t':
 376  0
                         out.write( '\\' );
 377  0
                         out.write( 't' );
 378  0
                         break;
 379  
                     case '\f':
 380  0
                         out.write( '\\' );
 381  0
                         out.write( 'f' );
 382  0
                         break;
 383  
                     case '\r':
 384  1
                         out.write( '\\' );
 385  1
                         out.write( 'r' );
 386  1
                         break;
 387  
                     default:
 388  0
                         if ( ch > 0xf )
 389  
                         {
 390  0
                             out.write( "\\u00" + hex( ch ) );
 391  
                         }
 392  
                         else
 393  
                         {
 394  0
                             out.write( "\\u000" + hex( ch ) );
 395  
                         }
 396  0
                         break;
 397  
                 }
 398  
             }
 399  
             else
 400  
             {
 401  3797
                 switch ( ch )
 402  
                 {
 403  
                     case '\'':
 404  0
                         if ( escapeSingleQuote )
 405  
                         {
 406  0
                             out.write( '\\' );
 407  
                         }
 408  0
                         out.write( '\'' );
 409  0
                         break;
 410  
                     case '"':
 411  0
                         out.write( '\\' );
 412  0
                         out.write( '"' );
 413  0
                         break;
 414  
                     case '\\':
 415  216
                         out.write( '\\' );
 416  216
                         out.write( '\\' );
 417  216
                         break;
 418  
                     case '/':
 419  7
                         out.write( '\\' );
 420  7
                         out.write( '/' );
 421  7
                         break;
 422  
                     default:
 423  3574
                         out.write( ch );
 424  
                         break;
 425  
                 }
 426  
             }
 427  
         }
 428  116
     }
 429  
 
 430  
     public static void escapeJavaStyleString( ByteBuffer out, byte[] str, int off, int len )
 431  
     {
 432  1
         if ( out == null )
 433  
         {
 434  0
             throw new IllegalArgumentException( "The Writer must not be null" );
 435  
         }
 436  1
         final int inputLength = str.length;
 437  1
         if ( str == null || inputLength == 0 )
 438  
         {
 439  0
             return;
 440  
         }
 441  1
         int outputPos = 0;
 442  1
         int end = off + len;
 443  5
         for ( int i = off; i < end; i++ )
 444  
         {
 445  4
             char ch = (char) str[i];
 446  
 
 447  
             // handle unicode
 448  4
             if ( ch > 0xfff )
 449  
             {
 450  0
                 outputPos = writeOut( out, outputPos, "\\u" + hex( ch ) );
 451  
             }
 452  4
             else if ( ch > 0xff )
 453  
             {
 454  0
                 outputPos = writeOut( out, outputPos, "\\u0" + hex( ch ) );
 455  
             }
 456  4
             else if ( ch > 0x7f || ch == ',' )
 457  
             {    // Kr - this line modified from commons
 458  0
                 outputPos = writeOut( out, outputPos, "\\u00" + hex( ch ) );
 459  
             }
 460  4
             else if ( ch < 32 )
 461  
             {
 462  1
                 switch ( ch )
 463  
                 {
 464  
                     case '\b':
 465  0
                         out.append( '\\' );
 466  0
                         out.append( 'b' );
 467  0
                         break;
 468  
                     case '\n':
 469  0
                         out.append( '\\' );
 470  0
                         out.append( 'n' );
 471  0
                         break;
 472  
                     case '\t':
 473  1
                         out.append( '\\' );
 474  1
                         out.append( 't' );
 475  1
                         break;
 476  
                     case '\f':
 477  0
                         out.append( '\\' );
 478  0
                         out.append( 'f' );
 479  0
                         break;
 480  
                     case '\r':
 481  0
                         out.append( '\\' );
 482  0
                         out.append( 'r' );
 483  0
                         break;
 484  
                     default:
 485  0
                         if ( ch > 0xf )
 486  
                         {
 487  0
                             outputPos = writeOut( out, outputPos, "\\u00" + hex( ch ) );
 488  
                         }
 489  
                         else
 490  
                         {
 491  0
                             outputPos = writeOut( out, outputPos, "\\u000" + hex( ch ) );
 492  
                         }
 493  0
                         break;
 494  
                 }
 495  
             }
 496  
             else
 497  
             {
 498  3
                 switch ( ch )
 499  
                 {
 500  
                     case '\'':
 501  0
                         out.append( '\\' );
 502  0
                         out.append( '\'' );
 503  0
                         break;
 504  
                     case '"':
 505  0
                         out.append( '\\' );
 506  0
                         out.append( '"' );
 507  0
                         break;
 508  
                     case '\\':
 509  0
                         out.append( '\\' );
 510  0
                         out.append( '\\' );
 511  0
                         break;
 512  
                     case '/':
 513  0
                         out.append( '\\' );
 514  0
                         out.append( '/' );
 515  0
                         break;
 516  
                     default:
 517  3
                         out.append( ch );
 518  
                         break;
 519  
                 }
 520  
             }
 521  
         }
 522  1
     }
 523  
 
 524  
     public static void escapeJavaStyleString( PrintStream out, byte[] str, int off, int len )
 525  
     {
 526  0
         if ( out == null )
 527  
         {
 528  0
             throw new IllegalArgumentException( "The Writer must not be null" );
 529  
         }
 530  0
         final int inputLength = str.length;
 531  0
         if ( str == null || inputLength == 0 )
 532  
         {
 533  0
             return;
 534  
         }
 535  0
         int outputPos = 0;
 536  0
         int end = off + len;
 537  0
         for ( int i = off; i < end; i++ )
 538  
         {
 539  0
             char ch = (char) str[i];
 540  
 
 541  
             // handle unicode
 542  0
             if ( ch > 0xfff )
 543  
             {
 544  0
                 outputPos = writeOut( out, outputPos, "\\u" + hex( ch ) );
 545  
             }
 546  0
             else if ( ch > 0xff )
 547  
             {
 548  0
                 outputPos = writeOut( out, outputPos, "\\u0" + hex( ch ) );
 549  
             }
 550  0
             else if ( ch > 0x7f || ch == ',' )
 551  
             {    // Kr - this line modified from commons
 552  0
                 outputPos = writeOut( out, outputPos, "\\u00" + hex( ch ) );
 553  
             }
 554  0
             else if ( ch < 32 )
 555  
             {
 556  0
                 switch ( ch )
 557  
                 {
 558  
                     case '\b':
 559  0
                         out.append( '\\' );
 560  0
                         out.append( 'b' );
 561  0
                         break;
 562  
                     case '\n':
 563  0
                         out.append( '\\' );
 564  0
                         out.append( 'n' );
 565  0
                         break;
 566  
                     case '\t':
 567  0
                         out.append( '\\' );
 568  0
                         out.append( 't' );
 569  0
                         break;
 570  
                     case '\f':
 571  0
                         out.append( '\\' );
 572  0
                         out.append( 'f' );
 573  0
                         break;
 574  
                     case '\r':
 575  0
                         out.append( '\\' );
 576  0
                         out.append( 'r' );
 577  0
                         break;
 578  
                     default:
 579  0
                         if ( ch > 0xf )
 580  
                         {
 581  0
                             outputPos = writeOut( out, outputPos, "\\u00" + hex( ch ) );
 582  
                         }
 583  
                         else
 584  
                         {
 585  0
                             outputPos = writeOut( out, outputPos, "\\u000" + hex( ch ) );
 586  
                         }
 587  0
                         break;
 588  
                 }
 589  
             }
 590  
             else
 591  
             {
 592  0
                 switch ( ch )
 593  
                 {
 594  
                     case '\'':
 595  0
                         out.append( '\\' );
 596  0
                         out.append( '\'' );
 597  0
                         break;
 598  
                     case '"':
 599  0
                         out.append( '\\' );
 600  0
                         out.append( '"' );
 601  0
                         break;
 602  
                     case '\\':
 603  0
                         out.append( '\\' );
 604  0
                         out.append( '\\' );
 605  0
                         break;
 606  
                     case '/':
 607  0
                         out.append( '\\' );
 608  0
                         out.append( '/' );
 609  0
                         break;
 610  
                     default:
 611  0
                         out.append( ch );
 612  
                         break;
 613  
                 }
 614  
             }
 615  
         }
 616  0
     }
 617  
 
 618  
     public static int escapeJavaStyleString( byte[] out, int outoff, byte[] str, int off, int len )
 619  
     {
 620  2
         if ( out == null )
 621  
         {
 622  0
             throw new IllegalArgumentException( "The Writer must not be null" );
 623  
         }
 624  2
         final int inputLength = str.length;
 625  2
         if ( str == null || inputLength == 0 )
 626  
         {
 627  1
             return 0;
 628  
         }
 629  1
         int outputPos = outoff;
 630  1
         int end = off + len;
 631  2
         for ( int i = off; i < end; i++ )
 632  
         {
 633  1
             char ch = (char) str[i];
 634  
 
 635  
             // handle unicode
 636  1
             if ( ch > 0xfff )
 637  
             {
 638  0
                 outputPos = writeOut( out, outputPos, "\\u" + hex( ch ) );
 639  
             }
 640  1
             else if ( ch > 0xff )
 641  
             {
 642  0
                 outputPos = writeOut( out, outputPos, "\\u0" + hex( ch ) );
 643  
             }
 644  1
             else if ( ch > 0x7f || ch == ',' )
 645  
             {    // Kr - this line modified from commons
 646  0
                 outputPos = writeOut( out, outputPos, "\\u00" + hex( ch ) );
 647  
             }
 648  1
             else if ( ch < 32 )
 649  
             {
 650  0
                 switch ( ch )
 651  
                 {
 652  
                     case '\b':
 653  0
                         out[outputPos++] = '\\';
 654  0
                         out[outputPos++] = 'b';
 655  0
                         break;
 656  
                     case '\n':
 657  0
                         out[outputPos++] = '\\';
 658  0
                         out[outputPos++] = 'n';
 659  0
                         break;
 660  
                     case '\t':
 661  0
                         out[outputPos++] = '\\';
 662  0
                         out[outputPos++] = 't';
 663  0
                         break;
 664  
                     case '\f':
 665  0
                         out[outputPos++] = '\\';
 666  0
                         out[outputPos++] = 'f';
 667  0
                         break;
 668  
                     case '\r':
 669  0
                         out[outputPos++] = '\\';
 670  0
                         out[outputPos++] = 'r';
 671  0
                         break;
 672  
                     default:
 673  0
                         if ( ch > 0xf )
 674  
                         {
 675  0
                             outputPos = writeOut( out, outputPos, "\\u00" + hex( ch ) );
 676  
                         }
 677  
                         else
 678  
                         {
 679  0
                             outputPos = writeOut( out, outputPos, "\\u000" + hex( ch ) );
 680  
                         }
 681  0
                         break;
 682  
                 }
 683  
             }
 684  
             else
 685  
             {
 686  1
                 switch ( ch )
 687  
                 {
 688  
                     case '\'':
 689  0
                         out[outputPos++] = '\\';
 690  0
                         out[outputPos++] = '\'';
 691  0
                         break;
 692  
                     case '"':
 693  0
                         out[outputPos++] = '\\';
 694  0
                         out[outputPos++] = '"';
 695  0
                         break;
 696  
                     case '\\':
 697  0
                         out[outputPos++] = '\\';
 698  0
                         out[outputPos++] = '\\';
 699  0
                         break;
 700  
                     case '/':
 701  0
                         out[outputPos++] = '\\';
 702  0
                         out[outputPos++] = '/';
 703  0
                         break;
 704  
                     default:
 705  1
                         out[outputPos++] = (byte) ch;
 706  
                         break;
 707  
                 }
 708  
             }
 709  
         }
 710  1
         return outputPos - outoff;
 711  
     }
 712  
 
 713  
     private static int writeOut( ByteBuffer out, int outputPos, final String msg )
 714  
     {
 715  0
         byte[] bytes = msg.getBytes();
 716  0
         for ( int cnt = 0; cnt < bytes.length; cnt++ )
 717  
         {
 718  0
             out.append( bytes[cnt] );
 719  
         }
 720  0
         return outputPos;
 721  
     }
 722  
 
 723  
     private static int writeOut( PrintStream out, int outputPos, final String msg )
 724  
     {
 725  0
         byte[] bytes = msg.getBytes();
 726  0
         for ( int cnt = 0; cnt < bytes.length; cnt++ )
 727  
         {
 728  0
             out.write( bytes[cnt] );
 729  
         }
 730  0
         return outputPos;
 731  
     }
 732  
 
 733  
 
 734  
     private static int writeOut( byte[] out, int outputPos, final String msg )
 735  
     {
 736  0
         byte[] bytes = msg.getBytes();
 737  0
         for ( int cnt = 0; cnt < bytes.length; cnt++ )
 738  
         {
 739  0
             out[outputPos++] = bytes[cnt];
 740  
         }
 741  0
         return outputPos;
 742  
     }
 743  
 
 744  
 
 745  
     public static String hex( char ch )
 746  
     {
 747  1
         return Integer.toHexString( ch ).toUpperCase();
 748  
     }
 749  
 
 750  
     /**
 751  
      * Courtesy of commons-lang StringEscapeUtils, slightly modified, see below
 752  
      *
 753  
      * @param str String to escape values in, may be null
 754  
      * @return the escaped string
 755  
      */
 756  
     public static void escapeJavaStyleString( StringBuffer target, String str )
 757  
     {
 758  116
         if ( str == null )
 759  
         {
 760  0
             return;
 761  
         }
 762  
         try
 763  
         {
 764  116
             StringWriter writer = new StringWriter( str.length() * 2 );
 765  116
             escapeJavaStyleString( writer, str, true );
 766  116
             target.append( writer.toString() ); // todo: be bit smarter
 767  
         }
 768  0
         catch ( IOException ioe )
 769  
         {
 770  
             // this should never ever happen while writing to a StringWriter
 771  0
             ioe.printStackTrace();
 772  116
         }
 773  116
     }
 774  
 
 775  
     public static void escapeJavaStyleString( PrintStream target, String str )
 776  
     {
 777  0
         if ( str == null )
 778  
         {
 779  0
             return;
 780  
         }
 781  
         try
 782  
         {
 783  0
             StringWriter writer = new StringWriter( str.length() * 2 );
 784  0
             escapeJavaStyleString( writer, str, true );
 785  0
             target.append( writer.toString() ); // todo: be bit smarter
 786  
         }
 787  0
         catch ( IOException ioe )
 788  
         {
 789  
             // this should never ever happen while writing to a StringWriter
 790  0
             ioe.printStackTrace();
 791  0
         }
 792  0
     }
 793  
 }
 794