public void write(int begin, byte[] block) throws IOException {

        int filePieceIndex = findFilePieceIndex(begin);
        FilePieceMapper filePiece = files.get(filePieceIndex);

        int writtenBytes = 0;
        while (writtenBytes < block.length) {
            RandomAccessFile raf = filePiece.getFile();
            Long seek = filePiece.getFileOffset() + ((begin + writtenBytes) - filePiece.getPieceOffset());
            raf.seek(seek);

            int byteToWrite = block.length - writtenBytes;
            Long byteAvaiableInThisFile = raf.length() - seek;

            Long byteAvaiableToWrite = byteToWrite < byteAvaiableInThisFile ? byteToWrite : byteAvaiableInThisFile;
            raf.write(block, writtenBytes, byteAvaiableToWrite.intValue());
            writtenBytes += byteAvaiableToWrite.intValue();

            if (byteAvaiableToWrite.equals(byteAvaiableInThisFile) && writtenBytes < block.length) {
                filePiece = files.get(++filePieceIndex);
            }
        }


        addPieceBlock(begin, block.length);

        if (isCompleted()) {
            if (!checkSha1()) {
                blocks.clear();
                throw new IOException("sha check failed");
            }
        }
    }