@Test
	public void testPieceFeeding() throws InvalidBEncodingException, IOException, TorrentException, MetaInfoException, NullHashException, BlockLengthException, WrongIndexException, NoSuchAlgorithmException {
		
		MetaInfo metainfo = MetaInfoReader.openMetaInfo("trunk/data/LePetitPrince.torrent");
    	Torrent torrent = Torrent.createTorrent(metainfo);
		
		int pieceLength = torrent.getPieces().get(0).getSize();
		int pieceCount = torrent.getPieces().size();
		
		List<Integer> indices = new ArrayList<Integer>();
		for(int i = 0; i < pieceCount; i++) {
			indices.add(i);
		}
		Collections.shuffle(indices);
		Assert.assertEquals(pieceCount, indices.size());
		Assert.assertEquals(pieceLength, 1 << 15);
		
		RandomAccessFile file = new RandomAccessFile("trunk/data/st_exupery_le_petit_prince.pdf", "r");
		for(int index: indices) {
			Piece piece = torrent.getPieces().get(index);
			
			// do the second block first, if any
			int secondBlockSize = piece.getSize() - Block.BLOCK_SIZE;
			if(secondBlockSize > 0) {
				file.seek(index * pieceLength + Block.BLOCK_SIZE);
				byte[] block = new byte[secondBlockSize];
				file.read(block);
				
				try {
					piece.feed(Block.BLOCK_SIZE, new Block(block));
				} catch (NullHashException e) {}
				catch (BlockLengthException e) {}
				catch (WrongIndexException e) {}
			}
			
			// now do the first block
			{
				int firstBlockSize = Math.min(Block.BLOCK_SIZE, piece.getSize()); 
				file.seek(index * pieceLength);
				byte[] block = new byte[firstBlockSize];
				file.read(block);
				
				try {
					piece.feed(0, new Block(block));
				} catch (NullHashException e) {}
				catch (BlockLengthException e) {} 
				catch (WrongIndexException e) {}
			}
		}
		file.close();
		//torrent.getPieceManager().showProgress();
		torrent.getPieceManager().updatePriorities();
		Assert.assertTrue(torrent.isComplete());
	}