use of java.util.jar.JarEntry in project robovm by robovm.
the class OldJarFileTest method test_getInputStreamLjava_util_jar_JarEntry.
public void test_getInputStreamLjava_util_jar_JarEntry() throws IOException {
Support_Resources.copyFile(resources, null, jarName);
File localFile = new File(resources, jarName);
byte[] b = new byte[1024];
JarFile jf = new JarFile(localFile);
InputStream is = jf.getInputStream(jf.getEntry(entryName));
assertTrue("Returned invalid stream", is.available() > 0);
int r = is.read(b, 0, 1024);
is.close();
StringBuilder stringBuffer = new StringBuilder(r);
for (int i = 0; i < r; i++) {
stringBuffer.append((char) (b[i] & 0xff));
}
String contents = stringBuffer.toString();
assertTrue("Incorrect stream read", contents.indexOf("bar") > 0);
jf.close();
jf = new JarFile(localFile);
InputStream in = jf.getInputStream(new JarEntry("invalid"));
assertNull("Got stream for non-existent entry", in);
try {
Support_Resources.copyFile(resources, null, jarName);
File signedFile = new File(resources, jarName);
jf = new JarFile(signedFile);
JarEntry jre = new JarEntry("foo/bar/A.class");
jf.getInputStream(jre);
// InputStream returned in any way, exception can be thrown in case
// of reading from this stream only.
// fail("Should throw ZipException");
} catch (ZipException expected) {
}
try {
Support_Resources.copyFile(resources, null, jarName);
File signedFile = new File(resources, jarName);
jf = new JarFile(signedFile);
JarEntry jre = new JarEntry("foo/bar/A.class");
jf.close();
jf.getInputStream(jre);
// InputStream returned in any way, exception can be thrown in case
// of reading from this stream only.
// The same for IOException
fail("Should throw IllegalStateException");
} catch (IllegalStateException expected) {
}
}
use of java.util.jar.JarEntry in project jetty.project by eclipse.
the class JarScanner method matched.
public void matched(URI uri) throws Exception {
LOG.debug("Search of {}", uri);
if (uri.toString().toLowerCase(Locale.ENGLISH).endsWith(".jar")) {
InputStream in = Resource.newResource(uri).getInputStream();
if (in == null)
return;
JarInputStream jar_in = new JarInputStream(in);
try {
JarEntry entry = jar_in.getNextJarEntry();
while (entry != null) {
processEntry(uri, entry);
entry = jar_in.getNextJarEntry();
}
} finally {
jar_in.close();
}
}
}
use of java.util.jar.JarEntry in project jetty.project by eclipse.
the class MetaInfConfiguration method getTlds.
/**
* Find all .tld files in the given jar.
*
* @param uri the uri to jar file
* @return the collection of tlds as url references
* @throws IOException if unable to scan the jar file
*/
public Collection<URL> getTlds(URI uri) throws IOException {
HashSet<URL> tlds = new HashSet<URL>();
String jarUri = uriJarPrefix(uri, "!/");
URL url = new URL(jarUri);
JarURLConnection jarConn = (JarURLConnection) url.openConnection();
jarConn.setUseCaches(Resource.getDefaultUseCaches());
JarFile jarFile = jarConn.getJarFile();
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry e = entries.nextElement();
String name = e.getName();
if (name.startsWith("META-INF") && name.endsWith(".tld")) {
tlds.add(new URL(jarUri + name));
}
}
if (!Resource.getDefaultUseCaches())
jarFile.close();
return tlds;
}
use of java.util.jar.JarEntry in project buck by facebook.
the class ClassNodeListSupplierTest method testOneJar.
@Test
public void testOneJar() throws IOException {
File jar = new File(tmpDir.getRoot(), "primary.jar");
ZipOutputStream jarOut = new JarOutputStream(new FileOutputStream(jar));
jarOut.putNextEntry(new JarEntry("com/facebook/buck/android/ClassNodeListSupplierTest.class"));
writeClassBytes(ClassNodeListSupplierTest.class, jarOut);
jarOut.close();
Supplier<ImmutableList<ClassNode>> supplier = ClassNodeListSupplier.createMemoized(ImmutableList.of(jar.toPath()));
ImmutableList<ClassNode> classNodes = supplier.get();
assertEquals(1, classNodes.size());
assertEquals(Type.getType(ClassNodeListSupplierTest.class).getInternalName(), classNodes.get(0).name);
// Memoized should always return the same object
assertSame(classNodes, supplier.get());
}
use of java.util.jar.JarEntry in project jetty.project by eclipse.
the class SelectiveJarResource method copyTo.
/**
* @see org.eclipse.jetty.util.resource.JarResource#copyTo(java.io.File)
*/
@Override
public void copyTo(File directory) throws IOException {
if (_includes == null)
_includes = DEFAULT_INCLUDES;
if (_excludes == null)
_excludes = DEFAULT_EXCLUDES;
//parts of the jar file are copied
if (!exists())
return;
String urlString = this.getURL().toExternalForm().trim();
int endOfJarUrl = urlString.indexOf("!/");
int startOfJarUrl = (endOfJarUrl >= 0 ? 4 : 0);
if (endOfJarUrl < 0)
throw new IOException("Not a valid jar url: " + urlString);
URL jarFileURL = new URL(urlString.substring(startOfJarUrl, endOfJarUrl));
try (InputStream is = jarFileURL.openConnection().getInputStream();
JarInputStream jin = new JarInputStream(is)) {
JarEntry entry;
while ((entry = jin.getNextJarEntry()) != null) {
String entryName = entry.getName();
LOG.debug("Looking at " + entryName);
String dotCheck = entryName.replace('\\', '/');
dotCheck = URIUtil.canonicalPath(dotCheck);
if (dotCheck == null) {
LOG.info("Invalid entry: " + entryName);
continue;
}
File file = new File(directory, entryName);
if (entry.isDirectory()) {
if (isIncluded(entryName)) {
if (!isExcluded(entryName)) {
// Make directory
if (!file.exists())
file.mkdirs();
} else
LOG.debug("{} dir is excluded", entryName);
} else
LOG.debug("{} dir is NOT included", entryName);
} else {
//entry is a file, is it included?
if (isIncluded(entryName)) {
if (!isExcluded(entryName)) {
// make directory (some jars don't list dirs)
File dir = new File(file.getParent());
if (!dir.exists())
dir.mkdirs();
// Make file
try (OutputStream fout = new FileOutputStream(file)) {
IO.copy(jin, fout);
}
// touch the file.
if (entry.getTime() >= 0)
file.setLastModified(entry.getTime());
} else
LOG.debug("{} file is excluded", entryName);
} else
LOG.debug("{} file is NOT included", entryName);
}
}
Manifest manifest = jin.getManifest();
if (manifest != null) {
if (isIncluded("META-INF") && !isExcluded("META-INF")) {
File metaInf = new File(directory, "META-INF");
metaInf.mkdir();
File f = new File(metaInf, "MANIFEST.MF");
try (OutputStream fout = new FileOutputStream(f)) {
manifest.write(fout);
}
}
}
}
}
Aggregations