/** * Outputs the class index table to the INDEX.LIST file of the * root jar file. */ void dumpIndex(String rootjar, JarIndex index) throws IOException { File jarFile = new File(rootjar); Path jarPath = jarFile.toPath(); Path tmpPath = createTempFileInSameDirectoryAs(jarFile).toPath(); try { if (update(Files.newInputStream(jarPath), Files.newOutputStream(tmpPath), null, index)) { try { Files.move(tmpPath, jarPath, REPLACE_EXISTING); } catch (IOException e) { throw new IOException(getMsg("error.write.file"), e); } } } finally { Files.deleteIfExists(tmpPath); } }
/** * Generates class index file for the specified root jar file. */ void genIndex(String rootjar, String[] files) throws IOException { List<String> jars = getJarPath(rootjar); int njars = jars.size(); String[] jarfiles; if (njars == 1 && files != null) { // no class-path attribute defined in rootjar, will // use command line specified list of jars for (int i = 0; i < files.length; i++) { jars.addAll(getJarPath(files[i])); } njars = jars.size(); } jarfiles = jars.toArray(new String[njars]); JarIndex index = new JarIndex(jarfiles); dumpIndex(rootjar, index); }
/** * Reads the next ZIP file entry and positions the stream at the * beginning of the entry data. If verification has been enabled, * any invalid signature detected while positioning the stream for * the next entry will result in an exception. * @exception ZipException if a ZIP file error has occurred * @exception IOException if an I/O error has occurred * @exception SecurityException if any of the jar file entries * are incorrectly signed. */ public ZipEntry getNextEntry() throws IOException { JarEntry e; if (first == null) { e = (JarEntry)super.getNextEntry(); if (tryManifest) { e = checkManifest(e); tryManifest = false; } } else { e = first; if (first.getName().equalsIgnoreCase(JarIndex.INDEX_NAME)) tryManifest = true; first = null; } if (jv != null && e != null) { // At this point, we might have parsed all the meta-inf // entries and have nothing to verify. If we have // nothing to verify, get rid of the JarVerifier object. if (jv.nothingToVerify() == true) { jv = null; mev = null; } else { jv.beginEntry(e, mev); } } return e; }
JarIndex getIndex() { try { ensureOpen(); } catch (IOException e) { throw new InternalError(e); } return index; }
private void addIndex(JarIndex index, ZipOutputStream zos) throws IOException { ZipEntry e = new ZipEntry(INDEX_NAME); e.setTime(System.currentTimeMillis()); if (flag0) { CRC32OutputStream os = new CRC32OutputStream(); index.write(os); os.updateEntry(e); } zos.putNextEntry(e); index.write(zos); zos.closeEntry(); }
public static void main(String[] args) throws Exception { File jar1 = buildJar1(); File jar2 = buildJar2(); JarIndex jarIndex1 = new JarIndex(new String[] { jar1.getAbsolutePath() }); JarIndex jarIndex2 = new JarIndex(new String[] { jar2.getAbsolutePath() }); jarIndex1.merge(jarIndex2, null); assertFileResolved(jarIndex2, "com/test1/resource1.file", jar1.getAbsolutePath()); assertFileResolved(jarIndex2, "com/test2/resource2.file", jar2.getAbsolutePath()); }
static void assertFileResolved(JarIndex jarIndex2, String file, String jarName) { @SuppressWarnings("unchecked") LinkedList<String> jarLists = (LinkedList<String>)jarIndex2.get(file); if (jarLists == null || jarLists.size() == 0 || !jarName.equals(jarLists.get(0))) { throw new RuntimeException( "Unexpected result: the merged index must resolve file: " + file); } }