VaultFileCopy.java 3.5 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.RepositoryException
 */
package com.day.jcr.vault.fs;

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.util.BinaryCheckOutputStream;
import com.day.jcr.vault.util.LineOutputStream;
import com.day.jcr.vault.util.MD5;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.jcr.RepositoryException;

public class VaultFileCopy {
    private final VaultFile remoteFile;
    private final File localFile;
    private final MessageDigest digest;
    private byte[] lineFeed = null;
    private MD5 md5 = null;
    private long length;
    private boolean binary;

    private VaultFileCopy(VaultFile remote, File local, MessageDigest digest, byte[] lineFeed) {
        this.remoteFile = remote;
        this.localFile = local;
        this.digest = digest;
        this.lineFeed = lineFeed;
    }

    public static VaultFileCopy copy(VaultFile remote, File local) throws IOException {
        return VaultFileCopy.copy(remote, local, null);
    }

    public static VaultFileCopy copy(VaultFile remote, File local, byte[] lineFeed) throws IOException {
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("md5");
        }
        catch (NoSuchAlgorithmException e) {
            throw new IllegalArgumentException(e.toString());
        }
        VaultFileCopy copy = new VaultFileCopy(remote, local, md, lineFeed);
        try {
            copy.run();
        }
        catch (RepositoryException e) {
            throw new IOException(e.toString());
        }
        return copy;
    }

    private void run() throws IOException, RepositoryException {
        Artifact a = this.remoteFile.getArtifact();
        if (a.getPreferredAccess() == AccessType.NONE) {
            throw new IOException("Artifact has no content.");
        }
        OutputStream base = this.digest == null ? new FileOutputStream(this.localFile) : new DigestOutputStream(new FileOutputStream(this.localFile), this.digest);
        if (this.lineFeed != null) {
            base = new LineOutputStream(base, this.lineFeed);
        }
        BinaryCheckOutputStream out = new BinaryCheckOutputStream(base);
        switch (a.getPreferredAccess()) {
            case SPOOL: {
                a.spool(out);
                out.close();
                break;
            }
            case STREAM: {
                int read;
                InputStream in = a.getInputStream();
                byte[] buffer = new byte[8192];
                while ((read = in.read(buffer)) >= 0) {
                    out.write(buffer, 0, read);
                }
                in.close();
                out.close();
            }
        }
        this.binary = out.isBinary();
        this.length = this.localFile.length();
        long lastMod = this.remoteFile.lastModified();
        if (lastMod > 0) {
            this.localFile.setLastModified(lastMod);
        }
        if (this.digest != null) {
            this.md5 = new MD5(this.digest.digest());
        }
    }

    public MD5 getMd5() {
        return this.md5;
    }

    public long getLength() {
        return this.length;
    }

    public boolean isBinary() {
        return this.binary;
    }

}