IOUtils.java

package libsidutils;

import net.java.truevfs.access.TArchiveDetector;
import net.java.truevfs.access.TFile;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.nio.channels.WritableByteChannel;
import java.nio.charset.Charset;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.DecimalFormat;
import java.util.*;
import java.util.Map.Entry;
import java.util.function.BooleanSupplier;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;

/**
 * This class provides general IO utility functions.
 *
 * @author Ken Händel
 */
public class IOUtils {
    /**
     * Linux, OSX, and ZIP entries use slash, Windows uses backslash.
     */
    private static final Pattern SEPARATOR = Pattern.compile("[/\\\\]");

    private static final int COPY_BUFFER_CHUNK_SIZE = 16 * 1024;

    private static final int BUFFER_LENGTH = 64;

    /**
     * Create a filename of the given path relative to the collection root dir. <BR> e.g.
     * "&lt;root&gt;/MUSICIANS/D/Daglish_Ben/Bombo.sid" -&gt; "/MUSICIANS/D/Daglish_Ben/Bombo.sid"
     *
     * @param collectionRoot root file of the path
     * @param file           file to get the relative path for
     * @return relative path to the collection root file (empty string, if the path is not relative to the collection
     * root file)
     */
    public static String getCollectionName(final File collectionRoot, final File file) {
        return createFilename(getFiles(file.getPath(), collectionRoot, null));
    }

    /**
     * Reverse function of {@link #getFiles(String, File, FileFilter)}.<BR> e.g. [File(D), File(Daglish_Ben),
     * File(Bombo.sid)] -&gt; "/MUSICIANS/D/Daglish_Ben/Bombo.sid"
     *
     * @param files file list to create a filename for
     * @return filename where a slash delimits each path segment.
     */
    private static String createFilename(List<File> files) {
        StringBuilder result = new StringBuilder();
        for (File file : files) {
            result.append("/").append(file.getName());
        }
        return result.toString();
    }

    /**
     * Get a file for a given path. The path can be relative to HVSC or CGSC or even absolute.<BR> e.g.
     * "&lt;root&gt;/MUSICIANS/D/Daglish_Ben/Bombo.sid" -&gt; File(/MUSICIANS/D/Daglish_Ben/Bombo.sid)
     *
     * @param path     the path to get a file for, possible root directory can be either hvscRoot or cgscRoot or none,
     *                 if absolute
     * @param hvscRoot root of HVSC
     * @param cgscRoot root of CGSC
     * @return file of the path
     */
    public static File getFile(String path, File hvscRoot, File cgscRoot) {
        List<File> files = getFiles(path, hvscRoot, null);
        if (!files.isEmpty()) {
            // relative path name of HVSC?
            return files.get(files.size() - 1);
        }
        files = getFiles(path, cgscRoot, null);
        if (!files.isEmpty()) {
            // relative path name of CGSC?
            return files.get(files.size() - 1);
        }
        // absolute path name
        return new File(path);
    }

    /**
     * Get the file list of the given file path. Each entry corresponds to a path segment. It is sorted from parent to
     * child.<BR> e.g. "&lt;root&gt;/MUSICIANS/D/Daglish_Ben/Bombo.sid" -&gt; [File(MUSICIANS), File(D),
     * File(Daglish_Ben), File(Bombo.sid)]
     *
     * @param path       file path to get the file list for. Each path segment is delimited by slash or backslash.
     * @param rootFile   Root file to start. The first path segment must match a direct child of rootPath and so on.
     * @param fileFilter Files contained in the file filter are visible as child files (null means filter disabled)
     * @return a file list sorted from the parent file to the child file (empty list, if the path is wrong or
     * incomplete)
     */
    public static List<File> getFiles(String path, File rootFile, FileFilter fileFilter) {
        if (rootFile == null) {
            return Collections.emptyList();
        }
        String rootPath = rootFile.getPath();
        if (path.startsWith(rootPath)) {
            // remove root folder and separator (not for ZIP file entries)
            path = path.substring(rootPath.length());
            if (!path.isEmpty()) {
                path = path.substring(1);
            }
        }
        final List<File> pathSegs = new ArrayList<>();
        try (Scanner scanner = new Scanner(path)) {
            scanner.useDelimiter(SEPARATOR);
            nextPathSeg:
            while (scanner.hasNext()) {
                final String pathSeg = scanner.next();
                File[] childFiles = rootFile.listFiles(fileFilter);
                if (childFiles != null) {
                    for (File childFile : childFiles) {
                        if (childFile.getName().equalsIgnoreCase(pathSeg)) {
                            pathSegs.add(rootFile = childFile);
                            continue nextPathSeg;
                        }
                    }
                }
                return Collections.emptyList();
            }
            return pathSegs;
        }
    }

