Java Code Examples for java.util.jar.JarInputStream#read()

The following examples show how to use java.util.jar.JarInputStream#read() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
    if (in.getManifest() != null) {
        ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(me);
        in.getManifest().write(out);
        out.closeEntry();
    }
    byte[] buffer = new byte[1 << 14];
    for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
        out.putNextEntry(je);
        for (int nr; 0 < (nr = in.read(buffer)); ) {
            out.write(buffer, 0, nr);
        }
    }
    in.close();
    markJarFile(out);  // add PACK200 comment
}
 
Example 2
Source File: Utils.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
    if (in.getManifest() != null) {
        ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(me);
        in.getManifest().write(out);
        out.closeEntry();
    }
    byte[] buffer = new byte[1 << 14];
    for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
        out.putNextEntry(je);
        for (int nr; 0 < (nr = in.read(buffer)); ) {
            out.write(buffer, 0, nr);
        }
    }
    in.close();
    markJarFile(out);  // add PACK200 comment
}
 
Example 3
Source File: Utils.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
    if (in.getManifest() != null) {
        ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(me);
        in.getManifest().write(out);
        out.closeEntry();
    }
    byte[] buffer = new byte[1 << 14];
    for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
        out.putNextEntry(je);
        for (int nr; 0 < (nr = in.read(buffer)); ) {
            out.write(buffer, 0, nr);
        }
    }
    in.close();
    markJarFile(out);  // add PACK200 comment
}
 
Example 4
Source File: Utils.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
    if (in.getManifest() != null) {
        ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(me);
        in.getManifest().write(out);
        out.closeEntry();
    }
    byte[] buffer = new byte[1 << 14];
    for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
        out.putNextEntry(je);
        for (int nr; 0 < (nr = in.read(buffer)); ) {
            out.write(buffer, 0, nr);
        }
    }
    in.close();
    markJarFile(out);  // add PACK200 comment
}
 
Example 5
Source File: Utils.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
    if (in.getManifest() != null) {
        ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(me);
        in.getManifest().write(out);
        out.closeEntry();
    }
    byte[] buffer = new byte[1 << 14];
    for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
        out.putNextEntry(je);
        for (int nr; 0 < (nr = in.read(buffer)); ) {
            out.write(buffer, 0, nr);
        }
    }
    in.close();
    markJarFile(out);  // add PACK200 comment
}
 
Example 6
Source File: JarModifier.java    From JReFrameworker with MIT License 6 votes vote down vote up
public byte[] extractEntry(String entry) throws IOException {
	JarInputStream zin = new JarInputStream(new BufferedInputStream(new FileInputStream(jarFile)));
	JarEntry currentEntry = null;
	while ((currentEntry = zin.getNextJarEntry()) != null) {
		if (currentEntry.getName().equals(entry)) {
			// currentEntry.getSize() may not be accurate, so read bytes into a stream first
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] buf = new byte[4096];
			while (true) {
				int n = zin.read(buf);
				if (n < 0){
					break;
				}
				baos.write(buf, 0, n);
			}
			zin.close();
			return baos.toByteArray();
		}
	}
	zin.close();
	return null;
}
 
Example 7
Source File: Utils.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
    if (in.getManifest() != null) {
        ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(me);
        in.getManifest().write(out);
        out.closeEntry();
    }
    byte[] buffer = new byte[1 << 14];
    for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
        out.putNextEntry(je);
        for (int nr; 0 < (nr = in.read(buffer)); ) {
            out.write(buffer, 0, nr);
        }
    }
    in.close();
    markJarFile(out);  // add PACK200 comment
}
 
Example 8
Source File: Utils.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
    if (in.getManifest() != null) {
        ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(me);
        in.getManifest().write(out);
        out.closeEntry();
    }
    byte[] buffer = new byte[1 << 14];
    for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
        out.putNextEntry(je);
        for (int nr; 0 < (nr = in.read(buffer)); ) {
            out.write(buffer, 0, nr);
        }
    }
    in.close();
    markJarFile(out);  // add PACK200 comment
}
 
Example 9
Source File: Utils.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
    if (in.getManifest() != null) {
        ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(me);
        in.getManifest().write(out);
        out.closeEntry();
    }
    byte[] buffer = new byte[1 << 14];
    for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
        out.putNextEntry(je);
        for (int nr; 0 < (nr = in.read(buffer)); ) {
            out.write(buffer, 0, nr);
        }
    }
    in.close();
    markJarFile(out);  // add PACK200 comment
}
 
