Example usage for org.apache.commons.compress.tar TarInputStream getNextEntry

List of usage examples for org.apache.commons.compress.tar TarInputStream getNextEntry

Introduction

In this page you can find the example usage for org.apache.commons.compress.tar TarInputStream getNextEntry.

Prototype

public TarEntry getNextEntry() throws IOException 

Source Link

Document

Get the next entry in this tar archive.

Usage

From source file:org.bootchart.Main.java

/**
 * Parses the bootchart log tarball from the log tarball or directory and
 * renders the chart to the output stream.
 * /*from ww  w .  ja  v a2s .  co  m*/
 * @param logFile        log tarball or directory
 * @param format         image format (png, svg or eps)
 * @param prune          whether to prune the tree
 * @param fileName       file name prefix
 * @return               rendered image file path
 * @throws IOException               if an I/O error is thrown
 */
public static String render(File logFile, String format, boolean prune, String fileName) throws IOException {

    if (!logFile.exists()) {
        return null;
    }
    TarInputStream tis = null;
    File[] files = null;
    boolean isLogTarball; // whether it's a tarball or dir
    if (logFile.isFile()) {
        isLogTarball = true;
        InputStream is = new FileInputStream(logFile);
        if (logFile.getName().endsWith("gz")) {
            is = new GZIPInputStream(is);
        }
        tis = new TarInputStream(is);
    } else {
        isLogTarball = false;
        files = logFile.listFiles();
        Arrays.sort(files);
    }

    Properties headers = new Properties();
    Map pidNameMap = null;
    Map forkMap = null;
    Stats cpuStats = null;
    Stats diskStats = null;
    PsStats psStats = null;
    BootStats bootStats = null;
    int numCpu = 1;

    String monitoredApp = null; // used for profiling specific processes

    boolean hasMoreFiles = true;
    int fileI = 0;
    while (hasMoreFiles) {
        String logName = null;
        InputStream is = null;
        TarEntry tarEntry = null;
        if (isLogTarball) {
            tarEntry = tis.getNextEntry();
            if (tarEntry == null) {
                hasMoreFiles = false;
                break;
            }
            logName = tarEntry.getName();
            is = tis;
        } else {
            logName = files[fileI].getName();
            is = new FileInputStream(files[fileI]);
            fileI++;
            if (fileI >= files.length) {
                hasMoreFiles = false;
            }
        }
        log.fine("Parsing log file: " + logName);

        if (logName.equals("header")) {
            // read the headers
            headers = HeaderParser.parseLog(is);
            if (headers != null) {
                monitoredApp = headers.getProperty("profile.process");
            }
            numCpu = HeaderParser.getNumCPUs(headers);

        } else if (logName.equals("ps.log")) {
            // read ps log file
            psStats = PsParser.parseLog(is, pidNameMap, forkMap);

        } else if (logName.equals("proc_ps.log")) {
            // read the /proc/[PID]/stat log file
            psStats = ProcPsParser.parseLog(is, pidNameMap, forkMap);

        } else if (logName.equals("proc_stat.log")) {
            // read the /proc/stat log file
            cpuStats = ProcStatParser.parseLog(is);

        } else if (logName.equals("proc_diskstats.log")) {
            // read the /proc/diskstats log file
            diskStats = ProcDiskStatParser.parseLog(is, numCpu);

        } else if (logName.equals("kernel_pacct")) {
            // parse process forking PID mappings
            forkMap = PacctParser.parseLog(is);

        } else if (logName.equals("init_pidname.log")) {
            // map pids to command names (useful for Gentoo, where most init
            // processes are sourced and thus shown as "rc boot" or
            // "rc default")
            pidNameMap = PidNameParser.parseLog(is);

        } else {
            log.warning("Unknown log file: " + logName);
            if (logName.length() > 32) {
                // We're getting garbage from TarInputStream.  Break out since
                // a malformed tarball can cause spinning.
                hasMoreFiles = false;
                break;
            }
        }
    }

    long opTime = 0;
    if (bootStats == null) {
        ProcessTree procTree = new ProcessTree(null, monitoredApp, prune);
        if (psStats == null || psStats.processList.size() == 0) {
            log.warning("No process samples");
        } else {
            opTime = System.currentTimeMillis();
            procTree = new ProcessTree(psStats, monitoredApp, prune);
            opTime = System.currentTimeMillis() - opTime;
            log.fine("Tree generation and pruning took " + opTime + " ms");

            if (procTree.processTree.size() == 0 || procTree.duration == 0) {
                log.warning("No processes found");
            }
        }
        //log.fine(procTree.toString());
        bootStats = new BootStats(cpuStats, diskStats, procTree);
    }

    Renderer renderer = null;
    opTime = System.currentTimeMillis();
    if ("png".equals(format)) {
        renderer = new PNGRenderer();
    } else if ("svg".equals(format)) {
        renderer = new SVGRenderer();
    } else if ("eps".equals(format)) {
        renderer = new EPSRenderer();
    } else {
        throw new IllegalArgumentException("Invalid format: " + format);
    }
    fileName += "." + renderer.getFileSuffix();
    FileOutputStream fos = new FileOutputStream(fileName);
    renderer.render(headers, bootStats, fos);
    fos.close();
    log.fine("Wrote image: " + new File(fileName).getAbsolutePath());
    System.out.println("Wrote image: " + new File(fileName).getAbsolutePath());
    opTime = System.currentTimeMillis() - opTime;
    log.fine("Render time: " + opTime + " ms)");
    return fileName;
}

