1 package org.apache.maven.surefire.util.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.net.MalformedURLException;
24 import java.net.URL;
25 import java.util.BitSet;
26
27 import static org.apache.maven.surefire.util.internal.StringUtils.UTF_8;
28
29
30
31
32 public final class UrlUtils
33 {
34 private static final BitSet UNRESERVED = new BitSet( Byte.MAX_VALUE - Byte.MIN_VALUE + 1 );
35
36 private static final int RADIX = 16;
37
38 private static final int MASK = 0xf;
39
40 private UrlUtils()
41 {
42 throw new IllegalStateException( "no instantiable constructor" );
43 }
44
45 static
46 {
47 byte[] bytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'():/".getBytes( UTF_8 );
48 for ( byte aByte : bytes )
49 {
50 UNRESERVED.set( aByte );
51 }
52 }
53
54 public static URL toURL( File file )
55 throws MalformedURLException
56 {
57
58
59 URL url = file.toURL();
60
61
62 byte[] bytes = url.toString().getBytes( UTF_8 );
63 StringBuilder buf = new StringBuilder( bytes.length );
64 for ( byte b : bytes )
65 {
66 if ( b > 0 && UNRESERVED.get( b ) )
67 {
68 buf.append( (char) b );
69 }
70 else
71 {
72 buf.append( '%' );
73 buf.append( Character.forDigit( b >>> 4 & MASK, RADIX ) );
74 buf.append( Character.forDigit( b & MASK, RADIX ) );
75 }
76 }
77 return new URL( buf.toString() );
78 }
79 }