Example 10
Source File: MergeAndShadeTransformer.java    From metron with Apache License 2.0 6 votes vote down vote up
/**
 * Merges two jars.  The first jar will get merged into the output jar.
 * A running set of jar entries is kept so that duplicates are skipped.
 * This has the side-effect that the first instance of a given entry will be added
 * and all subsequent entries are skipped.
 *
 * @param jin The input jar
 * @param jout The output jar
 * @param entries The set of existing entries.  Note that this set will be mutated as part of this call.
 * @return The set of entries.
 * @throws IOException
 */
private Set<String> copy(JarInputStream jin, JarOutputStream jout, Set<String> entries) throws IOException {
  byte[] buffer = new byte[1024];
  for(JarEntry entry = jin.getNextJarEntry(); entry != null; entry = jin.getNextJarEntry()) {
    if(entries.contains(entry.getName())) {
      continue;
    }
    LOG.debug("Merging jar entry {}", entry.getName());
    entries.add(entry.getName());
    jout.putNextEntry(entry);
    int len = 0;
    while( (len = jin.read(buffer)) > 0 ) {
      jout.write(buffer, 0, len);
    }
  }
  return entries;
}
 
Example 11
Source File: Utils.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
    if (in.getManifest() != null) {
        ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(me);
        in.getManifest().write(out);
        out.closeEntry();
    }
    byte[] buffer = new byte[1 << 14];
    for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
        out.putNextEntry(je);
        for (int nr; 0 < (nr = in.read(buffer)); ) {
            out.write(buffer, 0, nr);
        }
    }
    in.close();
    markJarFile(out);  // add PACK200 comment
}
 
Example 12
Source File: Utils.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
    if (in.getManifest() != null) {
        ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(me);
        in.getManifest().write(out);
        out.closeEntry();
    }
    byte[] buffer = new byte[1 << 14];
    for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
        out.putNextEntry(je);
        for (int nr; 0 < (nr = in.read(buffer)); ) {
            out.write(buffer, 0, nr);
        }
    }
    in.close();
    markJarFile(out);  // add PACK200 comment
}
 
Example 13
Source File: Utils.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException {
    if (in.getManifest() != null) {
        ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME);
        out.putNextEntry(me);
        in.getManifest().write(out);
        out.closeEntry();
    }
    byte[] buffer = new byte[1 << 14];
    for (JarEntry je; (je = in.getNextJarEntry()) != null; ) {
        out.putNextEntry(je);
        for (int nr; 0 < (nr = in.read(buffer)); ) {
            out.write(buffer, 0, nr);
        }
    }
    in.close();
    markJarFile(out);  // add PACK200 comment
}
 
Example 14
Source File: MarshallingTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
/**
 * In this case we are dealing with facts which are not on the systems classpath.
 */
@Test
public void testSerializabilityWithJarFacts() throws Exception {
    MapBackedClassLoader loader = new MapBackedClassLoader( this.getClass().getClassLoader() );

    JarInputStream jis = new JarInputStream( this.getClass().getResourceAsStream( "/billasurf.jar" ) );

    JarEntry entry = null;
    byte[] buf = new byte[1024];
    int len = 0;
    while ( (entry = jis.getNextJarEntry()) != null ) {
        if ( !entry.isDirectory() ) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            while ( (len = jis.read( buf )) >= 0 ) {
                out.write( buf,
                           0,
                           len );
            }
            loader.addResource( entry.getName(),
                                out.toByteArray() );
        }
    }

    String drl = "package foo.bar \n" +
                 "import com.billasurf.Board\n" +
                 "rule 'MyGoodRule' \n dialect 'mvel' \n when " +
                 "   Board() " +
                 "then \n" +
                 " System.err.println(42); \n" +
                 "end\n";


    KnowledgeBuilderConfiguration kbuilderConf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, loader);

    Collection<KiePackage>  kpkgs = loadKnowledgePackagesFromString(kbuilderConf, drl);

    kpkgs = SerializationHelper.serializeObject( kpkgs, loader );

}
 