From source file:org.dcm4chex.archive.hsm.TarRetrieverService.java

private TarEntry skipDirectoryEntries(TarInputStream tar) throws IOException {
    for (TarEntry entry = tar.getNextEntry(); entry != null; entry = tar.getNextEntry()) {
        if (!entry.isDirectory())
            return entry;
    }// ww  w .ja v  a2  s .co m
    return null;
}

From source file:org.dcm4chex.archive.hsm.VerifyTar.java

public static Map<String, byte[]> verify(InputStream in, String tarname, byte[] buf,
        ArrayList<String> objectNames) throws IOException, VerifyTarException {
    TarInputStream tar = new TarInputStream(in);
    try {//  w w  w . j  a v  a 2s .c  om
        log.debug("Verify tar file: {}", tarname);
        TarEntry entry = tar.getNextEntry();
        if (entry == null)
            throw new VerifyTarException("No entries in " + tarname);
        String entryName = entry.getName();
        if (!"MD5SUM".equals(entryName))
            throw new VerifyTarException("Missing MD5SUM entry in " + tarname);
        BufferedReader dis = new BufferedReader(new InputStreamReader(tar));

        HashMap<String, byte[]> md5sums = new HashMap<String, byte[]>();
        String line;
        while ((line = dis.readLine()) != null) {
            char[] c = line.toCharArray();
            byte[] md5sum = new byte[16];
            for (int i = 0, j = 0; i < md5sum.length; i++, j++, j++) {
                md5sum[i] = (byte) ((fromHexDigit(c[j]) << 4) | fromHexDigit(c[j + 1]));
            }
            md5sums.put(line.substring(34), md5sum);
        }
        Map<String, byte[]> entries = new HashMap<String, byte[]>(md5sums.size());
        entries.putAll(md5sums);
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        while ((entry = tar.getNextEntry()) != null) {
            entryName = entry.getName();
            log.debug("START: Check MD5 of entry: {}", entryName);
            if (objectNames != null && !objectNames.remove(entryName))
                throw new VerifyTarException(
                        "TAR " + tarname + " contains entry: " + entryName + " not in file list");
            byte[] md5sum = (byte[]) md5sums.remove(entryName);
            if (md5sum == null)
                throw new VerifyTarException("Unexpected TAR entry: " + entryName + " in " + tarname);
            digest.reset();
            in = new DigestInputStream(tar, digest);
            while (in.read(buf) > 0)
                ;
            if (!Arrays.equals(digest.digest(), md5sum)) {
                throw new VerifyTarException("Failed MD5 check of TAR entry: " + entryName + " in " + tarname);
            }
            log.debug("DONE: Check MD5 of entry: {}", entryName);
        }
        if (!md5sums.isEmpty())
            throw new VerifyTarException("Missing TAR entries: " + md5sums.keySet() + " in " + tarname);
        if (objectNames != null && !objectNames.isEmpty())
            throw new VerifyTarException(
                    "Missing TAR entries from object list: " + objectNames.toString() + " in " + tarname);
        return entries;
    } finally {
        tar.close();
    }
}