/** * 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); } }
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); } }
public static byte [] unzipBytes(byte [] bs, Compression cmp) { InputStream is = null; int off = 0; try { switch(cmp) { case NONE: // No compression return bs; case ZIP: { ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(bs)); ZipEntry ze = zis.getNextEntry(); // Get the *FIRST* entry // There is at least one entry in zip file and it is not a directory. if( ze != null && !ze.isDirectory() ) { is = zis; break; } zis.close(); return bs; // Don't crash, ignore file if cannot unzip } case GZIP: is = new GZIPInputStream(new ByteArrayInputStream(bs)); break; default: assert false:"cmp = " + cmp; } // If reading from a compressed stream, estimate we can read 2x uncompressed assert( is != null ):"is is NULL, cmp = " + cmp; bs = new byte[bs.length * 2]; // Now read from the (possibly compressed) stream while( off < bs.length ) { int len = is.read(bs, off, bs.length - off); if( len < 0 ) break; off += len; if( off == bs.length ) { // Dataset is uncompressing alot! Need more space... if( bs.length >= ValueArray.CHUNK_SZ ) break; // Already got enough bs = Arrays.copyOf(bs, bs.length * 2); } } } catch( IOException ioe ) { // Stop at any io error Log.err(ioe); } finally { Utils.close(is); } return bs; }
public String toString() { return Utils.getVersionString(); }
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 }