static void parseHeader(final String header, Map<Token, String> response) {
        if (response.size() == Token.values().length) {
            //already parsed everything
            return;
        }
        char[] headerChars = header.toCharArray();

        SearchingFor searchingFor = SearchingFor.START_OF_NAME;
        int nameStart = 0;
        Token currentToken = null;
        int valueStart = 0;

        int escapeCount = 0;
        boolean containsEscapes = false;

        for (int i = 0; i < headerChars.length; i++) {
            switch (searchingFor) {
                case START_OF_NAME:
                    // Eliminate any white space before the name of the parameter.
                    if (headerChars[i] != ';' && !Character.isWhitespace(headerChars[i])) {
                        nameStart = i;
                        searchingFor = SearchingFor.EQUALS_SIGN;
                    }
                    break;
                case EQUALS_SIGN:
                    if (headerChars[i] == '=') {
                        String paramName = String.valueOf(headerChars, nameStart, i - nameStart);
                        currentToken = TOKENS.get(paramName.toLowerCase(Locale.ENGLISH));
                        //we allow unkown tokens, but just ignore them
                        searchingFor = SearchingFor.START_OF_VALUE;
                    }
                    break;
                case START_OF_VALUE:
                    if (!Character.isWhitespace(headerChars[i])) {
                        if (headerChars[i] == '"') {
                            valueStart = i + 1;
                            searchingFor = SearchingFor.LAST_QUOTE;
                        } else {
                            valueStart = i;
                            searchingFor = SearchingFor.END_OF_VALUE;
                        }
                    }
                    break;
                case LAST_QUOTE:
                    if (headerChars[i] == '\\') {
                        escapeCount++;
                        containsEscapes = true;
                    } else if (headerChars[i] == '"' && (escapeCount % 2 == 0)) {
                        String value = String.valueOf(headerChars, valueStart, i - valueStart);
                        if (containsEscapes) {
                            StringBuilder sb = new StringBuilder();
                            boolean lastEscape = false;
                            for (int j = 0; j < value.length(); ++j) {
                                char c = value.charAt(j);
                                if (c == '\\' && !lastEscape) {
                                    lastEscape = true;
                                } else {
                                    lastEscape = false;
                                    sb.append(c);
                                }
                            }
                            value = sb.toString();
                            containsEscapes = false;
                        }
                        if (currentToken != null && !response.containsKey(currentToken)) {
                            response.put(currentToken, value);
                        }

                        searchingFor = SearchingFor.START_OF_NAME;
                        escapeCount = 0;
                    } else {
                        escapeCount = 0;
                    }
                    break;
                case END_OF_VALUE:
                    if (headerChars[i] == ';' || headerChars[i] == ',' || Character.isWhitespace(headerChars[i])) {
                        String value = String.valueOf(headerChars, valueStart, i - valueStart);
                        if (currentToken != null && !response.containsKey(currentToken)) {
                            response.put(currentToken, value);
                        }

                        searchingFor = SearchingFor.START_OF_NAME;
                    }
                    break;
            }
        }

        if (searchingFor == SearchingFor.END_OF_VALUE) {
            // Special case where we reached the end of the array containing the header values.
            String value = String.valueOf(headerChars, valueStart, headerChars.length - valueStart);
            if (currentToken != null && !response.containsKey(currentToken)) {
                response.put(currentToken, value);
            }
        } else if (searchingFor != SearchingFor.START_OF_NAME) {
            // Somehow we are still in the middle of searching for a current value.
            throw MESSAGES.invalidHeader();
        }

    }
static void parseHeader ( final String header , Map < Token , String > response ) { if ( response . size ( ) == Token . values ( ) . length ) { return ; } char [ ] headerChars = header . toCharArray ( ) ; SearchingFor searchingFor = SearchingFor . START_OF_NAME ; int nameStart = 0 ; Token currentToken = null ; int valueStart = 0 ; int escapeCount = 0 ; boolean containsEscapes = false ; for ( int i = 0 ; i < headerChars . length ; i ++ ) { switch ( searchingFor ) { case START_OF_NAME : if ( headerChars [ i ] != ';' && ! Character . isWhitespace ( headerChars [ i ] ) ) { nameStart = i ; searchingFor = SearchingFor . EQUALS_SIGN ; } break ; case EQUALS_SIGN : if ( headerChars [ i ] == '=' ) { String paramName = String . valueOf ( headerChars , nameStart , i - nameStart ) ; currentToken = TOKENS . get ( paramName . toLowerCase ( Locale . ENGLISH ) ) ; searchingFor = SearchingFor . START_OF_VALUE ; } break ; case START_OF_VALUE : if ( ! Character . isWhitespace ( headerChars [ i ] ) ) { if ( headerChars [ i ] == '"' ) { valueStart = i + 1 ; searchingFor = SearchingFor . LAST_QUOTE ; } else { valueStart = i ; searchingFor = SearchingFor . END_OF_VALUE ; } } break ; case LAST_QUOTE : if ( headerChars [ i ] == '\\' ) { escapeCount ++ ; containsEscapes = true ; } else if ( headerChars [ i ] == '"' && ( escapeCount % 2 == 0 ) ) { String value = String . valueOf ( headerChars , valueStart , i - valueStart ) ; if ( containsEscapes ) { StringBuilder sb = new StringBuilder ( ) ; boolean lastEscape = false ; for ( int j = 0 ; j < value . length ( ) ; ++ j ) { char c = value . charAt ( j ) ; if ( c == '\\' && ! lastEscape ) { lastEscape = true ; } else { lastEscape = false ; sb . append ( c ) ; } } value = sb . toString ( ) ; containsEscapes = false ; } if ( currentToken != null && ! response . containsKey ( currentToken ) ) { response . put ( currentToken , value ) ; } searchingFor = SearchingFor . START_OF_NAME ; escapeCount = 0 ; } else { escapeCount = 0 ; } break ; case END_OF_VALUE : if ( headerChars [ i ] == ';' || headerChars [ i ] == ',' || Character . isWhitespace ( headerChars [ i ] ) ) { String value = String . valueOf ( headerChars , valueStart , i - valueStart ) ; if ( currentToken != null && ! response . containsKey ( currentToken ) ) { response . put ( currentToken , value ) ; } searchingFor = SearchingFor . START_OF_NAME ; } break ; } } if ( searchingFor == SearchingFor . END_OF_VALUE ) { String value = String . valueOf ( headerChars , valueStart , headerChars . length - valueStart ) ; if ( currentToken != null && ! response . containsKey ( currentToken ) ) { response . put ( currentToken , value ) ; } } else if ( searchingFor != SearchingFor . START_OF_NAME ) { throw MESSAGES . invalidHeader ( ) ; } }
package private for testing