public synchronized void cancel(int index, int begin, int length) {
        Collection<Message> messagesToCancel = new LinkedList<Message>();
        for (Message elem : messagesToBeSent) {
            if (elem.getType() == Message.PIECE) {
                Piece block = (Piece) elem;
                if (block.getIndex() == index && block.getBegin() == begin && block.getLength() == length) {
                    messagesToCancel.add(elem);
                }
            }
        }

        messagesToBeSent.removeAll(messagesToCancel);
    }