001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
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 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.connect.update.util;
020
021import java.io.File;
022import java.io.FileInputStream;
023import java.io.IOException;
024import java.security.MessageDigest;
025import java.security.NoSuchAlgorithmException;
026
027import org.nuxeo.common.utils.FileUtils;
028import org.nuxeo.connect.update.LocalPackage;
029
030/**
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 */
033public class IOUtils {
034
035    private IOUtils() {
036    }
037
038    /**
039     * Backup the given file to the package backup directory. The backup file will be returned. The backup file will be
040     * named: MD5ofFilepath_filename.
041     *
042     * @return the name of the backup file.
043     */
044    public static File backup(LocalPackage pkg, File file) throws IOException {
045        file = file.getCanonicalFile();
046        String md5 = createMd5(file.getAbsolutePath());
047        File bak = pkg.getData().getEntry(LocalPackage.BACKUP_DIR);
048        bak.mkdirs();
049        String name = file.getName();
050        File bakFile = new File(bak, md5 + "_" + name);
051        FileUtils.copy(file, bakFile);
052        return bakFile;
053    }
054
055    public static String createMd5(String text) throws IOException {
056        MessageDigest digest = getMD5Digest();
057        digest.update(text.getBytes());
058        byte[] hash = digest.digest();
059        return md5ToHex(hash);
060    }
061
062    public static String createMd5(File file) throws IOException {
063        MessageDigest digest = getMD5Digest();
064        FileInputStream in = new FileInputStream(file);
065        try {
066            byte[] bytes = new byte[64 * 1024];
067            int r = in.read(bytes);
068            while (r > -1) {
069                if (r > 0) {
070                    digest.update(bytes, 0, r);
071                }
072                r = in.read(bytes);
073            }
074            byte[] hash = digest.digest();
075            return md5ToHex(hash);
076        } finally {
077            in.close();
078        }
079    }
080
081    protected static MessageDigest getMD5Digest() throws IOException {
082        try {
083            return MessageDigest.getInstance("MD5");
084        } catch (NoSuchAlgorithmException e) {
085            throw new IOException(e);
086        }
087    }
088
089    public static String md5ToHex(byte[] hash) {
090        StringBuilder hexString = new StringBuilder();
091        for (byte b : hash) {
092            String hex = Integer.toHexString(0xFF & b);
093            if (hex.length() == 1) {
094                hexString.append('0');
095            }
096            hexString.append(hex);
097        }
098        return hexString.toString();
099    }
100
101}