public void writeBinaryFile(final String path, final String mode, final LoggedDataInputStream dis, int length)
                    throws IOException {
        if (DEBUG) {
            System.err.println("[writeBinaryFile] writing: " + path); // NOI18N
            System.err.println("[writeBinaryFile] length: " + length); // NOI18N
            System.err.println("Reader object is: " + dis.hashCode()); // NOI18N
        }

        final File file = new File(path);

        final boolean readOnly = resetReadOnly(file, mode);

        createNewFile(file);
        // FUTURE: optimisation possible - no need to use a temp file if there
        // is no post processing required (e.g. unzipping). So perhaps enhance
        // the interface to allow this stage to be optional
        final File cvsDir = new File(file.getParentFile(), "CVS");
        cvsDir.mkdir();
        final File tempFile = File.createTempFile("cvsPostConversion", "tmp", cvsDir); // NOI18N

        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tempFile));

            final byte[] chunk = new byte[CHUNK_SIZE];
            try {
                while (length > 0) {
                    final int bytesToRead = (length >= CHUNK_SIZE) ? CHUNK_SIZE : (int) length;
                    final int count = dis.read(chunk, 0, bytesToRead);
                    if (count == -1) {
                        throw new IOException("Unexpected end of stream: " + path + "\nMissing " + length
                                        + " bytes. Probably network communication failure.\nPlease try again."); // NOI18N
                    }
                    if (count < 0) {
                        break;
                    }

                    length -= count;
                    if (DEBUG) {
                        System.err.println("Still got: " + length + " to read"); // NOI18N
                    }
                    bos.write(chunk, 0, count);
                }
            } finally {
                bos.close();
            }

            // Here we read the temp file in, taking the opportunity to process
            // the file, e.g. unzip the data
            final BufferedInputStream tempIS = new BufferedInputStream(getProcessedInputStream(tempFile));
            bos = new BufferedOutputStream(createOutputStream(file));

            try {
                for (int count = tempIS.read(chunk, 0, CHUNK_SIZE); count > 0; count = tempIS
                                .read(chunk, 0, CHUNK_SIZE)) {
                    bos.write(chunk, 0, count);
                }
            } finally {
                bos.close();
                tempIS.close();
            }

            // now we need to modifiy the timestamp on the file, if specified
            if (modifiedDate != null) {
                file.setLastModified(modifiedDate.getTime());
                modifiedDate = null;
            }
        } finally {
            tempFile.delete();
        }

	setFileMode(mode, file);
        if (readOnly) {
            FileUtils.setFileReadOnly(file, true);
        }
    }