aQute.lib.jardiff.Diff Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bndlib Show documentation
Show all versions of bndlib Show documentation
A Swiss Army Knife for OSGi
package aQute.lib.jardiff;
import java.io.*;
import java.util.*;
import java.util.jar.*;
import aQute.lib.osgi.*;
public class Diff {
/**
* Compare two JAR files with each other.
*
* @param a
* @param b
* @param strict
* @return
* @throws IOException
*/
public Map diff(Jar a, Jar b, boolean strict)
throws Exception {
Map different = new TreeMap();
compareManifest(different, a.getManifest(), b.getManifest(), strict);
diff(different, a.getResources().keySet(), b.getResources().keySet());
Set shared = new HashSet(a.getResources().keySet());
shared.retainAll(b.getResources().keySet());
for (String path : a.getResources().keySet()) {
Resource ra = a.getResource(path);
Resource rb = a.getResource(path);
if (rb != null) {
if (ra.getClass() != rb.getClass()) {
different.put(path, "Different types: "
+ a.getClass().getName() + " : "
+ b.getClass().getName());
} else {
if (path.endsWith(".jar")) {
Jar aa = new Jar(path, ra.openInputStream());
Jar bb = new Jar(path, rb.openInputStream());
try {
Map result = diff(aa, bb, strict);
if (!result.isEmpty())
different.put(path, result);
} finally {
aa.close();
bb.close();
}
} else {
String cmp = diff(ra.openInputStream(), rb
.openInputStream());
if (cmp != null)
different.put(path, cmp);
}
}
}
}
return different;
}
String diff(InputStream a, InputStream b) throws IOException {
int n = 0;
int binary = 0;
StringBuffer sb = new StringBuffer();
while (true) {
int ac = a.read();
int bc = b.read();
if (ac < 0) {
if (bc < 0)
return null;
return "a is smaller";
} else if (bc < 0) {
return "b is smaller";
}
if (ac != bc) {
String s = "Difference at pos: " + n;
if (binary == 0 && sb.length() > 5) {
s = s + "Context: " + sb.substring(sb.length() - 5);
}
}
if (ac >= ' ' && ac <= '~')
sb.append((char) ac);
else
binary++;
n++;
}
}
void diff(Map different, Set> a, Set> b) {
Set