Buggy code:
public class HttpMirrorThread implements Runnable { private static final Logger log = LoggingManager . getLoggerForClass ( ) ; private static final String ISO_8859_1 = "ISO-8859-1" ; private static final byte [ ] CRLF = new byte [ ] { 13 , 10 } ; private final Socket clientSocket ; public HttpMirrorThread ( Socket _clientSocket ) { } public void run ( ) { HttpMirrorThread . log . debug ( "Starting<seq2seq4repair_space>thread" ) ; BufferedInputStream in = null ; BufferedOutputStream out = null ; try { in = new BufferedInputStream ( clientSocket . getInputStream ( ) ) ; int contentLength = - 1 ; boolean isChunked = false ; byte [ ] buffer = new byte [ 1024 ] ; StringBuilder headers = new StringBuilder ( ) ; int length = 0 ; int positionOfBody = 0 ; ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ; while ( ( positionOfBody <= 0 ) && ( ( length = in . read ( buffer ) ) != ( - 1 ) ) ) { HttpMirrorThread . log . debug ( "Write<seq2seq4repair_space>body" ) ; baos . write ( buffer , 0 , length ) ; headers . append ( new String ( buffer , 0 , length , HttpMirrorThread . ISO_8859_1 ) ) ; positionOfBody = HttpMirrorThread . getPositionOfBody ( headers . toString ( ) ) ; } baos . close ( ) ; final String headerString = headers . toString ( ) ; HttpMirrorThread . log . debug ( "Write<seq2seq4repair_space>headers" ) ; out = new BufferedOutputStream ( clientSocket . getOutputStream ( ) ) ; out . write ( "HTTP//1.0<seq2seq4repair_space>200<seq2seq4repair_space>OK" . getBytes ( HttpMirrorThread . ISO_8859_1 ) ) ; out . write ( HttpMirrorThread . CRLF ) ; out . write ( "Content-Type:<seq2seq4repair_space>text//plain" . getBytes ( HttpMirrorThread . ISO_8859_1 ) ) ; out . write ( HttpMirrorThread . CRLF ) ; String cookieHeaderValue = HttpMirrorThread . getRequestHeaderValue ( headerString , "X-SetCookie" ) ; if ( cookieHeaderValue != null ) { out . write ( "Set-Cookie:<seq2seq4repair_space>" . getBytes ( HttpMirrorThread . ISO_8859_1 ) ) ; out . write ( cookieHeaderValue . getBytes ( HttpMirrorThread . ISO_8859_1 ) ) ; out . write ( HttpMirrorThread . CRLF ) ; } out . write ( HttpMirrorThread . CRLF ) ; out . flush ( ) ; out . write ( baos . toByteArray ( ) ) ; String contentLengthHeaderValue = HttpMirrorThread . getRequestHeaderValue ( headerString , "Content-Length" ) ; if ( contentLengthHeaderValue != null ) { <START_BUG> contentLength = Integer . valueOf ( contentLengthHeaderValue ) . intValue ( ) ; <END_BUG> } String sleepHeaderValue = HttpMirrorThread . getRequestHeaderValue ( headerString , "X-Sleep" ) ; if ( sleepHeaderValue != null ) { Thread . sleep ( Integer . parseInt ( sleepHeaderValue ) ) ; } String transferEncodingHeaderValue = HttpMirrorThread . getRequestHeaderValue ( headerString , "Transfer-Encoding" ) ; if ( transferEncodingHeaderValue != null ) { isChunked = transferEncodingHeaderValue . equalsIgnoreCase ( "chunked" ) ; if ( ! isChunked ) { HttpMirrorThread . log . error ( ( "Transfer-Encoding<seq2seq4repair_space>header<seq2seq4repair_space>set,<seq2seq4repair_space>the<seq2seq4repair_space>value<seq2seq4repair_space>is<seq2seq4repair_space>not<seq2seq4repair_space>supported<seq2seq4repair_space>:<seq2seq4repair_space>" + transferEncodingHeaderValue ) ) ; } } length = 0 ; if ( contentLength > 0 ) { int totalReadBytes = ( ( headerString . length ( ) ) - positionOfBody ) - 2 ; HttpMirrorThread . log . debug ( ( ( ( "Reading,<seq2seq4repair_space>" + totalReadBytes ) + "<seq2seq4repair_space><<seq2seq4repair_space>" ) + contentLength ) ) ; while ( ( totalReadBytes < contentLength ) && ( ( length = in . read ( buffer ) ) != ( - 1 ) ) ) { HttpMirrorThread . log . debug ( ( "Read<seq2seq4repair_space>bytes:<seq2seq4repair_space>" + length ) ) ; out . write ( buffer , 0 , length ) ; totalReadBytes += length ; HttpMirrorThread . log . debug ( ( "totalReadBytes:<seq2seq4repair_space>" + totalReadBytes ) ) ; } } else if ( isChunked ) { HttpMirrorThread . log . debug ( "Chunked" ) ; while ( ( ( in . available ( ) ) > 0 ) && ( ( length = in . read ( buffer ) ) != ( - 1 ) ) ) { out . write ( buffer , 0 , length ) ; } } else { HttpMirrorThread . log . debug ( "Other" ) ; while ( ( ( in . available ( ) ) > 0 ) && ( ( length = in . read ( buffer ) ) != ( - 1 ) ) ) { HttpMirrorThread . log . debug ( ( "Read<seq2seq4repair_space>bytes:<seq2seq4repair_space>" + length ) ) ; out . write ( buffer , 0 , length ) ; } } HttpMirrorThread . log . debug ( "Flush" ) ; out . flush ( ) ; } catch ( IOException e ) { HttpMirrorThread . log . error ( "" , e ) ; } catch ( InterruptedException e ) { HttpMirrorThread . log . error ( "" , e ) ; } finally { JOrphanUtils . closeQuietly ( out ) ; JOrphanUtils . closeQuietly ( in ) ; JOrphanUtils . closeQuietly ( clientSocket ) ; } HttpMirrorThread . log . debug ( "End<seq2seq4repair_space>of<seq2seq4repair_space>Thread" ) ; } private static String getRequestHeaderValue ( String requestHeaders , String headerName ) { } private static int getPositionOfBody ( String stringToCheck ) { } }

Fix:
contentLength = Integer . parseInt ( contentLengthHeaderValue ) ;