public void copyJar(String inJar, JarOutputStream out, boolean verbose) throws Exception { File file = new File(inJar); InputStream input = new FileInputStream(file); JarInputStream in = new JarInputStream(input); JarEntry entry; while ((entry = in.getNextJarEntry()) != null) { String name = entry.getName(); if (name.equals("META-INF/MANIFEST.MF")) { in.closeEntry(); continue; } byte[] buf = Util.readStream(in); in.closeEntry(); entry.setCompressedSize(-1); writeJarEntry(entry, out, buf); } in.close(); }
// TODO: we really need string pairs; real path and desired path. protected void makeJar( String path, String mainClass, List<String> files, File cwd, File buildDir, String configPath, String stripPath, boolean verbose) throws FakeException { path = Util.makePath(cwd, path); if (verbose) { String output = "Making " + path; if (mainClass != null) output += " with main-class " + mainClass; output += " from"; for (String file : files) output += " " + file; err.println(output); } Manifest manifest = null; if (mainClass != null) { String text = "Manifest-Version: 1.0\nMain-Class: " + mainClass + "\n"; InputStream input = new ByteArrayInputStream(text.getBytes()); try { manifest = new Manifest(input); } catch (Exception e) { } } try { /* * Avoid SIGBUS when writing fake.jar: it may be * in use (mmap()ed), and overwriting that typically * results in a crash. */ String origPath = null; try { if (Util.moveFileOutOfTheWay(path)) { origPath = path; path += ".new"; } } catch (FakeException e) { path = moveToUpdateDirectory(path); } OutputStream out = new FileOutputStream(path); JarOutputStream jar = manifest == null ? new JarOutputStream(out) : new JarOutputStream(out, manifest); addPluginsConfigToJar(jar, configPath); String lastBase = stripPath; for (String realName : files) { if (realName.endsWith(".jar/")) { copyJar(Util.stripSuffix(Util.makePath(cwd, realName), "/"), jar, verbose); continue; } if (realName.endsWith("/") || realName.endsWith("\\") || realName.equals("")) { lastBase = realName; continue; } String name = realName; int bracket = name.indexOf('['); if (bracket >= 0 && name.endsWith("]")) { realName = name.substring(bracket + 1, name.length() - 1); name = name.substring(0, bracket); } byte[] buffer = Util.readFile(Util.makePath(cwd, realName)); if (buffer == null) throw new FakeException( "File " + realName + " does not exist," + " could not make " + path); if (realName.endsWith(".class")) { ByteCodeAnalyzer analyzer = new ByteCodeAnalyzer(buffer); name = analyzer.getPathForClass() + ".class"; } else if (lastBase != null && name.startsWith(lastBase)) { if (!lastBase.equals(stripPath)) throw new FakeException("strip " + "path mismatch: " + lastBase + " != " + stripPath); name = name.substring(lastBase.length()); } JarEntry entry = new JarEntry(name); writeJarEntry(entry, jar, buffer); } jar.close(); if (origPath != null) throw new FakeException( "Could not remove " + origPath + " before building it anew\n" + "Stored it as " + path + " instead."); } catch (Exception e) { new File(path).delete(); e.printStackTrace(); throw new FakeException("Error writing " + path + ": " + e); } }