private static InputStream detatchResponseHeader ( BufferedInputStream pIS ) throws IOException { // Store header in byte array ByteArrayOutputStream bytes = new ByteArrayOutputStream ( ) ; pIS . mark ( BUF_SIZE ) ; byte [ ] buffer = new byte [ BUF_SIZE ] ; int length ; int headerEnd ; // Read from iput, store in bytes while ( ( length = pIS . read ( buffer ) ) != - 1 ) { // End of header? headerEnd = findEndOfHeader ( buffer , length ) ; if ( headerEnd >= 0 ) { // Write rest bytes . write ( buffer , 0 , headerEnd ) ; // Go back to last mark pIS . reset ( ) ; // Position stream to right after header, and exit loop pIS . skip ( headerEnd ) ; break ; } else if ( headerEnd < - 1 ) { // Write partial (except matching header bytes) bytes . write ( buffer , 0 , length - 4 ) ; // Go back to last mark pIS . reset ( ) ; // Position stream to right before potential header end pIS . skip ( length - 4 ) ; } else { // Write all bytes . write ( buffer , 0 , length ) ; } // Can't read more than BUF_SIZE ahead anyway pIS . mark ( BUF_SIZE ) ; } return new ByteArrayInputStream ( bytes . toByteArray ( ) ) ; }
Reads the header part of the response and copies it to a different InputStream .