use of java.util.jar.JarEntry in project spring-boot by spring-projects.
the class PropertiesMergingResourceTransformer method modifyOutputStream.
@Override
public void modifyOutputStream(JarOutputStream os) throws IOException {
os.putNextEntry(new JarEntry(this.resource));
this.data.store(os, "Merged by PropertiesMergingResourceTransformer");
os.flush();
this.data.clear();
}
use of java.util.jar.JarEntry in project spring-boot by spring-projects.
the class JarFileTests method verifySignedJar.
@Test
public void verifySignedJar() throws Exception {
String classpath = System.getProperty("java.class.path");
String[] entries = classpath.split(System.getProperty("path.separator"));
String signedJarFile = null;
for (String entry : entries) {
if (entry.contains("bcprov")) {
signedJarFile = entry;
}
}
assertThat(signedJarFile).isNotNull();
java.util.jar.JarFile jarFile = new JarFile(new File(signedJarFile));
jarFile.getManifest();
Enumeration<JarEntry> jarEntries = jarFile.entries();
while (jarEntries.hasMoreElements()) {
JarEntry jarEntry = jarEntries.nextElement();
InputStream inputStream = jarFile.getInputStream(jarEntry);
inputStream.skip(Long.MAX_VALUE);
inputStream.close();
if (!jarEntry.getName().startsWith("META-INF") && !jarEntry.isDirectory() && !jarEntry.getName().endsWith("TigerDigest.class")) {
assertThat(jarEntry.getCertificates()).isNotNull();
}
}
jarFile.close();
}
use of java.util.jar.JarEntry in project camel by apache.
the class FatJarPackageScanClassResolver method doLoadJarClassEntries.
protected List<String> doLoadJarClassEntries(InputStream stream, String urlPath, boolean inspectNestedJars, boolean closeStream) {
List<String> entries = new ArrayList<String>();
JarInputStream jarStream = null;
try {
jarStream = new JarInputStream(stream);
JarEntry entry;
while ((entry = jarStream.getNextJarEntry()) != null) {
String name = entry.getName();
if (name != null) {
name = name.trim();
if (!entry.isDirectory() && name.endsWith(".class")) {
entries.add(cleanupSpringbootClassName(name));
} else if (inspectNestedJars && !entry.isDirectory() && isSpringbootNestedJar(name)) {
String nestedUrl = urlPath + "!/" + name;
log.trace("Inspecting nested jar: {}", nestedUrl);
List<String> nestedEntries = doLoadJarClassEntries(jarStream, nestedUrl, false, false);
entries.addAll(nestedEntries);
}
}
}
} catch (IOException ioe) {
log.warn("Cannot search jar file '" + urlPath + " due to an IOException: " + ioe.getMessage(), ioe);
} finally {
if (closeStream) {
// stream is left open when scanning nested jars, otherwise the fat jar stream gets closed
IOHelper.close(jarStream, urlPath, log);
}
}
return entries;
}
use of java.util.jar.JarEntry in project flink by apache.
the class JarFileCreator method createJarFile.
/**
* Creates a jar file which contains the previously added class. The content of the jar file is written to
* <code>outputFile</code> which has been provided to the constructor. If <code>outputFile</code> already exists, it
* is overwritten by this operation.
*
* @throws IOException
* thrown if an error occurs while writing to the output file
*/
public synchronized void createJarFile() throws IOException {
//Retrieve dependencies automatically
addDependencies();
// Temporary buffer for the stream copy
final byte[] buf = new byte[128];
// Check if output file is valid
if (this.outputFile == null) {
throw new IOException("Output file is null");
}
// If output file already exists, delete it
if (this.outputFile.exists()) {
this.outputFile.delete();
}
try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(this.outputFile), new Manifest())) {
final Iterator<Class<?>> it = this.classSet.iterator();
while (it.hasNext()) {
final Class<?> clazz = it.next();
final String entry = clazz.getName().replace('.', '/') + CLASS_EXTENSION;
jos.putNextEntry(new JarEntry(entry));
String name = clazz.getName();
int n = name.lastIndexOf('.');
String className = null;
if (n > -1) {
className = name.substring(n + 1, name.length());
}
//Using the part after last dot instead of class.getSimpleName() could resolve the problem of inner class.
final InputStream classInputStream = clazz.getResourceAsStream(className + CLASS_EXTENSION);
int num = classInputStream.read(buf);
while (num != -1) {
jos.write(buf, 0, num);
num = classInputStream.read(buf);
}
classInputStream.close();
jos.closeEntry();
}
}
}
use of java.util.jar.JarEntry in project camel by apache.
the class DefaultPackageScanClassResolver method doLoadJarClassEntries.
/**
* Loads all the class entries from the JAR.
*
* @param stream the inputstream of the jar file to be examined for classes
* @param urlPath the url of the jar file to be examined for classes
* @return all the .class entries from the JAR
*/
protected List<String> doLoadJarClassEntries(InputStream stream, String urlPath) {
List<String> entries = new ArrayList<String>();
JarInputStream jarStream = null;
try {
jarStream = new JarInputStream(stream);
JarEntry entry;
while ((entry = jarStream.getNextJarEntry()) != null) {
String name = entry.getName();
if (name != null) {
name = name.trim();
if (!entry.isDirectory() && name.endsWith(".class")) {
entries.add(name);
}
}
}
} catch (IOException ioe) {
log.warn("Cannot search jar file '" + urlPath + " due to an IOException: " + ioe.getMessage(), ioe);
} finally {
IOHelper.close(jarStream, urlPath, log);
}
return entries;
}
Aggregations