View Javadoc
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.File;
23  import java.io.UnsupportedEncodingException;
24  import java.net.MalformedURLException;
25  import java.net.URL;
26  import java.util.BitSet;
27  
28  /**
29   * Utility for dealing with URLs in pre-JDK 1.4.
30   */
31  public final class UrlUtils
32  {
33      private static final BitSet UNRESERVED = new BitSet( Byte.MAX_VALUE - Byte.MIN_VALUE + 1 );
34  
35      private static final int RADIX = 16;
36  
37      private static final int MASK = 0xf;
38  
39      private UrlUtils()
40      {
41          throw new IllegalStateException( "no instantiable constructor" );
42      }
43  
44      private static final String ENCODING = "UTF-8";
45  
46      static
47      {
48          try
49          {
50              byte[] bytes =
51                  "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'():/".getBytes( ENCODING );
52              for ( byte aByte : bytes )
53              {
54                  UNRESERVED.set( aByte );
55              }
56          }
57          catch ( UnsupportedEncodingException e )
58          {
59              // can't happen as UTF-8 must be present
60          }
61      }
62  
63      public static URL toURL( File file )
64          throws MalformedURLException
65      {
66          // with JDK 1.4+, code would be: return new URL( file.toURI().toASCIIString() );
67          //noinspection deprecation
68          URL url = file.toURL();
69          // encode any characters that do not comply with RFC 2396
70          // this is primarily to handle Windows where the user's home directory contains spaces
71          try
72          {
73              byte[] bytes = url.toString().getBytes( ENCODING );
74              StringBuilder buf = new StringBuilder( bytes.length );
75              for ( byte b : bytes )
76              {
77                  if ( b > 0 && UNRESERVED.get( b ) )
78                  {
79                      buf.append( (char) b );
80                  }
81                  else
82                  {
83                      buf.append( '%' );
84                      buf.append( Character.forDigit( b >>> 4 & MASK, RADIX ) );
85                      buf.append( Character.forDigit( b & MASK, RADIX ) );
86                  }
87              }
88              return new URL( buf.toString() );
89          }
90          catch ( UnsupportedEncodingException e )
91          {
92              // should not happen as UTF-8 must be present
93              throw new RuntimeException( e );
94          }
95      }
96  }