All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.maven.surefire.util.internal.UrlUtils Maven / Gradle / Ivy

Go to download

API used in Surefire and Failsafe MOJO, Booter, Common and test framework providers.

There is a newer version: 3.5.2
Show newest version
package org.apache.maven.surefire.util.internal;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.BitSet;

/**
 * Utility for dealing with URLs in pre-JDK 1.4.
 */
public final class UrlUtils
{
    private static final BitSet UNRESERVED = new BitSet( Byte.MAX_VALUE - Byte.MIN_VALUE + 1 );

    private static final int RADIX = 16;

    private static final int MASK = 0xf;

    private UrlUtils()
    {
        throw new IllegalStateException( "no instantiable constructor" );
    }

    private static final String ENCODING = "UTF-8";

    static
    {
        try
        {
            byte[] bytes =
                "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'():/".getBytes( ENCODING );
            for ( byte aByte : bytes )
            {
                UNRESERVED.set( aByte );
            }
        }
        catch ( UnsupportedEncodingException e )
        {
            // can't happen as UTF-8 must be present
        }
    }

    public static URL toURL( File file )
        throws MalformedURLException
    {
        // with JDK 1.4+, code would be: return new URL( file.toURI().toASCIIString() );
        //noinspection deprecation
        URL url = file.toURL();
        // encode any characters that do not comply with RFC 2396
        // this is primarily to handle Windows where the user's home directory contains spaces
        try
        {
            byte[] bytes = url.toString().getBytes( ENCODING );
            StringBuilder buf = new StringBuilder( bytes.length );
            for ( byte b : bytes )
            {
                if ( b > 0 && UNRESERVED.get( b ) )
                {
                    buf.append( (char) b );
                }
                else
                {
                    buf.append( '%' );
                    buf.append( Character.forDigit( b >>> 4 & MASK, RADIX ) );
                    buf.append( Character.forDigit( b & MASK, RADIX ) );
                }
            }
            return new URL( buf.toString() );
        }
        catch ( UnsupportedEncodingException e )
        {
            // should not happen as UTF-8 must be present
            throw new RuntimeException( e );
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy