private static int findEndOfHeader ( byte [ ] pBytes , int pEnd ) { byte [ ] header = HTTP_HEADER_END . getBytes ( ) ; // Normal condition, check all bytes for ( int i = 0 ; i < pEnd - 4 ; i ++ ) { // Need 4 bytes to match if ( ( pBytes [ i ] == header [ 0 ] ) && ( pBytes [ i + 1 ] == header [ 1 ] ) && ( pBytes [ i + 2 ] == header [ 2 ] ) && ( pBytes [ i + 3 ] == header [ 3 ] ) ) { //System.err.println("FOUND END OF HEADER!"); return i + 4 ; } } // Check last 3 bytes, to check if we have a partial match if ( ( pEnd - 1 >= 0 ) && ( pBytes [ pEnd - 1 ] == header [ 0 ] ) ) { //System.err.println("FOUND LAST BYTE"); return - 2 ; // LAST BYTE } else if ( ( pEnd - 2 >= 0 ) && ( pBytes [ pEnd - 2 ] == header [ 0 ] ) && ( pBytes [ pEnd - 1 ] == header [ 1 ] ) ) { //System.err.println("FOUND LAST TWO BYTES"); return - 3 ; // LAST TWO BYTES } else if ( ( pEnd - 3 >= 0 ) && ( pBytes [ pEnd - 3 ] == header [ 0 ] ) && ( pBytes [ pEnd - 2 ] == header [ 1 ] ) && ( pBytes [ pEnd - 1 ] == header [ 2 ] ) ) { //System.err.println("FOUND LAST THREE BYTES"); return - 4 ; // LAST THREE BYTES } return - 1 ; // NO BYTES MATCH }
Finds the end of the HTTP response header in an array of bytes .