private Map < String , String [ ] > parseQuery ( String qs ) { Map < String , String [ ] > ret = new TreeMap < String , String [ ] > ( ) ; try { String pairs [ ] = qs . split ( "&" ) ; for ( String pair : pairs ) { String name ; String value ; int pos = pair . indexOf ( ' ' ) ; // for "name=", the value is "", for "name" alone, the value is null if ( pos == - 1 ) { name = pair ; value = null ; } else { name = URLDecoder . decode ( pair . substring ( 0 , pos ) , "UTF-8" ) ; value = URLDecoder . decode ( pair . substring ( pos + 1 , pair . length ( ) ) , "UTF-8" ) ; } String [ ] values = ret . get ( name ) ; if ( values == null ) { values = new String [ ] { value } ; ret . put ( name , values ) ; } else { // That's not a very cheap algorithm to create new arrays on the fly, // but it is expected that there will be only a handful of array parameters // in an URL anyway. So, let us be dirty here ... String [ ] newValues = new String [ values . length + 1 ] ; System . arraycopy ( values , 0 , newValues , 0 , values . length ) ; newValues [ values . length ] = value ; ret . put ( name , newValues ) ; } } return ret ; } catch ( UnsupportedEncodingException e ) { throw new IllegalArgumentException ( "Cannot decode to UTF-8. Should not happen, though." , e ) ; } }
parse the query