package liquibase.util;

import liquibase.Scope;
import liquibase.GlobalConfiguration;

import java.io.*;

public class FileUtil {
    
    private FileUtil() {
        throw new IllegalStateException("This utility class must not be instantiated. Sorry.");
    }

    /**
     * Clean a directory without deleting it.
     */
    private static void cleanDirectory(final File directory) throws IOException {
        if ( !directory.exists() ) {
            return;
        }

        if ( !directory.isDirectory() ) {
            return;
        }

        IOException exception = null;

        final File[] files = directory.listFiles();
        if (files != null) {
            for (final File file : files) {
                try {
                    cleanDirectory(file);
                    if (!file.delete()) {
                        throw new IOException("Cannot delete "+file.getAbsolutePath());
                    }
                } catch (final IOException ioe) {
                    exception = ioe;
                }
            }
        }

        if ( null != exception ) {
            throw exception;
        }
    }

    public static String getContents(File file) throws IOException {
        if (!file.exists()) {
            return null;
        }
        try (
            FileInputStream fileInputStream = new FileInputStream(file);
        ) {
            
            return StreamUtil.readStreamAsString(fileInputStream);
        } catch (FileNotFoundException e) {
            return null;
        }
    }

    public static void write(String contents, File file) throws IOException {
        file.getParentFile().mkdirs();
        
        try (
            FileOutputStream output = new FileOutputStream(file);
        ){
            StreamUtil.copy(new ByteArrayInputStream(contents.getBytes(GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue())), output);
        }
    }

    public static String getFileNotFoundMessage(String physicalChangeLogLocation) {
        String message = "The file " + physicalChangeLogLocation + " was not found in" + System.lineSeparator();
        for (String location : Scope.getCurrentScope().getResourceAccessor().describeLocations()) {
            message += "    - " + location + System.lineSeparator();
        }
        message += "Specifying files by absolute path was removed in Liquibase 4.0. Please use a relative path or add '/' to the classpath parameter.";

        return message;
    }


}

@@ -1602,32 +1602,171 @@
 s IOException %7B%0A
+        write(contents, file, false);%0A    %7D%0A%0A    public static void write(String contents, File file, boolean append) throws IOException %7B%0A
         file.get
@@ -1788,24 +1788,16 @@
 dirs();%0A
-        
 %0A       
@@ -1795,32 +1795,36 @@
 %0A%0A        try (%0A
+    
             File
@@ -1862,34 +1862,41 @@
 utputStream(file
+, append
 )
-;
 %0A        )%7B%0A    
@@ -2035,32 +2035,33 @@
 put);%0A        %7D%0A
+%0A
     %7D%0A%0A    publi

add overloaded method to FileUtil that allows for overwriting existing file (#2040)