    /**
     * Delete directory and contained files and subdirectories.
     *
     * @param directory directory to delete
     * @throws IOException error during delete
     */
    public static void deleteDirectory(File directory) throws IOException {
        Files.walkFileTree(Paths.get(directory.toURI()), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }
        });
    }

    /**
     * Strip suffix of a filename.
     *
     * @param filename filename to get the suffix for
     * @return filename without suffix (e.g. "Bombo.sid" -&gt; "Bombo")
     */
    public static String getFilenameWithoutSuffix(final String filename) {
        return filename.substring(0, filename.length() - getFilenameSuffix(filename).length());
    }

    /**
     * Get suffix of a filename.
     *
     * @param filename filename to get the suffix for
     * @return suffix of a filename (e.g. "Bombo.sid" -&gt; ".sid")
     */
    public static String getFilenameSuffix(final String filename) {
        int lastIndexOf = filename.lastIndexOf('.');
        return lastIndexOf != -1 ? filename.substring(lastIndexOf) : "";
    }

    /**
     * Add upper case file extensions to the file extensions list.
     *
     * @param fileExtensions file extensions to add to
     * @return file extensions with added upper case file extensions
     */
    public static List<String> addUpperCase(List<String> fileExtensions) {
        List<String> result = new ArrayList<>(fileExtensions);
        fileExtensions.stream().map(fileName -> fileName.toUpperCase(Locale.US)).forEach(result::add);
        return result;
    }

    /**
     * Get physical size in b, Kb, Mb, Gb, or Tb.
     *
     * @param size size to calculate physical size for
     * @return physical size
     */
    public static String getPhysicalSize(long size) {
        if (size <= 0) {
            return "0b";
        }
        final String[] units = new String[]{"b", "Kb", "Mb", "Gb", "Tb"};
        int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
        return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + units[digitGroups];
    }

    /**
     * Fast file channel-based copy.
     *
     * @param is input
     * @param os output
     * @throws IOException I/O error
     */
    public static void copy(InputStream is, OutputStream os) throws IOException {
        final ReadableByteChannel inputChannel = Channels.newChannel(is);
        final WritableByteChannel outputChannel = Channels.newChannel(os);
        final ByteBuffer buffer = ByteBuffer.allocateDirect(COPY_BUFFER_CHUNK_SIZE);

        while (inputChannel.read(buffer) != -1) {
            buffer.flip();
            outputChannel.write(buffer);
            buffer.compact();
        }
        buffer.flip();
        while (buffer.hasRemaining()) {
            outputChannel.write(buffer);
        }
    }

    /**
     * Convert InputStream to String using charset.
     *
     * @param is      input
     * @param charset charset
     * @return read string
     */
    public static String convertStreamToString(InputStream is, Charset charset) {
        return convertStreamToString(is, charset, new HashMap<>());
    }

    /**
     * Convert InputStream to String using charset and replace strings on demand.
     *
     * @param is           input
     * @param charset      charset
     * @param replacements replacement strings
     * @return read string
     */
    public static String convertStreamToString(InputStream is, Charset charset, Map<String, String> replacements) {
        try (java.util.Scanner s = new java.util.Scanner(is, charset.name())) {
            s.useDelimiter("\\A");
            String string = s.hasNext() ? s.next() : "";
            for (Entry<String, String> replacement : replacements.entrySet()) {
                string = string.replace("${" + replacement.getKey() + "}", replacement.getValue());
            }
            return string;
        }
    }

    /**
     * Read complete InputStream contents into a byte array
     *
     * @param is input
     * @return byte array
     * @throws IOException I/O error
     */
    public static byte[] readAllBytes(InputStream is) throws IOException {
        if (is == null)
            throw new IOException("no InputStream");
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int nRead;
        byte[] b = new byte[BUFFER_LENGTH];
        while ((nRead = is.read(b, 0, b.length)) != -1) {
            buffer.write(b, 0, nRead);
        }
        return buffer.toByteArray();
    }

    /**
     * Read a specific number of InputStream contents into a byte array
     *
     * @param is  input
     * @param b   byte array
     * @param off offset in a byte array
     * @param len number of bytes to read from input
     * @return byte array
     * @throws IOException I/O error
     */
    public static int readNBytes(InputStream is, byte[] b, int off, int len) throws IOException {
        if (off < 0 || len < 0 || len > b.length - off)
            throw new IndexOutOfBoundsException();
        int nRead = 0;
        while (nRead < len) {
            int count = is.read(b, off + nRead, len - nRead);
            if (count < 0)
                break;
            nRead += count;
        }
        return nRead;
    }

    /**
     * Eventually extract a file, if compressed (ZIP, GZIP, TAR, etc.), otherwise return the same file.
     *
     * @param baseDir   base directory to extract into a newly created subdirectory
     * @param inputFile input to extract
     * @return extracted file or same file (if it was uncompressed)
     * @throws IOException I/O error
     */
    public static File extract(File baseDir, File inputFile) throws IOException {
        if (inputFile != null) {
            TFile file = new TFile(inputFile);
            if (file.isEntry()) {
                File tmpDir = new File(baseDir, UUID.randomUUID().toString());
                ioWrap(tmpDir::mkdirs);

                File tmpFile;
                if (inputFile.getName().toLowerCase(Locale.ENGLISH).endsWith(".gz")) {
                    tmpFile = new File(tmpDir, getFilenameWithoutSuffix(inputFile.getName()));
                    try (InputStream is = new GZIPInputStream(ZipFileUtils.newFileInputStream(inputFile))) {
                        TFile.cp(is, tmpFile);
                    }
                } else {
                    tmpFile = new File(tmpDir, inputFile.getName());
                    TFile.cp_rp(inputFile, tmpFile, TArchiveDetector.ALL);
                }
                return tmpFile;
            }
        }
        return inputFile;
    }

    public static void ioWrap(BooleanSupplier action) {
        if (!action.getAsBoolean()) {
            throw new RuntimeException("IO operation failed");
        }
    }

    @FunctionalInterface
    public interface BooleanSupplierThatThrows {
        boolean accept() throws IOException;
    }

    public static void ioWrap2(BooleanSupplierThatThrows action) {
        try {
            boolean created = action.accept();
            if (!created) {
                throw new RuntimeException("IO operation failed");
            }
        } catch (IOException e) {
            throw new RuntimeException("IO operation failed");
        }
    }

    @FunctionalInterface
    public interface IntSupplierThatThrows {
        int accept() throws IOException;
    }

    public static void ioWrap(IntSupplierThatThrows action) throws IOException {
        if (action.accept() == -1) {
            throw new RuntimeException("IO operation failed");
        }
    }
}