Buggy code:
public class HttpMirrorThread extends Thread { 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 ) { } @ Override public void run ( ) { HttpMirrorThread . log . info ( "Starting<seq2seq4repair_space>thread" ) ; BufferedInputStream in = null ; BufferedOutputStream out = null ; try { in = new BufferedInputStream ( clientSocket . getInputStream ( ) ) ; 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 ) ; out . write ( HttpMirrorThread . CRLF ) ; out . flush ( ) ; int contentLength = - 1 ; boolean isChunked = false ; byte [ ] buffer = new byte [ 1024 ] ; <START_BUG> StringBuffer headers = new StringBuffer ( ) ; <END_BUG> int length = 0 ; int positionOfBody = 0 ; while ( ( positionOfBody <= 0 ) && ( ( length = in . read ( buffer ) ) != ( - 1 ) ) ) { out . write ( buffer , 0 , length ) ; headers . append ( new String ( buffer , 0 , length , HttpMirrorThread . ISO_8859_1 ) ) ; positionOfBody = HttpMirrorThread . getPositionOfBody ( headers . toString ( ) ) ; } final String headerString = headers . toString ( ) ; String contentLengthHeaderValue = HttpMirrorThread . getRequestHeaderValue ( headerString , "Content-Length" ) ; if ( contentLengthHeaderValue != null ) { contentLength = new Integer ( contentLengthHeaderValue ) . intValue ( ) ; } 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 ; while ( ( totalReadBytes < contentLength ) && ( ( length = in . read ( buffer ) ) != ( - 1 ) ) ) { out . write ( buffer , 0 , length ) ; totalReadBytes += length ; } } else if ( isChunked ) { while ( ( ( in . available ( ) ) > 0 ) && ( ( length = in . read ( buffer ) ) != ( - 1 ) ) ) { out . write ( buffer , 0 , length ) ; } } else { while ( ( ( in . available ( ) ) > 0 ) && ( ( length = in . read ( buffer ) ) != ( - 1 ) ) ) { out . write ( buffer , 0 , length ) ; } } 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 . info ( "End<seq2seq4repair_space>of<seq2seq4repair_space>Thread" ) ; } private static String getRequestHeaderValue ( String requestHeaders , String headerName ) { } private static int getPositionOfBody ( String stringToCheck ) { } }

Fix:
StringBuilder headers = new StringBuilder ( ) ;