Code example for JarFile

Methods: getInputStreamgetJarEntrygetManifest

0
	 */ 
	@Override 
	protected Class<?> findClass(String name) throws ClassNotFoundException {
		try { 
			String pathName = transformName(name);
			JarEntry jarEntry = tempJarFile.getJarEntry(pathName);
			if (jarEntry != null) {
				long size = jarEntry.getSize();
				InputStream jarEntryInputStream = tempJarFile.getInputStream(jarEntry);
				ByteArrayOutputStream classBytesOutputStream = new ByteArrayOutputStream((int) size);
				FileUtil.copy(jarEntryInputStream, classBytesOutputStream, size);
				classBytesOutputStream.close();
				jarEntryInputStream.close();
				byte[] classBytes = classBytesOutputStream.toByteArray();
 
				definePackage(name);
					 
				Class<?> clazz = defineClass(name, classBytes, 0, classBytes.length);
				return clazz;
			} 
			throw new ClassNotFoundException("could not find jar entry for class " + name);
		} catch (IOException e) {
			throw new ClassNotFoundException(e.getMessage(), e);
		} 
	} 
 
	/** 
	 * {@inheritDoc} 
	 * <p> 
	 * Finds the resource within this jar only. If it isn't found within the jar, getResourceAsStream() 
	 * will look elsewhere. 
	 */ 
	@Override 
	protected URL findResource(String name) {
		/* FIXME compatibility code. remove when all plugins are fixed. */ 
		if (name.startsWith("/")) {
			name = name.substring(1);
		} 
		try { 
			if(tempJarFile.getJarEntry(name)==null) {
				return null; 
			} 
 
			return new URL("jar:" + new File(tempJarFile.getName()).toURI().toURL() + "!/" + name);
		} catch (MalformedURLException e) {
		} 
		return null; 
	} 
	 
	/** 
	 * {@inheritDoc} 
	 * <p> 
	 * If the resource is found in this jar, opens the stream using ZipEntry's, 
	 * so when tempJarFile is closed, so are all the streams, hence we can delete 
	 * the jar on Windows. 
	 *  
	 * @see java.lang.ClassLoader#getResourceAsStream(java.lang.String) 
	 */ 
	@Override 
	public InputStream getResourceAsStream(String name) {
		if(logMINOR) Logger.minor(this, "Requested resource: " + name, new Exception("debug"));
		URL url = getResource(name);
		if (url == null)
			return null; 
		if(logMINOR) Logger.minor(this, "Found resource at URL: " + url);
 
		// If the resource is not from our jar, return it as normal 
		URL localUrl = findResource(name);
		if (localUrl == null || !url.toString().equals(localUrl.toString()))
			try { 
				return url.openStream();
			} catch (IOException e) {
				return null; 
			} 
 
		// If the resource is from our jar, open InputStream explicitly from the jar 
		// so that we can close() all opened streams later and let the jar file 
		// to be deleted on Windows 
 
		/* FIXME compatibility code. remove when all plugins are fixed. */ 
		if (name.startsWith("/")) {
			name = name.substring(1);
		} 
 
		ZipEntry entry = tempJarFile.getEntry(name);
		try { 
			return entry != null ? tempJarFile.getInputStream(entry) : null;
		} catch (IOException e) {
			return null; 
		} 
	} 
 
	/** 
	 * Transforms the class name into a file name that can be used to locate 
	 * an entry in the jar file. 
	 *  
	 * @param name 
	 *            The name of the class 
	 * @return The path name of the entry in the jar file 
	 */ 
	private String transformName(String name) {
		return name.replace('.', '/') + ".class";
	} 
	 
	protected Package definePackage(String name) throws IllegalArgumentException {
		Package pkg = null;
		int i = name.lastIndexOf('.');
		if (i != -1) {
			String pkgname = name.substring(0, i);
			pkg = getPackage(pkgname);
			if (pkg == null) {
				try { 
					Manifest man = tempJarFile.getManifest();
					if(man == null) throw new IOException();
					pkg = definePackage(pkgname, man);
				} catch (IOException e) {
					pkg = definePackage(pkgname, null, null, null, null, null, null, null);
				} 
Connect your IDE to all the code out there  Get Codota for Java