1 /* 2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.java.util.jar.pack; 27 28 import java.beans.PropertyChangeListener; 29 import java.io.BufferedInputStream; 30 import java.io.ByteArrayOutputStream; 31 import java.io.File; 32 import java.io.FileInputStream; 33 import java.io.IOException; 34 import java.io.InputStream; 35 import java.io.OutputStream; 36 import java.util.HashSet; 37 import java.util.Set; 38 import java.util.SortedMap; 39 import java.util.TimeZone; 40 import java.util.jar.JarEntry; 41 import java.util.jar.JarInputStream; 42 import java.util.jar.JarOutputStream; 43 import java.util.jar.Pack200; 44 import java.util.zip.CRC32; 45 import java.util.zip.CheckedOutputStream; 46 import java.util.zip.ZipEntry; 47 48 /* 49 * Implementation of the Pack provider. 50 * </pre></blockquote> 51 * @author John Rose 52 * @author Kumar Srinivasan 53 */ 54 55 56 public class UnpackerImpl extends TLGlobals implements Pack200.Unpacker { 57 58 59 /** 60 * Register a listener for changes to options. 61 * @param listener An object to be invoked when a property is changed. 62 */ 63 public void addPropertyChangeListener(PropertyChangeListener listener) { 64 props.addListener(listener); 65 } 66 67 68 /** 69 * Remove a listener for the PropertyChange event. 70 * @param listener The PropertyChange listener to be removed. 71 */ 72 public void removePropertyChangeListener(PropertyChangeListener listener) { 73 props.removeListener(listener); 74 } 75 76 public UnpackerImpl() {} 77 78 79 80 /** 81 * Get the set of options for the pack and unpack engines. 82 * @return A sorted association of option key strings to option values. 83 */ 84 public SortedMap<String, String> properties() { 85 return props; 86 } 87 88 // Back-pointer to NativeUnpacker, when active. 89 Object _nunp; 90 91 92 public String toString() { 93 return Utils.getVersionString(); 94 } 95 96 //Driver routines 97 98 // The unpack worker... 99 /** 100 * Takes a packed-stream InputStream, and writes to a JarOutputStream. Internally 101 * the entire buffer must be read, it may be more efficient to read the packed-stream 102 * to a file and pass the File object, in the alternate method described below. 103 * <p> 104 * Closes its input but not its output. (The output can accumulate more elements.) 105 * @param in an InputStream. 106 * @param out a JarOutputStream. 107 * @exception IOException if an error is encountered. 108 */ 109 public synchronized void unpack(InputStream in, JarOutputStream out) throws IOException { 110 if (in == null) { 111 throw new NullPointerException("null input"); 112 } 113 if (out == null) { 114 throw new NullPointerException("null output"); 115 } 116 assert(Utils.currentInstance.get() == null); 117 TimeZone tz = (props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE)) 118 ? null 119 : TimeZone.getDefault(); 120 121 try { 122 Utils.currentInstance.set(this); 123 if (tz != null) TimeZone.setDefault(TimeZone.getTimeZone("UTC")); 124 final int verbose = props.getInteger(Utils.DEBUG_VERBOSE); 125 BufferedInputStream in0 = new BufferedInputStream(in); 126 if (Utils.isJarMagic(Utils.readMagic(in0))) { 127 if (verbose > 0) 128 Utils.log.info("Copying unpacked JAR file..."); 129 Utils.copyJarFile(new JarInputStream(in0), out); 130 } else if (props.getBoolean(Utils.DEBUG_DISABLE_NATIVE)) { 131 (new DoUnpack()).run(in0, out); 132 in0.close(); 133 Utils.markJarFile(out); 134 } else { 135 try { 136 (new NativeUnpack(this)).run(in0, out); 137 } catch (UnsatisfiedLinkError | NoClassDefFoundError ex) { 138 // failover to java implementation 139 (new DoUnpack()).run(in0, out); 140 } 141 in0.close(); 142 Utils.markJarFile(out); 143 } 144 } finally { 145 _nunp = null; 146 Utils.currentInstance.set(null); 147 if (tz != null) TimeZone.setDefault(tz); 148 } 149 } 150 151 /** 152 * Takes an input File containing the pack file, and generates a JarOutputStream. 153 * <p> 154 * Does not close its output. (The output can accumulate more elements.) 155 * @param in a File. 156 * @param out a JarOutputStream. 157 * @exception IOException if an error is encountered. 158 */ 159 public synchronized void unpack(File in, JarOutputStream out) throws IOException { 160 if (in == null) { 161 throw new NullPointerException("null input"); 162 } 163 if (out == null) { 164 throw new NullPointerException("null output"); 165 } 166 // Use the stream-based implementation. 167 // %%% Reconsider if native unpacker learns to memory-map the file. 168 try (FileInputStream instr = new FileInputStream(in)) { 169 unpack(instr, out); 170 } 171 if (props.getBoolean(Utils.UNPACK_REMOVE_PACKFILE)) { 172 in.delete(); 173 } 174 } 175 176 private class DoUnpack { 177 final int verbose = props.getInteger(Utils.DEBUG_VERBOSE); 178 179 { 180 props.setInteger(Pack200.Unpacker.PROGRESS, 0); 181 } 182 183 // Here's where the bits are read from disk: 184 final Package pkg = new Package(); 185 186 final boolean keepModtime 187 = Pack200.Packer.KEEP.equals( 188 props.getProperty(Utils.UNPACK_MODIFICATION_TIME, Pack200.Packer.KEEP)); 189 final boolean keepDeflateHint 190 = Pack200.Packer.KEEP.equals( 191 props.getProperty(Pack200.Unpacker.DEFLATE_HINT, Pack200.Packer.KEEP)); 192 final int modtime; 193 final boolean deflateHint; 194 { 195 if (!keepModtime) { 196 modtime = props.getTime(Utils.UNPACK_MODIFICATION_TIME); 197 } else { 198 modtime = pkg.default_modtime; 199 } 200 201 deflateHint = (keepDeflateHint) ? false : 202 props.getBoolean(java.util.jar.Pack200.Unpacker.DEFLATE_HINT); 203 } 204 205 // Checksum apparatus. 206 final CRC32 crc = new CRC32(); 207 final ByteArrayOutputStream bufOut = new ByteArrayOutputStream(); 208 final OutputStream crcOut = new CheckedOutputStream(bufOut, crc); 209 210 public void run(BufferedInputStream in, JarOutputStream out) throws IOException { 211 if (verbose > 0) { 212 props.list(System.out); 213 } 214 for (int seg = 1; ; seg++) { 215 unpackSegment(in, out); 216 217 // Try to get another segment. 218 if (!Utils.isPackMagic(Utils.readMagic(in))) break; 219 if (verbose > 0) 220 Utils.log.info("Finished segment #"+seg); 221 } 222 } 223 224 private void unpackSegment(InputStream in, JarOutputStream out) throws IOException { 225 props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"0"); 226 // Process the output directory or jar output. 227 new PackageReader(pkg, in).read(); 228 229 if (props.getBoolean("unpack.strip.debug")) pkg.stripAttributeKind("Debug"); 230 if (props.getBoolean("unpack.strip.compile")) pkg.stripAttributeKind("Compile"); 231 props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"50"); 232 pkg.ensureAllClassFiles(); 233 // Now write out the files. 234 Set<Package.Class> classesToWrite = new HashSet<>(pkg.getClasses()); 235 for (Package.File file : pkg.getFiles()) { 236 String name = file.nameString; 237 JarEntry je = new JarEntry(Utils.getJarEntryName(name)); 238 boolean deflate; 239 240 deflate = (keepDeflateHint) 241 ? (((file.options & Constants.FO_DEFLATE_HINT) != 0) || 242 ((pkg.default_options & Constants.AO_DEFLATE_HINT) != 0)) 243 : deflateHint; 244 245 boolean needCRC = !deflate; // STORE mode requires CRC 246 247 if (needCRC) crc.reset(); 248 bufOut.reset(); 249 if (file.isClassStub()) { 250 Package.Class cls = file.getStubClass(); 251 assert(cls != null); 252 new ClassWriter(cls, needCRC ? crcOut : bufOut).write(); 253 classesToWrite.remove(cls); // for an error check 254 } else { 255 // collect data & maybe CRC 256 file.writeTo(needCRC ? crcOut : bufOut); 257 } 258 je.setMethod(deflate ? JarEntry.DEFLATED : JarEntry.STORED); 259 if (needCRC) { 260 if (verbose > 0) 261 Utils.log.info("stored size="+bufOut.size()+" and crc="+crc.getValue()); 262 263 je.setMethod(JarEntry.STORED); 264 je.setSize(bufOut.size()); 265 je.setCrc(crc.getValue()); 266 } 267 if (keepModtime) { 268 je.setTime(file.modtime); 269 // Convert back to milliseconds 270 je.setTime((long)file.modtime * 1000); 271 } else { 272 je.setTime((long)modtime * 1000); 273 } 274 out.putNextEntry(je); 275 bufOut.writeTo(out); 276 out.closeEntry(); 277 if (verbose > 0) 278 Utils.log.info("Writing "+Utils.zeString((ZipEntry)je)); 279 } 280 assert(classesToWrite.isEmpty()); 281 props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"100"); 282 pkg.reset(); // reset for the next segment, if any 283 } 284 } 285 }