<commit_before>package com.redhat.ceylon.common.config;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * Serializes {@link CeylonConfig} instances to streams and files.
 * 
 * All serialization uses the UTF-8 character encoding.
 */
public class ConfigWriter {

    /**
     * Write the given configuration to the given file. Updating the existing file if
     * it exists or otherwise creating a new file.
     */
    public static void write(CeylonConfig config, File destination) throws IOException {
        OutputStream out = null;
        if (destination.isFile()) {
            write(config, destination, destination);
        } else {
            try {
                // First create any parent directories if necessary
                File parentDir = destination.getAbsoluteFile().getParentFile();
                if (!parentDir.exists()) {
                    parentDir.mkdirs();
                }
                // Now create the file itself
                out = new FileOutputStream(destination);
                write(config, out);
            } finally {
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) { }
                }
            }
        }
    }

    /**
     * Reads config from the given source file, updating it using the given 
     * configuration and writing in to the destination file. 
     */
    public static void write(CeylonConfig config, File source, File destination) throws IOException {
        boolean overwriteSource = destination.getCanonicalFile().equals(source.getCanonicalFile());
        if (source.isFile()) {
            InputStream in = null;
            OutputStream out = null;
            File tmpFile = null;
            boolean ok = false;
            try {
                try {
                    in = new FileInputStream(source);
                    if (overwriteSource) {
                        // Send the output to a temporary file first
                        tmpFile = File.createTempFile(source.getName(), ".tmp", source.getAbsoluteFile().getParentFile());
                        out = new FileOutputStream(tmpFile);
                    } else {
                        // First create any parent directories if necessary
                        File parentDir = destination.getAbsoluteFile().getParentFile();
                        if (!parentDir.exists()) {
                            parentDir.mkdirs();
                        }
                        // Now create the file itself
                        out = new FileOutputStream(destination);
                    }
                    write(config, in, out);
                    ok = true;
                } finally {
                    if (out != null) {
                        try {
                            out.close();
                        } catch (IOException e) { }
                    }
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) { }
                    }
                }
                if (ok) {
                    File sourceBackup = new File(source.getAbsoluteFile().getParentFile(), source.getName() + ".old");
                    Files.deleteIfExists(sourceBackup.toPath());
                    Files.move(source.toPath(), sourceBackup.toPath());
                    Files.move(tmpFile.toPath(), source.toPath());
                }
            } finally {
                if (tmpFile != null) {
                    Files.deleteIfExists(tmpFile.toPath());
                }
            }
        } else {
            throw new FileNotFoundException("Couldn't open source configuration file");
        }
    }

    /**
     * Reads config from the given source file, updating it using the given 
     * configuration and writing in to the given output.
     */
    public static void write(CeylonConfig config, File source, OutputStream out) throws IOException {
        if (source.isFile()) {
            InputStream in = null;
            try {<commit_msg>Now using the more common extension `~` for backup files instead of `.old`<commit_after>package com.redhat.ceylon.common.config;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * Serializes {@link CeylonConfig} instances to streams and files.
 * 
 * All serialization uses the UTF-8 character encoding.
 */
public class ConfigWriter {

    /**
     * Write the given configuration to the given file. Updating the existing file if
     * it exists or otherwise creating a new file.
     */
    public static void write(CeylonConfig config, File destination) throws IOException {
        OutputStream out = null;
        if (destination.isFile()) {
            write(config, destination, destination);
        } else {
            try {
                // First create any parent directories if necessary
                File parentDir = destination.getAbsoluteFile().getParentFile();
                if (!parentDir.exists()) {
                    parentDir.mkdirs();
                }
                // Now create the file itself
                out = new FileOutputStream(destination);
                write(config, out);
            } finally {
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) { }
                }
            }
        }
    }

    /**
     * Reads config from the given source file, updating it using the given 
     * configuration and writing in to the destination file. 
     */
    public static void write(CeylonConfig config, File source, File destination) throws IOException {
        boolean overwriteSource = destination.getCanonicalFile().equals(source.getCanonicalFile());
        if (source.isFile()) {
            InputStream in = null;
            OutputStream out = null;
            File tmpFile = null;
            boolean ok = false;
            try {
                try {
                    in = new FileInputStream(source);
                    if (overwriteSource) {
                        // Send the output to a temporary file first
                        tmpFile = File.createTempFile(source.getName(), ".tmp", source.getAbsoluteFile().getParentFile());
                        out = new FileOutputStream(tmpFile);
                    } else {
                        // First create any parent directories if necessary
                        File parentDir = destination.getAbsoluteFile().getParentFile();
                        if (!parentDir.exists()) {
                            parentDir.mkdirs();
                        }
                        // Now create the file itself
                        out = new FileOutputStream(destination);
                    }
                    write(config, in, out);
                    ok = true;
                } finally {
                    if (out != null) {
                        try {
                            out.close();
                        } catch (IOException e) { }
                    }
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) { }
                    }
                }
                if (ok) {
                    File sourceBackup = new File(source.getAbsoluteFile().getParentFile(), source.getName() + "~");
                    Files.deleteIfExists(sourceBackup.toPath());
                    Files.move(source.toPath(), sourceBackup.toPath());
                    Files.move(tmpFile.toPath(), source.toPath());
                }
            } finally {
                if (tmpFile != null) {
                    Files.deleteIfExists(tmpFile.toPath());
                }
            }
        } else {
            throw new FileNotFoundException("Couldn't open source configuration file");
        }
    }

    /**
     * Reads config from the given source file, updating it using the given 
     * configuration and writing in to the given output.
     */
    public static void write(CeylonConfig config, File source, OutputStream out) throws IOException {
        if (source.isFile()) {
            InputStream in = null;
            try {
{'fraction_non_alphanumeric': 0.08803362088033621, 'fraction_numerical': 0.00022119000221190003, 'mean_word_length': 1.4250469294717083, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3867015', 'n_tokens_mistral': 2120, 'n_tokens_neox': 1984, 'n_words': 604}