001/*
002 * Copyright 2012-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.loader.tools;
018
019import java.io.File;
020import java.io.FileInputStream;
021import java.io.IOException;
022import java.security.DigestInputStream;
023import java.security.MessageDigest;
024import java.security.NoSuchAlgorithmException;
025
026/**
027 * Utilities for manipulating files and directories in Spring Boot tooling.
028 *
029 * @author Dave Syer
030 * @author Phillip Webb
031 */
032public abstract class FileUtils {
033
034        /**
035         * Utility to remove duplicate files from an "output" directory if they already exist
036         * in an "origin". Recursively scans the origin directory looking for files (not
037         * directories) that exist in both places and deleting the copy.
038         * @param outputDirectory the output directory
039         * @param originDirectory the origin directory
040         */
041        public static void removeDuplicatesFromOutputDirectory(File outputDirectory,
042                        File originDirectory) {
043                if (originDirectory.isDirectory()) {
044                        for (String name : originDirectory.list()) {
045                                File targetFile = new File(outputDirectory, name);
046                                if (targetFile.exists() && targetFile.canWrite()) {
047                                        if (!targetFile.isDirectory()) {
048                                                targetFile.delete();
049                                        }
050                                        else {
051                                                FileUtils.removeDuplicatesFromOutputDirectory(targetFile,
052                                                                new File(originDirectory, name));
053                                        }
054                                }
055                        }
056                }
057        }
058
059        /**
060         * Generate a SHA.1 Hash for a given file.
061         * @param file the file to hash
062         * @return the hash value as a String
063         * @throws IOException if the file cannot be read
064         */
065        public static String sha1Hash(File file) throws IOException {
066                try {
067                        DigestInputStream inputStream = new DigestInputStream(
068                                        new FileInputStream(file), MessageDigest.getInstance("SHA-1"));
069                        try {
070                                byte[] buffer = new byte[4098];
071                                while (inputStream.read(buffer) != -1) {
072                                        // Read the entire stream
073                                }
074                                return bytesToHex(inputStream.getMessageDigest().digest());
075                        }
076                        finally {
077                                inputStream.close();
078                        }
079                }
080                catch (NoSuchAlgorithmException ex) {
081                        throw new IllegalStateException(ex);
082                }
083        }
084
085        private static String bytesToHex(byte[] bytes) {
086                StringBuilder hex = new StringBuilder();
087                for (byte b : bytes) {
088                        hex.append(String.format("%02x", b));
089                }
090                return hex.toString();
091        }
092
093}