use of java.security.DigestInputStream in project bndtools by bndtools.
the class ReposTemplateLoaderTest method testAlternateDelimiters.
public void testAlternateDelimiters() throws Exception {
List<Template> templates = loader.findTemplates("test2", new ProgressMonitorReporter(new NullProgressMonitor(), "")).getValue();
assertEquals(1, templates.size());
Template template = templates.get(0);
Map<String, List<Object>> parameters = new HashMap<>();
parameters.put("projectName", Collections.<Object>singletonList("org.example.foo"));
parameters.put("srcDir", Collections.<Object>singletonList("src/main/java"));
parameters.put("basePackageDir", Collections.<Object>singletonList("org/example/foo"));
ResourceMap outputs = template.generateOutputs(parameters);
assertEquals(5, outputs.size());
Iterator<Entry<String, Resource>> iter = outputs.entries().iterator();
Entry<String, Resource> entry;
entry = iter.next();
assertEquals("src/main/java/org/example/foo/Activator.java", entry.getKey());
assertEquals("package org.example.foo; public class Activator {}", IO.collect(entry.getValue().getContent()));
entry = iter.next();
assertEquals("pic.jpg", entry.getKey());
// Check the digest of the pic to ensure it didn't get damaged by the templating engine
DigestInputStream digestStream = new DigestInputStream(entry.getValue().getContent(), MessageDigest.getInstance("SHA-256"));
IO.drain(digestStream);
byte[] digest = digestStream.getMessageDigest().digest();
assertEquals("ea5d770bc2deddb1f9a20df3ad337bdc1490ba7b35fa41c33aa4e9a534e82ada", Hex.toHexString(digest).toLowerCase());
entry = iter.next();
assertEquals("src/main/java/", entry.getKey());
entry = iter.next();
assertEquals("src/main/java/org/example/foo/", entry.getKey());
entry = iter.next();
assertEquals("bnd.bnd", entry.getKey());
assertEquals("Bundle-SymbolicName: org.example.foo", IO.collect(entry.getValue().getContent()));
}
use of java.security.DigestInputStream in project bnd by bndtools.
the class AetherRepository method put.
@Override
public PutResult put(InputStream stream, PutOptions options) throws Exception {
init();
DigestInputStream digestStream = new DigestInputStream(stream, MessageDigest.getInstance("SHA-1"));
final File tmpFile = IO.createTempFile(cacheDir, "put", ".bnd");
try {
IO.copy(digestStream, tmpFile);
byte[] digest = digestStream.getMessageDigest().digest();
if (options.digest != null && !Arrays.equals(options.digest, digest))
throw new IOException("Retrieved artifact digest doesn't match specified digest");
// Get basic info about the bundle we're deploying
Jar jar = new Jar(tmpFile);
Artifact artifact = ConversionUtils.fromBundleJar(jar);
artifact = artifact.setFile(tmpFile);
// Setup the Aether repo session and create the deployment request
DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
session.setLocalRepositoryManager(repoSystem.newLocalRepositoryManager(session, localRepo));
final DeployRequest request = new DeployRequest();
request.addArtifact(artifact);
request.setRepository(remoteRepo);
// Capture the result including remote resource URI
final ResultHolder resultHolder = new ResultHolder();
session.setTransferListener(new AbstractTransferListener() {
@Override
public void transferSucceeded(TransferEvent event) {
TransferResource resource = event.getResource();
if (event.getRequestType() == RequestType.PUT && tmpFile.equals(resource.getFile())) {
PutResult result = new PutResult();
result.artifact = URI.create(resource.getRepositoryUrl() + resource.getResourceName());
resultHolder.result = result;
System.out.println("UPLOADED to: " + URI.create(resource.getRepositoryUrl() + resource.getResourceName()));
}
}
@Override
public void transferFailed(TransferEvent event) {
if (event.getRequestType() == RequestType.PUT && tmpFile.equals(event.getResource().getFile()))
resultHolder.error = event.getException();
}
@Override
public void transferCorrupted(TransferEvent event) throws TransferCancelledException {
if (event.getRequestType() == RequestType.PUT && tmpFile.equals(event.getResource().getFile()))
resultHolder.error = event.getException();
}
});
// Do the deploy and report results
repoSystem.deploy(session, request);
if (resultHolder.result != null) {
if (indexedRepo != null)
indexedRepo.reset();
return resultHolder.result;
} else if (resultHolder.error != null) {
throw new Exception("Error during artifact upload", resultHolder.error);
} else {
throw new Exception("Artifact was not uploaded");
}
} finally {
if (tmpFile != null && tmpFile.isFile())
IO.delete(tmpFile);
}
}
use of java.security.DigestInputStream in project ceylon-compiler by ceylon.
the class JavapTask method read.
public ClassFileInfo read(JavaFileObject fo) throws IOException, ConstantPoolException {
InputStream in = fo.openInputStream();
try {
SizeInputStream sizeIn = null;
MessageDigest md = null;
if (options.sysInfo || options.verbose) {
try {
md = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException ignore) {
}
in = new DigestInputStream(in, md);
in = sizeIn = new SizeInputStream(in);
}
ClassFile cf = ClassFile.read(in, attributeFactory);
byte[] digest = (md == null) ? null : md.digest();
int size = (sizeIn == null) ? -1 : sizeIn.size();
return new ClassFileInfo(fo, cf, digest, size);
} finally {
in.close();
}
}
use of java.security.DigestInputStream in project Essentials by drtshock.
the class ManagedFile method checkForVersion.
public static boolean checkForVersion(final File file, final String version) throws IOException {
if (file.length() < 33) {
return false;
}
final BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
try {
final byte[] buffer = new byte[(int) file.length()];
int position = 0;
do {
final int length = bis.read(buffer, position, Math.min((int) file.length() - position, BUFFERSIZE));
if (length < 0) {
break;
}
position += length;
} while (position < file.length());
final ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
if (bais.skip(file.length() - 33) != file.length() - 33) {
return false;
}
final BufferedReader reader = new BufferedReader(new InputStreamReader(bais));
try {
String hash = reader.readLine();
if (hash != null && hash.matches("#[a-f0-9]{32}")) {
hash = hash.substring(1);
bais.reset();
final String versionline = reader.readLine();
if (versionline != null && versionline.matches("#version: .+")) {
final String versioncheck = versionline.substring(10);
if (!versioncheck.equalsIgnoreCase(version)) {
bais.reset();
final MessageDigest digest = getDigest();
final DigestInputStream digestStream = new DigestInputStream(bais, digest);
try {
final byte[] bytes = new byte[(int) file.length() - 33];
digestStream.read(bytes);
final BigInteger correct = new BigInteger(hash, 16);
final BigInteger test = new BigInteger(1, digest.digest());
if (correct.equals(test)) {
return true;
} else {
Bukkit.getLogger().warning("File " + file.toString() + " has been modified by user and file version differs, please update the file manually.");
}
} finally {
digestStream.close();
}
}
}
}
} finally {
reader.close();
}
} finally {
bis.close();
}
return false;
}
use of java.security.DigestInputStream in project Essentials by drtshock.
the class EssentialsUpgrade method deleteOldItemsCsv.
private void deleteOldItemsCsv() {
if (doneFile.getBoolean("deleteOldItemsCsv", false)) {
return;
}
final File file = new File(ess.getDataFolder(), "items.csv");
if (file.exists()) {
try {
final Set<BigInteger> oldconfigs = new HashSet<>();
// sep 1
oldconfigs.add(new BigInteger("66ec40b09ac167079f558d1099e39f10", 16));
// crlf
oldconfigs.add(new BigInteger("34284de1ead43b0bee2aae85e75c041d", 16));
// jul 24
oldconfigs.add(new BigInteger("c33bc9b8ee003861611bbc2f48eb6f4f", 16));
// crlf
oldconfigs.add(new BigInteger("6ff17925430735129fc2a02f830c1daa", 16));
MessageDigest digest = ManagedFile.getDigest();
final BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
final byte[] buffer = new byte[1024];
try (DigestInputStream dis = new DigestInputStream(bis, digest)) {
while (dis.read(buffer) != -1) {
}
}
BigInteger hash = new BigInteger(1, digest.digest());
if (oldconfigs.contains(hash) && !file.delete()) {
throw new IOException("Could not delete file " + file.toString());
}
doneFile.setProperty("deleteOldItemsCsv", true);
doneFile.save();
} catch (IOException ex) {
Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex);
}
}
}
Aggregations