VaultFileCopy.java
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* 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;
}
}