private static String getUserInfo ( String url ) { String userInfo = null ; int startIndex = Integer . MIN_VALUE ; int nextSlashIndex = Integer . MIN_VALUE ; int endIndex = Integer . MIN_VALUE ; try { // The user info start index should be the first index of "//". startIndex = url . indexOf ( "//" ) ; if ( startIndex != - 1 ) { startIndex += 2 ; // The user info should be found before the next '/' index. nextSlashIndex = url . indexOf ( ' ' , startIndex ) ; if ( nextSlashIndex == - 1 ) { nextSlashIndex = url . length ( ) ; } // The user info ends at the last index of '@' from the previously // computed subsequence. endIndex = url . substring ( startIndex , nextSlashIndex ) . lastIndexOf ( ' ' ) ; if ( endIndex != - 1 ) { userInfo = url . substring ( startIndex , startIndex + endIndex ) ; } } } catch ( StringIndexOutOfBoundsException ex ) { System . err . println ( "String index out of bounds for:|" + url + "|" ) ; System . err . println ( "Start index: " + startIndex ) ; System . err . println ( "Next slash index " + nextSlashIndex ) ; System . err . println ( "End index :" + endIndex ) ; System . err . println ( "User info :|" + userInfo + "|" ) ; ex . printStackTrace ( ) ; } return userInfo ; }
Extract the user info from an URL .