Example 15
Source File: AlbianClassLoader.java    From Albianj2 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private byte[] getBytes(JarInputStream jis) throws IOException {
    int len = 0;
    byte[] bytes = new byte[8192];
    ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
    while ((len = jis.read(bytes, 0, bytes.length)) != -1) {
        baos.write(bytes, 0, len);
    }
    return baos.toByteArray();
}
 
Example 16
Source File: JarHelper.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Given an InputStream on a jar file, unjars the contents into the given
 * directory.
 */
public void unjar(InputStream in, File destDir) throws IOException {
	BufferedOutputStream dest = null;
	JarInputStream jis = new JarInputStream(in);
	JarEntry entry;
	while ((entry = jis.getNextJarEntry()) != null) {
		if (entry.isDirectory()) {
			File dir = new File(destDir, entry.getName());
			dir.mkdir();
			if (entry.getTime() != -1) {
				dir.setLastModified(entry.getTime());
			}
			continue;
		}
		int count;
		byte[] data = new byte[BUFFER_SIZE];
		File destFile = new File(destDir, entry.getName());
		if (mVerbose) {
			System.out.println("unjarring " + destFile +
				" from " + entry.getName());
		}
		FileOutputStream fos = new FileOutputStream(destFile);
		dest = new BufferedOutputStream(fos, BUFFER_SIZE);
		try {
			while ((count = jis.read(data, 0, BUFFER_SIZE)) != -1) {
				dest.write(data, 0, count);
			}
			dest.flush();
		} finally {
			dest.close();
		}
		if (entry.getTime() != -1) {
			destFile.setLastModified(entry.getTime());
		}
	}
	jis.close();
}
 
Example 17
Source File: JClassLoader.java    From rscplus with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Fetches the game jar and loads and patches the classes
 *
 * @param jarURL The URL of the jar to be loaded and patched
 * @return If no exceptions occurred
 */
public boolean fetch(String jarURL) {
  Logger.Info("Fetching Jar: " + jarURL);

  try {
    JarInputStream in = new JarInputStream(Settings.getResourceAsStream(jarURL));
    Launcher.getInstance().setProgress(1, 1);

    JarEntry entry;
    while ((entry = in.getNextJarEntry()) != null) {
      // Check if file is needed
      String name = entry.getName();

      // Read class to byte array
      ByteArrayOutputStream bOut = new ByteArrayOutputStream();
      byte[] data = new byte[1024];
      int readSize;
      while ((readSize = in.read(data, 0, data.length)) != -1) bOut.write(data, 0, readSize);
      byte[] classData = bOut.toByteArray();
      bOut.close();

      Logger.Info("Loading file: " + name);
      Launcher.getInstance().setStatus("Loading " + name + "...");

      if (name.endsWith(".class")) {
        name = name.substring(0, name.indexOf(".class"));
        classData = JClassPatcher.getInstance().patch(classData);
        m_classData.put(name, classData);
      }
    }
    in.close();
  } catch (Exception e) {
    e.printStackTrace();
    return false;
  }
  return true;
}
 
