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; }
/** Generates the transitive closure of the Class-Path attribute for the specified jar file. */ List<String> getJarPath(String jar) throws IOException { List<String> files = new ArrayList<String>(); files.add(jar); jarPaths.add(jar); // take out the current path String path = jar.substring(0, Math.max(0, jar.lastIndexOf('/') + 1)); // class path attribute will give us jar file name with // '/' as separators, so we need to change them to the // appropriate one before we open the jar file. JarFile rf = new JarFile(jar.replace('/', File.separatorChar)); if (rf != null) { Manifest man = rf.getManifest(); if (man != null) { Attributes attr = man.getMainAttributes(); if (attr != null) { String value = attr.getValue(Attributes.Name.CLASS_PATH); if (value != null) { StringTokenizer st = new StringTokenizer(value); while (st.hasMoreTokens()) { String ajar = st.nextToken(); if (!ajar.endsWith("/")) { // it is a jar file ajar = path.concat(ajar); /* check on cyclic dependency */ if (!jarPaths.contains(ajar)) { files.addAll(getJarPath(ajar)); } } } } } } } rf.close(); return files; }