com.pclinuxos.rpm.util.FileUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.pclinuxos.rpm.util.FileUtils.java

Source

/**
 *  RCEB
 *  Program for creating/editing .spec files for RPMS and it provides also a buildbot for creating RPMS.
 *  Copyright (C) 2013  Hans-Martin Haase
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.pclinuxos.rpm.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;

import org.apache.commons.compress.archivers.cpio.CpioArchiveEntry;
import org.apache.commons.compress.archivers.cpio.CpioArchiveInputStream;

import com.pclinuxos.rpm.constants.FileConstants;

/**
 * The class provide static file functions for non root users.
 * 
 * @author Hans-Martin Haase a.k.a ghostbunny <hmhaase at pclinuxosusers dot de>
 * @version 0.01
 */
public class FileUtils {

    /**
     * The method generated a string which contains all differences between two files, here for 
     * two lists with package names.
     * 
     * @param before original package count
     * @param after the content of this file will be search in "before"
     * @return if errors occured null else a string with differences which are not in "before".
     */
    public static String compareRpmLists(File before, File after) {

        String out = null;

        try {

            BufferedReader l1Reader = new BufferedReader(new FileReader(before));
            BufferedReader l2Reader = new BufferedReader(new FileReader(after));

            HashSet<String> bef = new HashSet<String>();
            HashSet<String> aft = new HashSet<String>();

            String read = l1Reader.readLine();

            while (read != null) {

                bef.add(read);
                read = l1Reader.readLine();
            }

            l1Reader.close();
            read = l2Reader.readLine();

            while (read != null) {

                aft.add(read);
                read = l2Reader.readLine();
            }

            l2Reader.close();

            for (String pkg : aft) {

                if (bef.contains(pkg)) {

                    out = out + " " + pkg;
                }
            }
        } catch (FileNotFoundException e) {

            out = null;
        } catch (IOException e) {

            out = null;
        }

        return out;
    }

    /**
     * The method extracts a srpm into the default directory used by the program (/home/<user>/RCEB/srpms/tmp)
     * 
     * @param srpm the srpm to extract
     * @return 0 if the extraction was successfully, -1 if a IOException occurred, -2 if a InterruptException
     *  occurred. Values > 0 for return codes of the rpm2cpio command.
     */
    public static int extractSrpm(String srpm) {

        int returnCode = 0;

        try {

            Process extractProcess = Runtime.getRuntime()
                    .exec("rpm2cpio " + FileConstants.SRCSRPMS.getAbsolutePath() + "/" + srpm);
            // 64kb buffer
            byte[] buffer = new byte[0xFFFF];
            InputStream inread = extractProcess.getInputStream();

            FileOutputStream out = new FileOutputStream(new File(FileConstants.F4SRPMEX + "archive.cpio"));

            while (inread.read(buffer) != -1) {

                out.write(buffer);
            }

            returnCode = extractProcess.waitFor();

            if (returnCode == 0) {

                CpioArchiveInputStream cpioIn = new CpioArchiveInputStream(
                        new FileInputStream(FileConstants.F4SRPMEX + "archive.cpio"));
                CpioArchiveEntry cpEntry;

                while ((cpEntry = cpioIn.getNextCPIOEntry()) != null) {

                    FileOutputStream fOut = new FileOutputStream(FileConstants.F4SRPMEX + cpEntry.getName());
                    // Do not make this buffer bigger it breaks the cpio decompression
                    byte[] buffer2 = new byte[1];
                    ArrayList<Byte> buf = new ArrayList<Byte>();

                    while (cpioIn.read(buffer2) != -1) {

                        buf.add(buffer2[0]);
                    }

                    byte[] file = new byte[buf.size()];

                    for (int i = 0; i < buf.size(); i++) {

                        file[i] = buf.get(i);
                    }

                    fOut.write(file);

                    fOut.flush();
                    fOut.close();
                }

                cpioIn.close();
            }
        } catch (IOException e) {

            returnCode = -1;
        } catch (InterruptedException e) {

            returnCode = -2;
        }

        new File(FileConstants.F4SRPMEX + "archive.cpio").delete();

        return returnCode;
    }

    /**
     * The method copies a file to an other location. 
     * 
     * @param source file/folder to copy
     * @param dest destination file/folder
     * @return 0 if copying was successfully, 1 if the source file/folder does not exist, 2 if a IOException
     * occurred while copying.
     */
    public static byte cp(File source, File dest) {

        byte returnCode = 0;

        try {

            FileInputStream sourceIn = new FileInputStream(source);
            FileOutputStream destOut = new FileOutputStream(dest);

            if (!source.exists()) {

                returnCode = 1;
            }

            // 64kb buffer
            byte[] buffer = new byte[0xFFFF];

            while (sourceIn.read(buffer) != -1) {

                destOut.write(buffer);
            }

            sourceIn.close();
            destOut.flush();
            destOut.close();

        } catch (FileNotFoundException e) {

            returnCode = 1;

        } catch (IOException e) {

            returnCode = 2;
        }

        return returnCode;
    }
}