private void writeAndPostProcessTextFile(final String path, final String mode, final LoggedDataInputStream dis,
                    int length, final WriteTextFilePreprocessor processor) throws IOException {
        if (DEBUG) {
            System.err.println("[writeTextFile] writing: " + path); // NOI18N
            System.err.println("[writeTextFile] 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);
        
        // For CRLF conversion, we have to read the file
        // into a temp file, then do the conversion. This is because we cannot
        // perform a sequence of readLines() until we've read the file from
        // the server - the file transmission is not followed by a newline.
        // Bah.
        final File tempFile = File.createTempFile("cvsCRLF", "tmp"); // NOI18N

        try {
            OutputStream os = null;
            try {
                os = new BufferedOutputStream(new FileOutputStream(tempFile));
                final byte[] chunk = new byte[CHUNK_SIZE];
                while (length > 0) {
                    int count = (length >= CHUNK_SIZE) ? CHUNK_SIZE : length;
                    count = dis.read(chunk, 0, count);
                    if (count == -1) {
                        throw new IOException("Unexpected end of stream: " + path + "\nMissing " + length
                                        + " bytes. Probably network communication failure.\nPlease try again."); // NOI18N
                    }
                    length -= count;
                    if (DEBUG) {
                        System.err.println("Still got: " + length + " to read"); // NOI18N
                    }
                    os.write(chunk, 0, count);
                }
            } finally {
                if (os != null) {
                    try {
                        os.close();
                    } catch (final IOException ex) {
                        // ignore
                    }
                }
            }

            // Here we read the temp file in again, doing any processing
            // required
            // (for example, unzipping). We must not convert bytes to characters
            // because it would break characters that are not in the current
            // encoding
            final InputStream tempInput = getProcessedInputStream(tempFile);

            try {
                // BUGLOG - assert the processor is not null..
                processor.copyTextFileToLocation(tempInput, file, new StreamProvider(file));
            } finally {
                tempInput.close();
            }

            if (modifiedDate != null) {
                file.setLastModified(modifiedDate.getTime());
                modifiedDate = null;
            }
        } finally {
            tempFile.delete();
        }

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