JarExporter.java 4.35 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.RepositoryException
 *  org.apache.commons.io.IOUtils
 *  org.apache.commons.io.output.CloseShieldOutputStream
 */
package com.day.jcr.vault.fs.io;

import com.day.jcr.vault.fs.api.AccessType;
import com.day.jcr.vault.fs.api.Artifact;
import com.day.jcr.vault.fs.api.VaultFile;
import com.day.jcr.vault.fs.io.AbstractExporter;
import com.day.jcr.vault.fs.io.ExportInfo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.jcr.RepositoryException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.output.CloseShieldOutputStream;

public class JarExporter
extends AbstractExporter {
    private JarOutputStream jOut;
    private OutputStream out;
    private File jarFile;

    public JarExporter(File jarFile) {
        this.jarFile = jarFile;
    }

    public JarExporter(OutputStream out) {
        this.out = out;
    }

    public void open() throws IOException {
        if (this.jOut == null) {
            if (this.jarFile != null) {
                this.jOut = new JarOutputStream(new FileOutputStream(this.jarFile));
            } else if (this.out != null) {
                this.jOut = new JarOutputStream(this.out);
            } else {
                throw new IllegalArgumentException("Either out or jarFile needs to be set.");
            }
        }
    }

    public void close() throws IOException {
        if (this.jOut != null) {
            this.jOut.close();
            this.jOut = null;
        }
    }

    public void createDirectory(VaultFile file, String relPath) throws RepositoryException, IOException {
        ZipEntry e = new ZipEntry(this.getPlatformFilePath(file, relPath) + "/");
        this.jOut.putNextEntry(e);
        this.jOut.closeEntry();
        this.track("A", relPath);
        this.exportInfo.update(ExportInfo.Type.MKDIR, e.getName());
    }

    public void createDirectory(String relPath) throws IOException {
        ZipEntry e = new ZipEntry(relPath + "/");
        this.jOut.putNextEntry(e);
        this.jOut.closeEntry();
        this.exportInfo.update(ExportInfo.Type.MKDIR, e.getName());
    }

    public void writeFile(VaultFile file, String relPath) throws RepositoryException, IOException {
        ZipEntry e = new ZipEntry(this.getPlatformFilePath(file, relPath));
        Artifact a = file.getArtifact();
        if (a.getLastModified() > 0) {
            e.setTime(a.getLastModified());
        }
        this.track("A", relPath);
        this.exportInfo.update(ExportInfo.Type.ADD, e.getName());
        this.jOut.putNextEntry(e);
        switch (a.getPreferredAccess()) {
            case NONE: {
                throw new RepositoryException("Artifact has no content.");
            }
            case SPOOL: {
                CloseShieldOutputStream nout = new CloseShieldOutputStream((OutputStream)this.jOut);
                a.spool((OutputStream)nout);
                break;
            }
            case STREAM: {
                CloseShieldOutputStream nout = new CloseShieldOutputStream((OutputStream)this.jOut);
                InputStream in = a.getInputStream();
                IOUtils.copy((InputStream)in, (OutputStream)nout);
                in.close();
            }
        }
        this.jOut.closeEntry();
    }

    public void writeFile(InputStream in, String relPath) throws IOException {
        ZipEntry e = new ZipEntry(relPath);
        this.exportInfo.update(ExportInfo.Type.ADD, e.getName());
        this.jOut.putNextEntry(e);
        CloseShieldOutputStream nout = new CloseShieldOutputStream((OutputStream)this.jOut);
        IOUtils.copy((InputStream)in, (OutputStream)nout);
        in.close();
        this.jOut.closeEntry();
    }

    public void write(ZipFile zip, ZipEntry entry) throws IOException {
        this.track("A", entry.getName());
        this.exportInfo.update(ExportInfo.Type.ADD, entry.getName());
        ZipEntry copy = new ZipEntry(entry);
        this.jOut.putNextEntry(copy);
        if (!entry.isDirectory()) {
            InputStream in = zip.getInputStream(entry);
            IOUtils.copy((InputStream)in, (OutputStream)this.jOut);
            in.close();
        }
        this.jOut.closeEntry();
    }

}