Ejemplo n.º 1
0
  protected void generateJarFile(File jarFile, boolean standAlone, File dir) throws IOException {
    Verbose.log("Generating jar file ... " + jarFile);
    FileOutputStream out = new FileOutputStream(jarFile);
    InputStream manifestIo = getManifest();
    Manifest manifest = new Manifest(manifestIo);
    JarOutputStream jar = new JarOutputStream(out, manifest);

    int rootLen = dir.getAbsolutePath().length();

    int len = "java".length();
    for (File javaFile : javaFiles) {
      String fileName = javaFile.getPath();
      String classFile = fileName.substring(0, fileName.length() - len) + "class";
      String className = classFile.substring(rootLen + 1);
      addFileToJar(className, classFile, jar);

      String javaName = fileName.substring(rootLen + 1);
      addFileToJar(javaName, fileName, jar);
    }

    if (standAlone) {
      Verbose.log("Adding runtime classes to the jar");
      addRuntimeClasses(jar);
    }

    jar.close();
    out.close();
    Verbose.log("Generated jar file " + jarFile);
  }
Ejemplo n.º 2
0
  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();
  }
Ejemplo n.º 3
0
 /** Create a jar file containing one or more entries. */
 File createJar(File jar, String... entries) throws IOException {
   OutputStream out = new FileOutputStream(jar);
   try {
     JarOutputStream jos = new JarOutputStream(out);
     for (String e : entries) {
       jos.putNextEntry(new JarEntry(getPathForZipEntry(e)));
       jos.write(getBodyForEntry(e).getBytes());
     }
     jos.close();
   } finally {
     out.close();
   }
   return jar;
 }
Ejemplo n.º 4
0
  protected void addFileToJar(String className, String classFile, JarOutputStream jar)
      throws IOException {
    className = className.replace('\\', '/');
    FileInputStream fio = new FileInputStream(classFile);
    jar.putNextEntry(new JarEntry(className));

    int cb;
    byte[] buffer = new byte[8192];
    while ((cb = fio.read(buffer)) != -1) {
      jar.write(buffer, 0, cb);
    }

    jar.closeEntry();
    fio.close();
  }
Ejemplo n.º 5
0
 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();
   }
 }
Ejemplo n.º 6
0
  /**
   * Writes the index file out to <code>jos</code>. <code>oldEntries</code> gives the names of the
   * files that were removed, <code>movedMap</code> maps from the new name to the old name.
   */
  private static void createIndex(JarOutputStream jos, List oldEntries, Map movedMap)
      throws IOException {
    StringWriter writer = new StringWriter();

    writer.write(VERSION_HEADER);
    writer.write("\r\n");

    // Write out entries that have been removed
    for (int counter = 0; counter < oldEntries.size(); counter++) {
      String name = (String) oldEntries.get(counter);

      writer.write(REMOVE_COMMAND);
      writer.write(" ");
      writeEscapedString(writer, name);
      writer.write("\r\n");
    }

    // And those that have moved
    Iterator names = movedMap.keySet().iterator();

    if (names != null) {
      while (names.hasNext()) {
        String newName = (String) names.next();
        String oldName = (String) movedMap.get(newName);

        writer.write(MOVE_COMMAND);
        writer.write(" ");
        writeEscapedString(writer, oldName);
        writer.write(" ");
        writeEscapedString(writer, newName);
        writer.write("\r\n");
      }
    }

    JarEntry je = new JarEntry(INDEX_NAME);
    byte[] bytes = writer.toString().getBytes("UTF-8");

    writer.close();
    jos.putNextEntry(je);
    jos.write(bytes, 0, bytes.length);
  }
Ejemplo n.º 7
0
  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());
        }
      }
    }
  }
Ejemplo n.º 8
0
  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
      }
    }
  }
