001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 */
017package org.nuxeo.connect.update.util;
018
019import java.io.File;
020import java.io.FileInputStream;
021import java.io.IOException;
022import java.security.MessageDigest;
023import java.security.NoSuchAlgorithmException;
024
025import org.nuxeo.common.utils.FileUtils;
026import org.nuxeo.connect.update.LocalPackage;
027
028/**
029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
030 */
031public class IOUtils {
032
033    private IOUtils() {
034    }
035
036    /**
037     * Backup the given file to the package backup directory. The backup file will be returned. The backup file will be
038     * named: MD5ofFilepath_filename.
039     *
040     * @return the name of the backup file.
041     */
042    public static File backup(LocalPackage pkg, File file) throws IOException {
043        file = file.getCanonicalFile();
044        String md5 = createMd5(file.getAbsolutePath());
045        File bak = pkg.getData().getEntry(LocalPackage.BACKUP_DIR);
046        bak.mkdirs();
047        String name = file.getName();
048        File bakFile = new File(bak, md5 + "_" + name);
049        FileUtils.copy(file, bakFile);
050        return bakFile;
051    }
052
053    public static String createMd5(String text) throws IOException {
054        MessageDigest digest = getMD5Digest();
055        digest.update(text.getBytes());
056        byte[] hash = digest.digest();
057        return md5ToHex(hash);
058    }
059
060    public static String createMd5(File file) throws IOException {
061        MessageDigest digest = getMD5Digest();
062        FileInputStream in = new FileInputStream(file);
063        try {
064            byte[] bytes = new byte[64 * 1024];
065            int r = in.read(bytes);
066            while (r > -1) {
067                if (r > 0) {
068                    digest.update(bytes, 0, r);
069                }
070                r = in.read(bytes);
071            }
072            byte[] hash = digest.digest();
073            return md5ToHex(hash);
074        } finally {
075            in.close();
076        }
077    }
078
079    protected static MessageDigest getMD5Digest() throws IOException {
080        try {
081            return MessageDigest.getInstance("MD5");
082        } catch (NoSuchAlgorithmException e) {
083            throw new IOException(e);
084        }
085    }
086
087    public static String md5ToHex(byte[] hash) {
088        StringBuilder hexString = new StringBuilder();
089        for (byte b : hash) {
090            String hex = Integer.toHexString(0xFF & b);
091            if (hex.length() == 1) {
092                hexString.append('0');
093            }
094            hexString.append(hex);
095        }
096        return hexString.toString();
097    }
098
099}