private static URL attachUserInfo ( URL url , String user , char [ ] password ) throws MalformedURLException { if ( url == null ) { return null ; } if ( ( url . getAuthority ( ) == null || "" . equals ( url . getAuthority ( ) ) ) && ! "jar" . equals ( url . getProtocol ( ) ) ) { return url ; } StringBuilder buf = new StringBuilder ( ) ; String protocol = url . getProtocol ( ) ; if ( protocol . equals ( "jar" ) ) { URL newURL = new URL ( url . getPath ( ) ) ; newURL = attachUserInfo ( newURL , user , password ) ; buf . append ( "jar:" ) ; buf . append ( newURL . toString ( ) ) ; } else { password = correctPassword ( password ) ; user = correctUser ( user ) ; buf . append ( protocol ) ; buf . append ( "://" ) ; if ( ! "file" . equals ( protocol ) && user != null && user . trim ( ) . length ( ) > 0 ) { buf . append ( user ) ; if ( password != null && password . length > 0 ) { buf . append ( ":" ) ; buf . append ( password ) ; } buf . append ( "@" ) ; } buf . append ( url . getHost ( ) ) ; if ( url . getPort ( ) > 0 ) { buf . append ( ":" ) ; buf . append ( url . getPort ( ) ) ; } buf . append ( url . getPath ( ) ) ; String query = url . getQuery ( ) ; if ( query != null && query . trim ( ) . length ( ) > 0 ) { buf . append ( "?" ) . append ( query ) ; } String ref = url . getRef ( ) ; if ( ref != null && ref . trim ( ) . length ( ) > 0 ) { buf . append ( "#" ) . append ( ref ) ; } } return new URL ( buf . toString ( ) ) ; }
Build the URL from the data obtained from the user .