Example 18
Source File: NbModuleSuite.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static File rewrite(File jar, String[] mavenCP, String classpath) throws IOException { // #190992
    String[] classpathEntries = tokenizePath(classpath);
    StringBuilder classPathHeader = new StringBuilder();
    for (String artifact : mavenCP) {
        String[] grpArtVers = artifact.split(":");
        String partOfPath = File.separatorChar + grpArtVers[0].replace('.', File.separatorChar) + File.separatorChar + grpArtVers[1] + File.separatorChar + grpArtVers[2] + File.separatorChar + grpArtVers[1] + '-' + grpArtVers[2];
        File dep = null;
        for (String classpathEntry : classpathEntries) {
            if (classpathEntry.endsWith(".jar") && classpathEntry.contains(partOfPath)) {
                dep = new File(classpathEntry);
                break;
            }
        }
        if (dep == null) {
            throw new IOException("no match for " + artifact + " found in " + classpath);
        }
        File depCopy = File.createTempFile(artifact.replace(':', '-') + '-', ".jar");
        depCopy.deleteOnExit();
        NbTestCase.copytree(dep, depCopy);
        if (classPathHeader.length() > 0) {
            classPathHeader.append(' ');
        }
        classPathHeader.append(depCopy.getName());
    }
    String n = jar.getName();
    int dot = n.lastIndexOf('.');
    File jarCopy = File.createTempFile(n.substring(0, dot) + '-', n.substring(dot));
    jarCopy.deleteOnExit();
    InputStream is = new FileInputStream(jar);
    try {
        OutputStream os = new FileOutputStream(jarCopy);
        try {
            JarInputStream jis = new JarInputStream(is);
            Manifest mani = new Manifest(jis.getManifest());
            mani.getMainAttributes().putValue("Class-Path", classPathHeader.toString());
            JarOutputStream jos = new JarOutputStream(os, mani);
            JarEntry entry;
            while ((entry = jis.getNextJarEntry()) != null) {
                if (entry.getName().matches("META-INF/.+[.]SF")) {
                    throw new IOException("cannot handle signed JARs");
                }
                jos.putNextEntry(entry);
                byte[] buf = new byte[4092];
                for (;;) {
                    int more = jis.read(buf, 0, buf.length);
                    if (more == -1) {
                        break;
                    }
                    jos.write(buf, 0, more);
                }
            }
            jis.close();
            jos.close();
        } finally {
            os.close();
        }
    } finally {
        is.close();
    }
    return jarCopy;
}
 
Example 19
Source File: ZipUtils.java    From isu with GNU General Public License v3.0 4 votes vote down vote up
public static String generateUnhide(Context context, File output) {
    File temp = new File(context.getCacheDir(), "temp.apk");
    String pkg = "";
    Log.d(TAG, "try start ");
    try {
        JarInputStream source = new JarInputStream(new FileInputStream(new File(Tools.runCommand("pm path " + ISU_APK + "| head -n1 | cut -d: -f2", Tools.SuBinary(), context))));
        JarOutputStream dest = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(temp)));
        JarEntry entry;
        int size;
        byte buffer[] = new byte[4096];
        while ((entry = source.getNextJarEntry()) != null) {
            dest.putNextEntry(new JarEntry(entry.getName()));
            if (TextUtils.equals(entry.getName(), ANDROID_MANIFEST)) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                while ((size = source.read(buffer)) > 0) {
                    baos.write(buffer, 0, size);
                }

                byte xml[] = baos.toByteArray();
                boolean need_zero = false;
                int offset = Tools.FindOffSet(xml, COM_PKG_NAME);

                if (offset < 0) {
                    Log.d(TAG, "offset < 0 try appModZeros");
                    byte[] COM_PKG_NAME_ZERO = (Tools.appStringAddZeros(app_name)).getBytes();
                    offset = Tools.FindOffSet(xml, COM_PKG_NAME_ZERO);
                    need_zero = true;
                }
                if (offset < 0) {
                    Log.d(TAG, "offset < 0 again return");
                    return "";
                }

                Log.d(TAG, "offset " + offset + " leg " + COM_PKG_NAME.length);
                // Patch binary XML with new package name
                if (Tools.appInstaled(context)) {
                    if (need_zero)
                        pkg = Tools.appStringAddZeros(Tools.readString("hide_app_name", null, context));
                    else
                        pkg = Tools.readString("hide_app_name", null, context);
                } else
                    pkg = Tools.appStringMod(app_name, need_zero);
                System.arraycopy(pkg.getBytes(), 0, xml, offset, pkg.length());
                dest.write(xml);
            } else {
                while ((size = source.read(buffer)) > 0) {
                    dest.write(buffer, 0, size);
                }
            }
        }
        source.close();
        dest.close();
        signZip(context, temp, output, false);
        temp.delete();
    } catch (IOException e) {
        e.printStackTrace();
        return pkg;
    }
    Log.d(TAG, "pkg " + pkg.replace("\0", ""));
    return pkg;
}
 
Example 20
Source File: VulasConfiguration.java    From steady with Apache License 2.0 2 votes vote down vote up
/**
 * Reads the content of the current {@link JarEntry} from the given {@link JarInputStream} into a byte array.
 * @param _jis
 * @return
 * @throws IOException
 */
private byte[] readContent(JarInputStream _jis) throws IOException {
	byte[] bytes = new byte[1024];
	while(_jis.read(bytes, 0, 1024)!=-1) {;} //read()
	return bytes;
}