Ejemplo n.º 9
0
  /** Creates a patch from the two passed in files, writing the result to <code>os</code>. */
  public static void createPatch(String oldPath, String newPath, OutputStream os, boolean minimal)
      throws IOException {
    JarFile2 oldJar = new JarFile2(oldPath);
    JarFile2 newJar = new JarFile2(newPath);

    try {
      Iterator entries;
      HashMap moved = new HashMap();
      HashSet visited = new HashSet();
      HashSet implicit = new HashSet();
      HashSet moveSrc = new HashSet();
      HashSet newEntries = new HashSet();

      // FIRST PASS
      // Go through the entries in new jar and
      // determine which files are candidates for implicit moves
      // ( files that has the same filename and same content in old.jar
      // and new.jar )
      // and for files that cannot be implicitly moved, we will either
      // find out whether it is moved or new (modified)
      entries = newJar.getJarEntries();
      if (entries != null) {
        while (entries.hasNext()) {
          JarEntry newEntry = (JarEntry) entries.next();
          String newname = newEntry.getName();

          // Return best match of contents, will return a name match if possible
          String oldname = oldJar.getBestMatch(newJar, newEntry);
          if (oldname == null) {
            // New or modified entry
            if (_debug) {
              System.out.println("NEW: " + newname);
            }
            newEntries.add(newname);
          } else {
            // Content already exist - need to do a move

            // Should do implicit move? Yes, if names are the same, and
            // no move command already exist from oldJar
            if (oldname.equals(newname) && !moveSrc.contains(oldname)) {
              if (_debug) {
                System.out.println(newname + " added to implicit set!");
              }
              implicit.add(newname);
            } else {
              // The 1.0.1/1.0 JarDiffPatcher cannot handle
              // multiple MOVE command with same src.
              // The work around here is if we are going to generate
              // a MOVE command with duplicate src, we will
              // instead add the target as a new file.  This way
              // the jardiff can be applied by 1.0.1/1.0
              // JarDiffPatcher also.
              if (!minimal && (implicit.contains(oldname) || moveSrc.contains(oldname))) {

                // generate non-minimal jardiff
                // for backward compatibility

                if (_debug) {

                  System.out.println("NEW: " + newname);
                }
                newEntries.add(newname);
              } else {
                // Use newname as key, since they are unique
                if (_debug) {
                  System.err.println("moved.put " + newname + " " + oldname);
                }
                moved.put(newname, oldname);
                moveSrc.add(oldname);
              }
              // Check if this disables an implicit 'move <oldname> <oldname>'
              if (implicit.contains(oldname) && minimal) {

                if (_debug) {
                  System.err.println("implicit.remove " + oldname);

                  System.err.println("moved.put " + oldname + " " + oldname);
                }
                implicit.remove(oldname);
                moved.put(oldname, oldname);
                moveSrc.add(oldname);
              }
            }
          }
        }
      } // if (entries != null)

      // SECOND PASS: <deleted files> = <oldjarnames> - <implicitmoves> -
      // <source of move commands> - <new or modified entries>
      ArrayList deleted = new ArrayList();
      entries = oldJar.getJarEntries();
      if (entries != null) {
        while (entries.hasNext()) {
          JarEntry oldEntry = (JarEntry) entries.next();
          String oldName = oldEntry.getName();
          if (!implicit.contains(oldName)
              && !moveSrc.contains(oldName)
              && !newEntries.contains(oldName)) {
            if (_debug) {
              System.err.println("deleted.add " + oldName);
            }
            deleted.add(oldName);
          }
        }
      }

      // DEBUG
      if (_debug) {
        // DEBUG:  print out moved map
        entries = moved.keySet().iterator();
        if (entries != null) {
          System.out.println("MOVED MAP!!!");
          while (entries.hasNext()) {
            String newName = (String) entries.next();
            String oldName = (String) moved.get(newName);
            System.out.println("key is " + newName + " value is " + oldName);
          }
        }

        // DEBUG:  print out IMOVE map
        entries = implicit.iterator();
        if (entries != null) {
          System.out.println("IMOVE MAP!!!");
          while (entries.hasNext()) {
            String newName = (String) entries.next();
            System.out.println("key is " + newName);
          }
        }
      }

      JarOutputStream jos = new JarOutputStream(os);

      // Write out all the MOVEs and REMOVEs
      createIndex(jos, deleted, moved);

      // Put in New and Modified entries
      entries = newEntries.iterator();
      if (entries != null) {

        while (entries.hasNext()) {
          String newName = (String) entries.next();
          if (_debug) {
            System.out.println("New File: " + newName);
          }
          writeEntry(jos, newJar.getEntryByName(newName), newJar);
        }
      }

      jos.finish();
      jos.close();

    } catch (IOException ioE) {
      throw ioE;
    } finally {
      try {
        oldJar.getJarFile().close();
      } catch (IOException e1) {
        // ignore
      }
      try {
        newJar.getJarFile().close();
      } catch (IOException e1) {
        // ignore
      }
    } // finally
  }
Ejemplo n.º 10
0
    private void unpackSegment(InputStream in, JarOutputStream out) throws IOException {
      _props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS, "0");
      // Process the output directory or jar output.
      new PackageReader(pkg, in).read();

      if (_props.getBoolean("unpack.strip.debug")) pkg.stripAttributeKind("Debug");
      if (_props.getBoolean("unpack.strip.compile")) pkg.stripAttributeKind("Compile");
      _props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS, "50");
      pkg.ensureAllClassFiles();
      // Now write out the files.
      HashSet classesToWrite = new HashSet(pkg.getClasses());
      for (Iterator i = pkg.getFiles().iterator(); i.hasNext(); ) {
        Package.File file = (Package.File) i.next();
        String name = file.nameString;
        JarEntry je = new JarEntry(Utils.getJarEntryName(name));
        boolean deflate;

        deflate =
            (keepDeflateHint)
                ? (((file.options & Constants.FO_DEFLATE_HINT) != 0)
                    || ((pkg.default_options & Constants.AO_DEFLATE_HINT) != 0))
                : deflateHint;

        boolean needCRC = !deflate; // STORE mode requires CRC

        if (needCRC) crc.reset();
        bufOut.reset();
        if (file.isClassStub()) {
          Package.Class cls = file.getStubClass();
          assert (cls != null);
          new ClassWriter(cls, needCRC ? crcOut : bufOut).write();
          classesToWrite.remove(cls); // for an error check
        } else {
          // collect data & maybe CRC
          file.writeTo(needCRC ? crcOut : bufOut);
        }
        je.setMethod(deflate ? JarEntry.DEFLATED : JarEntry.STORED);
        if (needCRC) {
          if (verbose > 0)
            Utils.log.info("stored size=" + bufOut.size() + " and crc=" + crc.getValue());

          je.setMethod(JarEntry.STORED);
          je.setSize(bufOut.size());
          je.setCrc(crc.getValue());
        }
        if (keepModtime) {
          je.setTime(file.modtime);
          // Convert back to milliseconds
          je.setTime((long) file.modtime * 1000);
        } else {
          je.setTime((long) modtime * 1000);
        }
        out.putNextEntry(je);
        bufOut.writeTo(out);
        out.closeEntry();
        if (verbose > 0) Utils.log.info("Writing " + Utils.zeString((ZipEntry) je));
      }
      assert (classesToWrite.isEmpty());
      _props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS, "100");
      pkg.reset(); // reset for the next segment, if any
    }