Example #1
0
  /**
   * Takes a packed-stream InputStream, and writes to a JarOutputStream. Internally the entire
   * buffer must be read, it may be more efficient to read the packed-stream to a file and pass the
   * File object, in the alternate method described below.
   *
   * <p>Closes its input but not its output. (The output can accumulate more elements.)
   *
   * @param in an InputStream.
   * @param out a JarOutputStream.
   * @exception IOException if an error is encountered.
   */
  public void unpack(InputStream in0, JarOutputStream out) throws IOException {
    assert (Utils.currentInstance.get() == null);
    TimeZone tz = (_props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE)) ? null : TimeZone.getDefault();

    try {
      Utils.currentInstance.set(this);
      if (tz != null) TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
      final int verbose = _props.getInteger(Utils.DEBUG_VERBOSE);
      BufferedInputStream in = new BufferedInputStream(in0);
      if (Utils.isJarMagic(Utils.readMagic(in))) {
        if (verbose > 0) Utils.log.info("Copying unpacked JAR file...");
        Utils.copyJarFile(new JarInputStream(in), out);
      } else if (_props.getBoolean(Utils.DEBUG_DISABLE_NATIVE)) {
        (new DoUnpack()).run(in, out);
        in.close();
        Utils.markJarFile(out);
      } else {
        (new NativeUnpack(this)).run(in, out);
        in.close();
        Utils.markJarFile(out);
      }
    } finally {
      _nunp = null;
      Utils.currentInstance.set(null);
      if (tz != null) TimeZone.setDefault(tz);
    }
  }
Example #2
0
    public void run(BufferedInputStream in, JarOutputStream out) throws IOException {
      if (verbose > 0) {
        _props.list(System.out);
      }
      for (int seg = 1; ; seg++) {
        unpackSegment(in, out);

        // Try to get another segment.
        if (!Utils.isPackMagic(Utils.readMagic(in))) break;
        if (verbose > 0) Utils.log.info("Finished segment #" + seg);
      }
    }
Example #3
0
 public String toString() {
   return Utils.getVersionString();
 }
Example #4
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
    }