View Javadoc

1   /*
2    * The MIT License
3    * 
4    * Copyright (c) 2007 The Codehaus
5    * 
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7    * associated documentation files (the "Software"), to deal in the Software without restriction,
8    * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9    * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10   * furnished to do so, subject to the following conditions:
11   * 
12   * The above copyright notice and this permission notice shall be included in all copies or
13   * substantial portions of the Software.
14   * 
15   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16   * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17   * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18   * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20   */
21  package org.codehaus.mojo.native2ascii;
22  
23  import java.io.*;
24  import java.nio.CharBuffer;
25  
26  /**
27   * @author Evgeny Mandrikov
28   */
29  public final class Native2Ascii
30  {
31  
32      private Native2Ascii()
33      {
34      }
35  
36      /**
37       * Converts given CharSequence into ASCII String.
38       */
39      public static String nativeToAscii( CharSequence cs )
40      {
41          if ( cs == null )
42          {
43              return null;
44          }
45          StringBuilder sb = new StringBuilder();
46          for ( int i = 0; i < cs.length(); i++ )
47          {
48              char c = cs.charAt( i );
49              if ( c <= 0x7E )
50              {
51                  sb.append( c );
52              }
53              else
54              {
55                  sb.append( "\\u" );
56                  String hex = Integer.toHexString( c );
57                  for ( int j = hex.length(); j < 4; j++ )
58                  {
59                      sb.append( '0' );
60                  }
61                  sb.append( hex );
62              }
63          }
64          return sb.toString();
65      }
66  
67      public static void nativeToAscii( File src, File dst, String encoding )
68          throws IOException
69      {
70          BufferedReader input = null;
71          BufferedWriter output = null;
72          try
73          {
74              input = new BufferedReader( new InputStreamReader( new FileInputStream( src ), encoding ) );
75              output = new BufferedWriter( new OutputStreamWriter( new FileOutputStream( dst ), "US-ASCII" ) );
76  
77              char[] buffer = new char[4096];
78              int len;
79              while ( ( len = input.read( buffer ) ) != -1 )
80              {
81                  output.write( nativeToAscii( CharBuffer.wrap( buffer, 0, len ) ) );
82              }
83          }
84          finally
85          {
86              closeQuietly( input );
87              closeQuietly( output );
88          }
89      }
90  
91      private static void closeQuietly( Closeable closeable )
92      {
93          if ( closeable != null )
94          {
95              try
96              {
97                  closeable.close();
98              }
99              catch ( IOException e )
100             {
101                 // ignore
102             }
103         }
104     }
105 
106 }