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.io.UnsupportedEncodingException;
24 import java.net.MalformedURLException;
25 import java.net.URL;
26 import java.util.BitSet;
27
28
29
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
60 }
61 }
62
63 public static URL toURL( File file )
64 throws MalformedURLException
65 {
66
67
68 URL url = file.toURL();
69
70
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
93 throw new RuntimeException( e );
94 }
95 }
96 }