public static Headers of ( String ... namesAndValues ) { if ( namesAndValues == null ) throw new NullPointerException ( "namesAndValues == null" ) ; if ( namesAndValues . length % 2 != 0 ) { throw new IllegalArgumentException ( "Expected alternating header names and values" ) ; } // Make a defensive copy and clean it up. namesAndValues = namesAndValues . clone ( ) ; for ( int i = 0 ; i < namesAndValues . length ; i ++ ) { if ( namesAndValues [ i ] == null ) throw new IllegalArgumentException ( "Headers cannot be null" ) ; namesAndValues [ i ] = namesAndValues [ i ] . trim ( ) ; } // Check for malformed headers. for ( int i = 0 ; i < namesAndValues . length ; i += 2 ) { String name = namesAndValues [ i ] ; String value = namesAndValues [ i + 1 ] ; if ( name . length ( ) == 0 || name . indexOf ( ' ' ) != - 1 || value . indexOf ( ' ' ) != - 1 ) { throw new IllegalArgumentException ( "Unexpected header: " + name + ": " + value ) ; } } return new Headers ( namesAndValues ) ; }
Returns headers for the alternating header names and values . There must be an even number of arguments and they must alternate between header names and values .