/** * Load the policies from the specified file. Also checks that the policies are correctly signed. */ private static void loadPolicies( File jarPathName, CryptoPermissions defaultPolicy, CryptoPermissions exemptPolicy) throws Exception { JarFile jf = new JarFile(jarPathName); Enumeration<JarEntry> entries = jf.entries(); while (entries.hasMoreElements()) { JarEntry je = entries.nextElement(); InputStream is = null; try { if (je.getName().startsWith("default_")) { is = jf.getInputStream(je); defaultPolicy.load(is); } else if (je.getName().startsWith("exempt_")) { is = jf.getInputStream(je); exemptPolicy.load(is); } else { continue; } } finally { if (is != null) { is.close(); } } // Enforce the signer restraint, i.e. signer of JCE framework // jar should also be the signer of the two jurisdiction policy // jar files. JarVerifier.verifyPolicySigned(je.getCertificates()); } // Close and nullify the JarFile reference to help GC. jf.close(); jf = null; }
/** Returns true if the two InputStreams differ. */ private static boolean differs(InputStream oldIS, InputStream newIS) throws IOException { int newSize = 0; int oldSize; int total = 0; boolean retVal = false; try { while (newSize != -1) { newSize = newIS.read(newBytes); oldSize = oldIS.read(oldBytes); if (newSize != oldSize) { if (_debug) { System.out.println( "\tread sizes differ: " + newSize + " " + oldSize + " total " + total); } retVal = true; break; } if (newSize > 0) { while (--newSize >= 0) { total++; if (newBytes[newSize] != oldBytes[newSize]) { if (_debug) { System.out.println("\tbytes differ at " + total); } retVal = true; break; } if (retVal) { // Jump out break; } newSize = 0; } } } } catch (IOException ioE) { throw ioE; } finally { try { oldIS.close(); } catch (IOException e) { // Ignore } try { newIS.close(); } catch (IOException e) { // Ignore } } return retVal; }
/** Returns true if the two InputStreams differ. */ private static boolean differs(InputStream oldIS, InputStream newIS) throws IOException { int newSize = 0; int oldSize; int total = 0; while (newSize != -1) { newSize = newIS.read(newBytes); oldSize = oldIS.read(oldBytes); if (newSize != oldSize) { if (_debug) { System.out.println( "\tread sizes differ: " + newSize + " " + oldSize + " total " + total); } return true; } if (newSize > 0) { while (--newSize >= 0) { total++; if (newBytes[newSize] != oldBytes[newSize]) { if (_debug) { System.out.println("\tbytes differ at " + total); } return true; } } newSize = 0; } } return false; }
static { Attributes m = null; final URL loc = Prop.LOCATION; if (loc != null) { final String jar = loc.getFile(); try { final ClassLoader cl = JarManifest.class.getClassLoader(); final Enumeration<URL> list = cl.getResources("META-INF/MANIFEST.MF"); while (list.hasMoreElements()) { final URL url = list.nextElement(); if (!url.getFile().contains(jar)) continue; final InputStream in = url.openStream(); try { m = new Manifest(in).getMainAttributes(); break; } finally { in.close(); } } } catch (final IOException ex) { Util.errln(ex); } } MAP = m; }
/** * Copies all bytes from the input file to the output stream. Does not close or flush the output * stream. * * @param from the input file to read from * @param to the output stream to write to * @throws IOException if an I/O error occurs */ private void copy(File from, OutputStream to) throws IOException { InputStream in = new FileInputStream(from); try { copy(in, to); } finally { in.close(); } }
void copy(InputStream in, File dest) throws IOException { dest.getParentFile().mkdirs(); OutputStream out = new BufferedOutputStream(new FileOutputStream(dest)); try { byte[] data = new byte[8192]; int n; while ((n = in.read(data, 0, data.length)) > 0) out.write(data, 0, n); } finally { out.close(); in.close(); } }
private static void writeEntry(JarOutputStream jos, JarEntry entry, InputStream data) throws IOException { jos.putNextEntry(entry); // Read the entry int size = data.read(newBytes); while (size != -1) { jos.write(newBytes, 0, size); size = data.read(newBytes); } data.close(); }
public static int writeStreamTo( final InputStream input, final OutputStream output, int bufferSize) throws IOException { int available = Math.min(input.available(), 256 * KB); byte[] buffer = new byte[Math.max(bufferSize, available)]; int answer = 0; int count = input.read(buffer); while (count >= 0) { output.write(buffer, 0, count); answer += count; count = input.read(buffer); } return answer; }
private void digest(MessageDigest[] algorithms, Resource r) throws Exception { InputStream in = r.openInputStream(); byte[] data = new byte[BUFFER_SIZE]; int size = in.read(data); while (size > 0) { for (int a = 0; a < algorithms.length; a++) { if (algorithms[a] != null) { algorithms[a].update(data, 0, size); } } size = in.read(data); } }
/** * Puts the uninstaller. * * @exception Exception Description of the Exception */ private void putUninstaller() throws Exception { // Me make the .uninstaller directory String dest = translatePath("$INSTALL_PATH") + File.separator + "Uninstaller"; String jar = dest + File.separator + "uninstaller.jar"; File pathMaker = new File(dest); pathMaker.mkdirs(); // We log the uninstaller deletion information UninstallData udata = UninstallData.getInstance(); udata.setUninstallerJarFilename(jar); udata.setUninstallerPath(dest); // We open our final jar file FileOutputStream out = new FileOutputStream(jar); ZipOutputStream outJar = new ZipOutputStream(out); idata.uninstallOutJar = outJar; outJar.setLevel(9); udata.addFile(jar); // We copy the uninstaller InputStream in = getClass().getResourceAsStream("/res/IzPack.uninstaller"); ZipInputStream inRes = new ZipInputStream(in); ZipEntry zentry = inRes.getNextEntry(); while (zentry != null) { // Puts a new entry outJar.putNextEntry(new ZipEntry(zentry.getName())); // Byte to byte copy int unc = inRes.read(); while (unc != -1) { outJar.write(unc); unc = inRes.read(); } // Next one please inRes.closeEntry(); outJar.closeEntry(); zentry = inRes.getNextEntry(); } inRes.close(); // We put the langpack in = getClass().getResourceAsStream("/langpacks/" + idata.localeISO3 + ".xml"); outJar.putNextEntry(new ZipEntry("langpack.xml")); int read = in.read(); while (read != -1) { outJar.write(read); read = in.read(); } outJar.closeEntry(); }
protected void addRuntimeClasses(JarOutputStream jar) throws IOException { ClassLoader cl = Thread.currentThread().getContextClassLoader(); if (cl == null) { cl = getClass().getClassLoader(); } ArrayList<String> runtimeClasses = getRuntimeClasses(cl); for (String c : runtimeClasses) { jar.putNextEntry(new JarEntry(c)); InputStream in = cl.getResourceAsStream(c); int ch; while ((ch = in.read()) != -1) { jar.write((char) ch); } jar.closeEntry(); in.close(); } }
public byte[] getBytes(String className) { try { Tracer.mark(); String realName = className.replace(".", "/"); realName += ".class"; JarEntry je = jf.getJarEntry(realName); InputStream is = jf.getInputStream(je); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buff = new byte[4096]; while (is.available() > 0) { int read = is.read(buff); baos.write(buff, 0, read); } is.close(); return baos.toByteArray(); } catch (Exception e) { } finally { Tracer.unmark(); } return null; }
private Class<?> getClassFromStream( final InputStream stream, final String classname, final File container) throws IOException, SecurityException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); int bytesRead = -1; final byte[] buffer = new byte[8192]; while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) { baos.write(buffer, 0, bytesRead); } final byte[] classData = baos.toByteArray(); return this.defineClassFromData(container, classData, classname); }
public static void unpack(File inFile) { JarOutputStream out = null; InputStream in = null; String inName = inFile.getPath(); String outName; if (inName.endsWith(".pack.gz")) { outName = inName.substring(0, inName.length() - 8); } else if (inName.endsWith(".pack")) { outName = inName.substring(0, inName.length() - 5); } else { outName = inName + ".unpacked"; } try { Pack200.Unpacker unpacker = Pack200.newUnpacker(); out = new JarOutputStream(new FileOutputStream(outName)); in = new FileInputStream(inName); if (inName.endsWith(".gz")) in = new GZIPInputStream(in); unpacker.unpack(in, out); } catch (IOException ex) { ex.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException ex) { System.err.println("Error closing file: " + ex.getMessage()); } } if (out != null) { try { out.flush(); out.close(); } catch (IOException ex) { System.err.println("Error closing file: " + ex.getMessage()); } } } }
private static void writeEntry(JarOutputStream jos, JarEntry entry, InputStream data) throws IOException { jos.putNextEntry(entry); try { // Read the entry int size = data.read(newBytes); while (size != -1) { jos.write(newBytes, 0, size); size = data.read(newBytes); } } catch (IOException ioE) { throw ioE; } finally { try { data.close(); } catch (IOException e) { // Ignore } } }
static String getResourceAsString(String path) { InputStream is = null; ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream outPrint = new PrintStream(out); try { is = Util.class.getClassLoader().getResourceAsStream(path); int content; while ((content = is.read()) != -1) { // convert to char and display it outPrint.print((char) content); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (is != null) is.close(); if (outPrint != null) outPrint.close(); } catch (IOException ex) { ex.printStackTrace(); } } return out.toString(); }
/** * Extracts next entry from JAR file, creating directories as needed. If the entry is for a * directory which doesn't exist prior to this invocation, returns that entry, otherwise returns * null. */ ZipEntry extractFile(InputStream is, ZipEntry e) throws IOException { ZipEntry rc = null; String name = e.getName(); File f = new File(e.getName().replace('/', File.separatorChar)); if (e.isDirectory()) { if (f.exists()) { if (!f.isDirectory()) { throw new IOException(formatMsg("error.create.dir", f.getPath())); } } else { if (!f.mkdirs()) { throw new IOException(formatMsg("error.create.dir", f.getPath())); } else { rc = e; } } if (vflag) { output(formatMsg("out.create", name)); } } else { if (f.getParent() != null) { File d = new File(f.getParent()); if (!d.exists() && !d.mkdirs() || !d.isDirectory()) { throw new IOException(formatMsg("error.create.dir", d.getPath())); } } try { copy(is, f); } finally { if (is instanceof ZipInputStream) ((ZipInputStream) is).closeEntry(); else is.close(); } if (vflag) { if (e.getMethod() == ZipEntry.DEFLATED) { output(formatMsg("out.inflated", name)); } else { output(formatMsg("out.extracted", name)); } } } if (!useExtractionTime) { long lastModified = e.getTime(); if (lastModified != -1) { f.setLastModified(lastModified); } } return rc; }
/** * The alt-dd element specifies a URI to the post-assembly deployment descriptor relative to the * root of the application * * @param descriptor the Application deployment descriptor * @return <code>Result</code> the results for this assertion */ public Result check(Application descriptor) { Result result = getInitializedResult(); if (descriptor.getEjbBundleDescriptors().size() > 0) { boolean oneFailed = false; int na = 0; for (Iterator itr = descriptor.getEjbBundleDescriptors().iterator(); itr.hasNext(); ) { EjbBundleDescriptor ejbd = (EjbBundleDescriptor) itr.next(); if (ejbd.getModuleDescriptor().getAlternateDescriptor() != null) { if (!(ejbd.getModuleDescriptor().getAlternateDescriptor().equals(""))) { JarFile jarFile = null; InputStream deploymentEntry = null; // File f = null; // if (Verifier.getEarFile() != null) // f = new File(Verifier.getEarFile()); try { // if (f==null){ String uri = getAbstractArchiveUri(descriptor); // try { FileArchive arch = new FileArchive(); arch.open(uri); deploymentEntry = arch.getEntry(ejbd.getModuleDescriptor().getAlternateDescriptor()); // }catch (Exception e) { } // }else{ // // jarFile = new JarFile(f); // ZipEntry deploymentEntry1 = // jarFile.getEntry(ejbd.getModuleDescriptor().getAlternateDescriptor()); // if (deploymentEntry1 != null){ // deploymentEntry = // jarFile.getInputStream(deploymentEntry1); // } // } if (deploymentEntry != null) { result.addGoodDetails( smh.getLocalString( getClass().getName() + ".passed", "Found alternate EJB deployment descriptor URI file [ {0} ] within [ {1} ]", new Object[] { ejbd.getModuleDescriptor().getAlternateDescriptor(), ejbd.getName() })); } else { if (!oneFailed) { oneFailed = true; } result.addErrorDetails( smh.getLocalString( getClass().getName() + ".failed", "Error: No alternate EJB deployment descriptor URI file found, looking for [ {0} ] within [ {1} ]", new Object[] { ejbd.getModuleDescriptor().getAlternateDescriptor(), ejbd.getName() })); } // jarFile.close(); } catch (FileNotFoundException ex) { Verifier.debug(ex); if (!oneFailed) { oneFailed = true; } result.failed( smh.getLocalString( getClass().getName() + ".failedException", "Error: File not found trying to read deployment descriptor file [ {0} ] within [ {1} ]", new Object[] { ejbd.getModuleDescriptor().getAlternateDescriptor(), ejbd.getName() })); } catch (IOException ex) { Verifier.debug(ex); if (!oneFailed) { oneFailed = true; } result.failed( smh.getLocalString( getClass().getName() + ".failedException1", "Error: IO Error trying to read deployment descriptor file [ {0} ] within [ {1} ]", new Object[] { ejbd.getModuleDescriptor().getAlternateDescriptor(), ejbd.getName() })); } finally { try { if (deploymentEntry != null) deploymentEntry.close(); } catch (Exception x) { } } } } else { na++; result.notApplicable( smh.getLocalString( getClass().getName() + ".notApplicable1", "There is no java EJB alternative deployment descriptor in [ {0} ]", new Object[] {ejbd.getName()})); } } if (oneFailed) { result.setStatus(Result.FAILED); } else if (na == descriptor.getEjbBundleDescriptors().size()) { result.setStatus(Result.NOT_APPLICABLE); } else { result.setStatus(Result.PASSED); } } else { result.notApplicable( smh.getLocalString( getClass().getName() + ".notApplicable", "There are no EJB components in application [ {0} ]", new Object[] {descriptor.getName()})); } return result; }
/** Starts main program with the specified arguments. */ public synchronized boolean run(String args[]) { ok = true; if (!parseArgs(args)) { return false; } try { if (cflag || uflag) { if (fname != null) { // The name of the zip file as it would appear as its own // zip file entry. We use this to make sure that we don't // add the zip file to itself. zname = fname.replace(File.separatorChar, '/'); if (zname.startsWith("./")) { zname = zname.substring(2); } } } if (cflag) { Manifest manifest = null; InputStream in = null; if (!Mflag) { if (mname != null) { in = new FileInputStream(mname); manifest = new Manifest(new BufferedInputStream(in)); } else { manifest = new Manifest(); } addVersion(manifest); addCreatedBy(manifest); if (isAmbiguousMainClass(manifest)) { if (in != null) { in.close(); } return false; } if (ename != null) { addMainClass(manifest, ename); } if (pname != null) { if (!addProfileName(manifest, pname)) { if (in != null) { in.close(); } return false; } } } OutputStream out; if (fname != null) { out = new FileOutputStream(fname); } else { out = new FileOutputStream(FileDescriptor.out); if (vflag) { // Disable verbose output so that it does not appear // on stdout along with file data // error("Warning: -v option ignored"); vflag = false; } } expand(null, files, false); create(new BufferedOutputStream(out, 4096), manifest); if (in != null) { in.close(); } out.close(); } else if (uflag) { File inputFile = null, tmpFile = null; FileInputStream in; FileOutputStream out; if (fname != null) { inputFile = new File(fname); tmpFile = createTempFileInSameDirectoryAs(inputFile); in = new FileInputStream(inputFile); out = new FileOutputStream(tmpFile); } else { in = new FileInputStream(FileDescriptor.in); out = new FileOutputStream(FileDescriptor.out); vflag = false; } InputStream manifest = (!Mflag && (mname != null)) ? (new FileInputStream(mname)) : null; expand(null, files, true); boolean updateOk = update(in, new BufferedOutputStream(out), manifest, null); if (ok) { ok = updateOk; } in.close(); out.close(); if (manifest != null) { manifest.close(); } if (ok && fname != null) { // on Win32, we need this delete inputFile.delete(); if (!tmpFile.renameTo(inputFile)) { tmpFile.delete(); throw new IOException(getMsg("error.write.file")); } tmpFile.delete(); } } else if (tflag) { replaceFSC(files); if (fname != null) { list(fname, files); } else { InputStream in = new FileInputStream(FileDescriptor.in); try { list(new BufferedInputStream(in), files); } finally { in.close(); } } } else if (xflag) { replaceFSC(files); if (fname != null && files != null) { extract(fname, files); } else { InputStream in = (fname == null) ? new FileInputStream(FileDescriptor.in) : new FileInputStream(fname); try { extract(new BufferedInputStream(in), files); } finally { in.close(); } } } else if (iflag) { genIndex(rootjar, files); } } catch (IOException e) { fatalError(e); ok = false; } catch (Error ee) { ee.printStackTrace(); ok = false; } catch (Throwable t) { t.printStackTrace(); ok = false; } out.flush(); err.flush(); return ok; }
/** * Copies all bytes from the input stream to the output stream. Does not close or flush either * stream. * * @param from the input stream to read from * @param to the output stream to write to * @throws IOException if an I/O error occurs */ private void copy(InputStream from, OutputStream to) throws IOException { int n; while ((n = from.read(copyBuf)) != -1) to.write(copyBuf, 0, n); }