Code example for ByteBuffer

Methods: flip

0
	synchronized 
	public void consume(ByteBuffer piece) {
		join(piece);
		if (piecesToJoin != 0) return;
		 
		whole.flip();
		largePacketReceiver.consume(whole);
		whole = null;
	} 
	 
 
	private void join(ByteBuffer piece) {
		if (whole == null) {
			int pieceSize = piece.remaining();
			piecesToJoin = piece.get() & 0xFF;
			whole = ByteBuffer.allocate((1 + piecesToJoin) * pieceSize - BYTES_FOR_REMAINING_PIECES);
		} else { 
			piecesToJoin--;
		} 
		 
		whole.put(piece);