archive.java
来自「OSGI这是一个中间件,与UPNP齐名,是用于移植到嵌入式平台之上」· Java 代码 · 共 666 行 · 第 1/2 页
JAVA
666 行
* Get an attribute from the manifest of the archive. * * @param key Name of attribute to get. * @return A string with result or null if the entry doesn't exists. */ String getAttribute(String key) { Attributes a = manifest.getMainAttributes(); if (a != null) { return a.getValue(key); } return null; } /** * Get all attributes from the manifest of the archive. * * @return All attributes. */ Attributes getAttributes() { return manifest.getMainAttributes(); } /** * Get a byte array containg the contents of named file from * the archive. * * @param component File to get. * @return Byte array with contents of file or null if file doesn't exist. * @exception IOException if failed to read jar entry. */ byte[] getBytes(String component) throws IOException { if(bClosed) { return null; } ZipEntry ze; InputStream is; int len; if (jar != null) { if (subJar != null) { JarInputStream ji = new JarInputStream(jar.getInputStream(subJar)); do { ze = ji.getNextJarEntry(); if (ze == null) { ji.close(); return null; } } while (!component.equals(ze.getName())); is = (InputStream)ji; } else { ze = jar.getEntry(component); if (ze == null) { return null; } is = jar.getInputStream(ze); } len = (int)ze.getSize(); } else { File f = findFile(file, component); if (f.exists()) { is = new FileInputStream(f); len = is.available(); } else { return null; } } byte[] bytes; if (len >= 0) { bytes = new byte[len]; DataInputStream dis = new DataInputStream(is); dis.readFully(bytes); } else { bytes = new byte[0]; byte[] tmp = new byte[8192]; try { while ((len = is.read(tmp)) > 0) { byte[] oldbytes = bytes; bytes = new byte[oldbytes.length + len]; System.arraycopy(oldbytes, 0, bytes, 0, oldbytes.length); System.arraycopy(tmp, 0, bytes, oldbytes.length, len); } } catch (EOFException ignore) { // On Pjava we somtimes get a mysterious EOF excpetion, // but everything seems okey. (SUN Bug 4040920) } } is.close(); return bytes; } /** * Get an InputStream to named entry inside an Archive. * * @param component Entry to get reference to. * @return InputStream to entry or null if it doesn't exist. */ InputStream getInputStream(String component) { if(bClosed) { return null; } if (component.startsWith("/")) { component = component.substring(1); } ZipEntry ze; InputStream is; try { if (jar != null) { if (subJar != null) { JarInputStream ji = new JarInputStream(jar.getInputStream(subJar)); do { ze = ji.getNextJarEntry(); if (ze == null) { ji.close(); return null; } } while (!component.equals(ze.getName())); is = (InputStream)ji; } else { ze = jar.getEntry(component); is = (ze != null) ? jar.getInputStream(ze) : null; } } else { File f = findFile(file, component); is = f.exists() ? new FileInputStream(f) : null; } return is; } catch (IOException ignore) { return null; } } /** * Get an Archive handle to a named Jar file within this archive. * * @param path Name of Jar file to get. * @return An Archive object representing new archive. * @exception FileNotFoundException if no such Jar file in archive. * @exception IOException if failed to read Jar file. */ Archive getSubArchive(String path) throws IOException { if(bClosed) { return null; } return new Archive(this, path); } /** * Extract native library from JAR. * * @param key Name of Jar file to get. * @return A string with path to native library. */ String getNativeLibrary(String path) throws IOException { if(bClosed) { throw new IOException("Archive is closed"); } File lib; if (jar != null) { lib = getSubFile(this, path); if (!lib.exists()) { (new File(lib.getParent())).mkdirs(); ZipEntry ze = jar.getEntry(path); if (ze != null) { InputStream is = jar.getInputStream(ze); try { loadFile(lib, is, false); } finally { is.close(); } } else { throw new FileNotFoundException("No such sub-archive: " + path); } } } else { lib = findFile(file, path);//XXX - start L-3 modification if (!lib.exists() && (lib.getParent() != null)) { final String libname = lib.getName(); File[] list = lib.getParentFile().listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { int pos = name.lastIndexOf(libname); return ((pos > 1) && (name.charAt(pos - 1) == '_')); } }); if (list.length > 0) { lib = list[0]; } }//XXX - end L-3 modification } return lib.getAbsolutePath(); } /** * Remove archive and any unpacked sub-archives. */ void purge() { close(); // Remove archive file.delete(); // Remove any cached sub files getSubFileTree(this).delete(); } boolean bClosed = false; /** * Close archive and all open sub-archives. * If close fails it is silently ignored. */ void close() { bClosed = true; // Mark as closed to safely handle referenced files if (subJar == null && jar != null) { try { jar.close(); } catch (IOException ignore) {} } } // // Private methods // /** * Check that we have a valid manifest for the bundle * and if we need to unpack bundle for fast performance, * i.e has native code file or subjars. * * @return true if bundle needs to be unpack. * @exception IllegalArgumentException if we have a broken manifest. */ private boolean checkManifest() { Attributes a = manifest.getMainAttributes(); Util.parseEntries(Constants.EXPORT_PACKAGE, a.getValue(Constants.EXPORT_PACKAGE), true); Util.parseEntries(Constants.IMPORT_PACKAGE, a.getValue(Constants.IMPORT_PACKAGE), true); Iterator nc = Util.parseEntries(Constants.BUNDLE_NATIVECODE, a.getValue(Constants.BUNDLE_NATIVECODE), false); String bc = a.getValue(Constants.BUNDLE_CLASSPATH); return (bc != null && !bc.trim().equals(".")) || nc.hasNext(); } /** * Get file handle for file inside a directory structure. * The path for the file is always specified with a '/' * separated path. * * @param root Directory structure to search. * @param path Path to file to find. * @return The File object for file <code>path</code>. */ private File findFile(File root, String path) { return new File(root, path.replace('/', File.separatorChar)); } /** * Get the manifest for this archive. * * @return The manifest for this Archive */ private Manifest getManifest() throws IOException { // TBD: Should recognize entry with lower case? InputStream is = getInputStream("META-INF/MANIFEST.MF"); if (is != null) { return new Manifest(is); } else { throw new IOException("Manifest is missing"); } } /** * Get dir for unpacked components. * * @param archive Archive which contains the components. * @return FileTree for archives component cache directory. */ private FileTree getSubFileTree(Archive archive) { return new FileTree(archive.file.getParent(), SUBDIR + archive.file.getName().substring(ARCHIVE.length())); } /** * Get file for an unpacked component. * * @param archive Archive which contains the component. * @param path Name of the component to get. * @return File for componets cache file. */ private File getSubFile(Archive archive, String path) { return new File(getSubFileTree(archive), path.replace('/', '-')); } /** * Loads a file from an InputStream and stores it in a file. * * @param output File to save data in. * @param is InputStream to read from. */ private void loadFile(File output, InputStream is, boolean verify) throws IOException { OutputStream os = null; // NYI! Verify try { os = new FileOutputStream(output); byte[] buf = new byte[8192]; int n; try { while ((n = is.read(buf)) > 0) { os.write(buf, 0, n); } } catch (EOFException ignore) { // On Pjava we somtimes get a mysterious EOF excpetion, // but everything seems okey. (SUN Bug 4040920) } } catch (IOException e) { output.delete(); throw e; } finally { if (os != null) { os.close(); } } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?