/*
 * Copyright (c) 2012-2022 The ANTLR Project. All rights reserved.
 * Use of this file is governed by the BSD 3-clause license that
 * can be found in the LICENSE.txt file in the project root.
 */

package org.antlr.v4.test.runtime;

import org.antlr.v4.runtime.misc.Utils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.EnumSet;

import static org.antlr.v4.test.runtime.RuntimeTestUtils.FileSeparator;

public class FileUtils {
	public static void writeFile(String dir, String fileName, String content) {
		try {
			Utils.writeFile(dir + FileSeparator + fileName, content, "UTF-8");
		}
		catch (IOException ioe) {
			System.err.println("can't write file");
			ioe.printStackTrace(System.err);
		}
	}

	public static String readFile(String dir, String fileName) {
		try {
			return String.copyValueOf(Utils.readFile(dir+"/"+fileName, "UTF-8"));
		}
		catch (IOException ioe) {
			System.err.println("can't read file");
			ioe.printStackTrace(System.err);
		}
		return null;
	}

	public static void copyFile(File source, File dest) throws IOException {
		InputStream is = Files.newInputStream(source.toPath());
		OutputStream os = Files.newOutputStream(dest.toPath());
		byte[] buf = new byte[4 << 10];
		int l;
		while ((l = is.read(buf)) > -1) {
			os.write(buf, 0, l);
		}
		is.close();
		os.close();
	}

	public static boolean moveFile(File sourceFir, String destDir, String fileName) {
		File file = new File(sourceFir, fileName);
		File newFile = new File(destDir, fileName);
		if (newFile.exists()) {
			if (!newFile.delete()) {
				return false;
			}
		}
		return file.renameTo(newFile);
	}

	public static void replaceInFile(Path sourcePath, String target, String replacement) throws IOException {
		replaceInFile(sourcePath, sourcePath, target, replacement);
	}

	public static void replaceInFile(Path sourcePath, Path destPath, String target, String replacement) throws IOException {
		String content = new String(Files.readAllBytes(sourcePath), StandardCharsets.UTF_8);
		String newContent = content.replace(target, replacement);
		try (PrintWriter out = new PrintWriter(destPath.toString())) {
			out.println(newContent);
		}
	}

	public static void mkdir(String dir) {
		File f = new File(dir);
		f.mkdirs();
	}

	public static void copyDirectory(final Path source, final Path target, final CopyOption... options)
			throws IOException {
		Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), 2147483647, new SimpleFileVisitor<Path>() {
			@Override
			public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
					throws IOException {
				Files.createDirectories(target.resolve(source.relativize(dir)));
				return FileVisitResult.CONTINUE;
			}

			@Override
			public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
					throws IOException {
				Files.copy(file, target.resolve(source.relativize(file)), options);
				return FileVisitResult.CONTINUE;
			}
		});
	}

	public static void deleteDirectory(File f) throws IOException {
		if (f.isDirectory()) {
			File[] files = f.listFiles();
			if (files != null) {
				for (File c : files)
					deleteDirectory(c);
			}
		}
		if (!f.delete())
			throw new IOException("Failed to delete file: " + f);
	}
}

@@ -358,86 +358,8 @@
 e.*;
-%0Aimport java.nio.file.attribute.BasicFileAttributes;%0Aimport java.util.EnumSet;
 %0A%0Aim
@@ -1028,1938 +1028,651 @@
 oid 
-copyFile(File source, File dest) throws IOException %7B%0A%09%09InputStream is = Files.newInputStream(source.toPath());%0A%09%09OutputStream os = Files.newOutputStream(dest.toPath());%0A%09%09byte%5B%5D buf = new byte%5B4 %3C%3C 10%5D;%0A%09%09int l;%0A%09%09while ((l = is.read(buf)) %3E -1) %7B%0A%09%09%09os.write(buf, 0, l);%0A%09%09%7D%0A%09%09is.close();%0A%09%09os.close();%0A%09%7D%0A%0A%09public static boolean moveFile(File sourceFir, String destDir, String fileName) %7B%0A%09%09File file = new File(sourceFir, fileName);%0A%09%09File newFile = new File(destDir, fileName);%0A%09%09if (newFile.exists()) %7B%0A%09%09%09if (!newFile.delete()) %7B%0A%09%09%09%09return false;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return file.renameTo(newFile);%0A%09%7D%0A%0A%09public static void replaceInFile(Path sourcePath, String target, String replacement) throws IOException %7B%0A%09%09replaceInFile(sourcePath, sourcePath, target, replacement);%0A%09%7D%0A%0A%09public static void replaceInFile(Path sourcePath, Path destPath, String target, String replacement) throws IOException %7B%0A%09%09String content = new String(Files.readAllBytes(sourcePath), StandardCharsets.UTF_8);%0A%09%09String newContent = content.replace(target, replacement);%0A%09%09try (PrintWriter out = new PrintWriter(destPath.toString())) %7B%0A%09%09%09out.println(newContent);%0A%09%09%7D%0A%09%7D%0A%0A%09public static void mkdir(String dir) %7B%0A%09%09File f = new File(dir);%0A%09%09f.mkdirs();%0A%09%7D%0A%0A%09public static void copyDirectory(final Path source, final Path target, final CopyOption... options)%0A%09%09%09throws IOException %7B%0A%09%09Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), 2147483647, new SimpleFileVisitor%3CPath%3E() %7B%0A%09%09%09@Override%0A%09%09%09public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)%0A%09%09%09%09%09throws IOException %7B%0A%09%09%09%09Files.createDirectories(target.resolve(source.relativize(dir)));%0A%09%09%09%09return FileVisitResult.CONTINUE;%0A%09%09%09%7D%0A%0A%09%09%09@Override%0A%09%09%09public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)%0A%09%09%09%09%09throws IOException %7B%0A%09%09%09%09Files.copy(file, target.resolve(source.relativize(file)), options);%0A%09%09%09%09return FileVisitResult.CONTINUE;%0A%09%09%09%7D%0A%09%09%7D
+replaceInFile(Path sourcePath, String target, String replacement) throws IOException %7B%0A%09%09replaceInFile(sourcePath, sourcePath, target, replacement);%0A%09%7D%0A%0A%09public static void replaceInFile(Path sourcePath, Path destPath, String target, String replacement) throws IOException %7B%0A%09%09String content = new String(Files.readAllBytes(sourcePath), StandardCharsets.UTF_8);%0A%09%09String newContent = content.replace(target, replacement);%0A%09%09try (PrintWriter out = new PrintWriter(destPath.toString())) %7B%0A%09%09%09out.println(newContent);%0A%09%09%7D%0A%09%7D%0A%0A%09public static void mkdir(String dir) %7B%0A%09%09File f = new File(dir);%0A%09%09//noinspection ResultOfMethodCallIgnored%0A%09%09f.mkdirs(
 );%0A%09

Remove not used methods from FileUtils (runtime tests)