1 package org.apache.maven.surefire.util;
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 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 }
42
43 private static final String ENCODING = "UTF-8";
44
45 static
46 {
47 try
48 {
49 byte[] bytes =
50 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'():/".getBytes( ENCODING );
51 for ( byte aByte : bytes )
52 {
53 UNRESERVED.set( aByte );
54 }
55 }
56 catch ( UnsupportedEncodingException e )
57 {
58
59 }
60 }
61
62 public static URL getURL( File file )
63 throws MalformedURLException
64 {
65
66
67 URL url = file.toURL();
68
69
70 try
71 {
72 byte[] bytes = url.toString().getBytes( ENCODING );
73 StringBuilder buf = new StringBuilder( bytes.length );
74 for ( byte b : bytes )
75 {
76 if ( b > 0 && UNRESERVED.get( b ) )
77 {
78 buf.append( (char) b );
79 }
80 else
81 {
82 buf.append( '%' );
83 buf.append( Character.forDigit( b >>> 4 & MASK, RADIX ) );
84 buf.append( Character.forDigit( b & MASK, RADIX ) );
85 }
86 }
87 return new URL( buf.toString() );
88 }
89 catch ( UnsupportedEncodingException e )
90 {
91
92 throw new NestedRuntimeException( e );
93 }
94 }
95 }