private void exploreJarFile(File jarFile, List <String> classes) { JarInputStream jarStream = null; try { jarStream = new JarInputStream(new FileInputStream(jarFile)); JarEntry entry; while ((entry = jarStream.getNextJarEntry()) != null) { var name = entry.getName(); if (name.endsWith(".class")) { String className = name.substring(0, name.length() - 6); classes.add(className); } } } finally { try { if (jarStream != null) { jarStream.close(); } } catch (IOException e) { } } }
public virtual void TestExistingManifest() { FilePath dir = new FilePath(Runtime.GetProperty("test.build.dir", "target/test-dir" ), typeof(TestJarFinder).FullName + "-testExistingManifest"); Delete(dir); dir.Mkdirs(); FilePath metaInfDir = new FilePath(dir, "META-INF"); metaInfDir.Mkdirs(); FilePath manifestFile = new FilePath(metaInfDir, "MANIFEST.MF"); Manifest manifest = new Manifest(); OutputStream os = new FileOutputStream(manifestFile); manifest.Write(os); os.Close(); FilePath propsFile = new FilePath(dir, "props.properties"); TextWriter writer = new FileWriter(propsFile); new Properties().Store(writer, string.Empty); writer.Close(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); JarOutputStream zos = new JarOutputStream(baos); JarFinder.JarDir(dir, string.Empty, zos); JarInputStream jis = new JarInputStream(new ByteArrayInputStream(baos.ToByteArray ())); NUnit.Framework.Assert.IsNotNull(jis.GetManifest()); jis.Close(); }
public static string getInternalParserModelName(URL mcoUrl) { string internalParserModelName = null; try { JarEntry je; JarInputStream jis = new JarInputStream(mcoUrl.openConnection().InputStream); while ((je = jis.NextJarEntry) != null) { string fileName = je.Name; jis.closeEntry(); int index = fileName.IndexOf('/'); if (index == -1) { index = fileName.IndexOf('\\'); } if (ReferenceEquals(internalParserModelName, null)) { internalParserModelName = fileName.Substring(0, index); break; } } jis.close(); } catch (IOException e) { Console.WriteLine(e.ToString()); Console.Write(e.StackTrace); } return(internalParserModelName); }
/* * Returns the Manifest for the specified JAR file name. */ private static Manifest LoadManifest(String fn) { try { using (FileInputStream fis = new FileInputStream(fn), JarInputStream jis = new JarInputStream(fis, false)) { return(jis.Manifest); } } catch (IOException) { return(reflect.AnnotatedElement_Fields.Null); } }
/// <summary> /// Reads a jar file entry into a byte array. /// </summary> /// <param name="jis"> The jar input stream </param> /// <param name="jarName"> The name of a jar file entry </param> /// <exception cref="PluginException"> </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: private void loadClassBytes(java.util.jar.JarInputStream jis, String jarName) throws org.maltparser.core.exception.MaltChainedException private void loadClassBytes(JarInputStream jis, string jarName) { BufferedInputStream jarBuf = new BufferedInputStream(jis); MemoryStream jarOut = new MemoryStream(); int b; try { while ((b = jarBuf.read()) != -1) { jarOut.WriteByte(b); } classByteArrays[jarName.Substring(0, jarName.Length - 6)] = jarOut.toByteArray(); } catch (IOException e) { throw new PluginException("Error reading entry " + jarName + ". ", e); } }
/// <summary> /// Loads the content of a jar file that comply with a MaltParser Plugin /// </summary> /// <param name="jarUrl"> The URL to the jar file </param> /// <exception cref="PluginException"> </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public boolean readJarFile(java.net.URL jarUrl) throws org.maltparser.core.exception.MaltChainedException public virtual bool readJarFile(URL jarUrl) { JarInputStream jis; JarEntry je; ISet <URL> pluginXMLs = new Helper.HashSet <URL>(); /*if (logger.isDebugEnabled()) { * logger.debug("Loading jar " + jarUrl+"\n"); * }*/ JarFile jarFile; try { jarFile = new JarFile(jarUrl.File); } catch (IOException e) { throw new PluginException("Could not open jar file " + jarUrl + ". ", e); } try { Manifest manifest = jarFile.Manifest; if (manifest != null) { Attributes manifestAttributes = manifest.MainAttributes; if (!(manifestAttributes.getValue("MaltParser-Plugin") != null && manifestAttributes.getValue("MaltParser-Plugin").Equals("true"))) { return(false); } if (manifestAttributes.getValue("Class-Path") != null) { string[] classPathItems = manifestAttributes.getValue("Class-Path").Split(" "); for (int i = 0; i < classPathItems.Length; i++) { URL u; try { u = new URL(jarUrl.Protocol + ":" + (new File(jarFile.Name)).ParentFile.Path + "/" + classPathItems[i]); } catch (MalformedURLException e) { throw new PluginException("The URL to the plugin jar-class-path '" + jarUrl.Protocol + ":" + (new File(jarFile.Name)).ParentFile.Path + "/" + classPathItems[i] + "' is wrong. ", e); } URLClassLoader sysloader = (URLClassLoader)ClassLoader.SystemClassLoader; Type sysclass = typeof(URLClassLoader); System.Reflection.MethodInfo method = sysclass.getDeclaredMethod("addURL", new Type[] { typeof(URL) }); method.Accessible = true; method.invoke(sysloader, new object[] { u }); } } } } catch (PatternSyntaxException e) { throw new PluginException("Could not split jar-class-path entries in the jar-file '" + jarFile.Name + "'. ", e); } catch (IOException e) { throw new PluginException("Could not read the manifest file in the jar-file '" + jarFile.Name + "'. ", e); } catch (NoSuchMethodException e) { throw new PluginException("", e); } catch (IllegalAccessException e) { throw new PluginException("", e); } catch (InvocationTargetException e) { throw new PluginException("", e); } try { jis = new JarInputStream(jarUrl.openConnection().InputStream); while ((je = jis.NextJarEntry) != null) { string jarName = je.Name; if (jarName.EndsWith(".class", StringComparison.Ordinal)) { /* if (logger.isDebugEnabled()) { * logger.debug(" Loading class: " + jarName+"\n"); * }*/ loadClassBytes(jis, jarName); Type clazz = findClass(jarName.Substring(0, jarName.Length - 6)); classes[jarName.Substring(0, jarName.Length - 6).Replace('/', '.')] = clazz; loadClass(jarName.Substring(0, jarName.Length - 6).Replace('/', '.')); } if (jarName.EndsWith("plugin.xml", StringComparison.Ordinal)) { pluginXMLs.Add(new URL("jar:" + jarUrl.Protocol + ":" + jarUrl.Path + "!/" + jarName)); } jis.closeEntry(); } foreach (URL url in pluginXMLs) { /* if (logger.isDebugEnabled()) { * logger.debug(" Loading "+url+"\n"); * }*/ OptionManager.instance().loadOptionDescriptionFile(url); } } catch (MalformedURLException e) { throw new PluginException("The URL to the plugin.xml is wrong. ", e); } catch (IOException e) { throw new PluginException("cannot open jar file " + jarUrl + ". ", e); } catch (ClassNotFoundException e) { throw new PluginException("The class " + e.Message + " can't be found. ", e); } return(true); }