private boolean downloadPiece(int len)
	{
		piece_to_download = peer.getPeerResponseInt();
		offset = peer.getPeerResponseInt() / block_length;
		logger.debug("i = " + piece_to_download + ", o = " + offset);
		
		
		byte[] received_bytes = peer.getPeerResponse(len - 9);
		onDownload(received_bytes.length);
		byte[] tmp_buffer = buf.array();
		
		//allocate only the amount of bytes required
		
		//if last piece and last block
		if(downloaded_so_far+1==blocks_per_piece && isLastPiece())
			buf = ByteBuffer.allocate(buf.capacity()+ (torrent.file_length % block_length));
		else //else
			buf = ByteBuffer.allocate(buf.capacity()+block_length);
		
		//put the old bytes + the new received_bytes in the buffer
		buf.put(tmp_buffer);
		buf.put(received_bytes);
		
		//if piece complete or if last block of last piece then verify, send have msg & write
		if(downloaded_so_far+1==blocks_per_piece || (downloaded_so_far+1==blocks_for_last_piece && isLastPiece()))
		{	
			if(verify(piece_to_download,buf.array())){
				rubt.writeBytes(buf,piece_to_download);
				rubt.leftBytes(buf.array().length);
				rubt.pieceDownloaded(piece_to_download);
				controller.sendHave(piece_to_download,this);
			}
			buf = ByteBuffer.allocate(0);
		}
		
		received_bytes = null;
		downloaded_so_far++;
		

		
		//if not last block
		if (downloaded_so_far + 1 < blocks_per_piece)
		{
				//requesting next block of data for the same piece
				peer.request(piece_to_download, ++offset * block_length, block_length);
		}
		//if last block
		else if (downloaded_so_far + 1 == blocks_per_piece)
		{
			//This is the last block that needs to be downloaded
			//It may have an irregular length (not 16384 bytes)
			
			int length;
			
			if(isLastPiece())
				length = torrent.file_length % block_length;
			else
				length = block_length;
			
			peer.request(piece_to_download, ++offset * block_length, length);
			
		} else
		{
			//piece downloaded. break out and get next piece
			downloaded_so_far = 0;
			offset = 0;
			buf = ByteBuffer.allocate(0);
			return true;
		}
		
		return false;
	}