1 /* 2 * Copyright (c) 2003, 2010, 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 @SuppressWarnings("unchecked") 85 public SortedMap properties() { 86 return props; 87 } 88 89 // Back-pointer to NativeUnpacker, when active. 90 Object _nunp; 91 92 93 public String toString() { 94 return Utils.getVersionString(); 95 } 96 97 //Driver routines 98 99 // The unpack worker... 100 /** 101 * Takes a packed-stream InputStream, and writes to a JarOutputStream. Internally 102 * the entire buffer must be read, it may be more efficient to read the packed-stream 103 * to a file and pass the File object, in the alternate method described below. 104 * <p> 105 * Closes its input but not its output. (The output can accumulate more elements.) 106 * @param in an InputStream. 107 * @param out a JarOutputStream. 108 * @exception IOException if an error is encountered. 109 */ 110 public void unpack(InputStream in, JarOutputStream out) throws IOException { 111 if (in == null) { 112 throw new NullPointerException("null input"); 113 } 114 if (out == null) { 115 throw new NullPointerException("null output"); 116 } 117 assert(Utils.currentInstance.get() == null); 118 TimeZone tz = (props.getBoolean(Utils.PACK_DEFAULT_TIMEZONE)) 119 ? null 120 : TimeZone.getDefault(); 121 122 try { 123 Utils.currentInstance.set(this); 124 if (tz != null) TimeZone.setDefault(TimeZone.getTimeZone("UTC")); 125 final int verbose = props.getInteger(Utils.DEBUG_VERBOSE); 126 BufferedInputStream in0 = new BufferedInputStream(in); 127 if (Utils.isJarMagic(Utils.readMagic(in0))) { 128 if (verbose > 0) 129 Utils.log.info("Copying unpacked JAR file..."); 130 Utils.copyJarFile(new JarInputStream(in0), out); 131 } else if (props.getBoolean(Utils.DEBUG_DISABLE_NATIVE)) { 132 (new DoUnpack()).run(in0, out); 133 in0.close(); 134 Utils.markJarFile(out); 135 } else { 136 (new NativeUnpack(this)).run(in0, out); 137 in0.close(); 138 Utils.markJarFile(out); 139 } 140 } finally { 141 _nunp = null; 142 Utils.currentInstance.set(null); 143 if (tz != null) TimeZone.setDefault(tz); 144 } 145 } 146 147 /** 148 * Takes an input File containing the pack file, and generates a JarOutputStream. 149 * <p> 150 * Does not close its output. (The output can accumulate more elements.) 151 * @param in a File. 152 * @param out a JarOutputStream. 153 * @exception IOException if an error is encountered. 154 */ 155 public void unpack(File in, JarOutputStream out) throws IOException { 156 if (in == null) { 157 throw new NullPointerException("null input"); 158 } 159 if (out == null) { 160 throw new NullPointerException("null output"); 161 } 162 // Use the stream-based implementation. 163 // %%% Reconsider if native unpacker learns to memory-map the file. 164 try (FileInputStream instr = new FileInputStream(in)) { 165 unpack(instr, out); 166 } 167 if (props.getBoolean(Utils.UNPACK_REMOVE_PACKFILE)) { 168 in.delete(); 169 } 170 } 171 172 private class DoUnpack { 173 final int verbose = props.getInteger(Utils.DEBUG_VERBOSE); 174 175 { 176 props.setInteger(Pack200.Unpacker.PROGRESS, 0); 177 } 178 179 // Here's where the bits are read from disk: 180 final Package pkg = new Package(); 181 182 final boolean keepModtime 183 = Pack200.Packer.KEEP.equals( 184 props.getProperty(Utils.UNPACK_MODIFICATION_TIME, Pack200.Packer.KEEP)); 185 final boolean keepDeflateHint 186 = Pack200.Packer.KEEP.equals( 187 props.getProperty(Pack200.Unpacker.DEFLATE_HINT, Pack200.Packer.KEEP)); 188 final int modtime; 189 final boolean deflateHint; 190 { 191 if (!keepModtime) { 192 modtime = props.getTime(Utils.UNPACK_MODIFICATION_TIME); 193 } else { 194 modtime = pkg.default_modtime; 195 } 196 197 deflateHint = (keepDeflateHint) ? false : 198 props.getBoolean(java.util.jar.Pack200.Unpacker.DEFLATE_HINT); 199 } 200 201 // Checksum apparatus. 202 final CRC32 crc = new CRC32(); 203 final ByteArrayOutputStream bufOut = new ByteArrayOutputStream(); 204 final OutputStream crcOut = new CheckedOutputStream(bufOut, crc); 205 206 public void run(BufferedInputStream in, JarOutputStream out) throws IOException { 207 if (verbose > 0) { 208 props.list(System.out); 209 } 210 for (int seg = 1; ; seg++) { 211 unpackSegment(in, out); 212 213 // Try to get another segment. 214 if (!Utils.isPackMagic(Utils.readMagic(in))) break; 215 if (verbose > 0) 216 Utils.log.info("Finished segment #"+seg); 217 } 218 } 219 220 private void unpackSegment(InputStream in, JarOutputStream out) throws IOException { 221 props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"0"); 222 // Process the output directory or jar output. 223 new PackageReader(pkg, in).read(); 224 225 if (props.getBoolean("unpack.strip.debug")) pkg.stripAttributeKind("Debug"); 226 if (props.getBoolean("unpack.strip.compile")) pkg.stripAttributeKind("Compile"); 227 props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"50"); 228 pkg.ensureAllClassFiles(); 229 // Now write out the files. 230 Set<Package.Class> classesToWrite = new HashSet<>(pkg.getClasses()); 231 for (Package.File file : pkg.getFiles()) { 232 String name = file.nameString; 233 JarEntry je = new JarEntry(Utils.getJarEntryName(name)); 234 boolean deflate; 235 236 deflate = (keepDeflateHint) 237 ? (((file.options & Constants.FO_DEFLATE_HINT) != 0) || 238 ((pkg.default_options & Constants.AO_DEFLATE_HINT) != 0)) 239 : deflateHint; 240 241 boolean needCRC = !deflate; // STORE mode requires CRC 242 243 if (needCRC) crc.reset(); 244 bufOut.reset(); 245 if (file.isClassStub()) { 246 Package.Class cls = file.getStubClass(); 247 assert(cls != null); 248 new ClassWriter(cls, needCRC ? crcOut : bufOut).write(); 249 classesToWrite.remove(cls); // for an error check 250 } else { 251 // collect data & maybe CRC 252 file.writeTo(needCRC ? crcOut : bufOut); 253 } 254 je.setMethod(deflate ? JarEntry.DEFLATED : JarEntry.STORED); 255 if (needCRC) { 256 if (verbose > 0) 257 Utils.log.info("stored size="+bufOut.size()+" and crc="+crc.getValue()); 258 259 je.setMethod(JarEntry.STORED); 260 je.setSize(bufOut.size()); 261 je.setCrc(crc.getValue()); 262 } 263 if (keepModtime) { 264 je.setTime(file.modtime); 265 // Convert back to milliseconds 266 je.setTime((long)file.modtime * 1000); 267 } else { 268 je.setTime((long)modtime * 1000); 269 } 270 out.putNextEntry(je); 271 bufOut.writeTo(out); 272 out.closeEntry(); 273 if (verbose > 0) 274 Utils.log.info("Writing "+Utils.zeString((ZipEntry)je)); 275 } 276 assert(classesToWrite.isEmpty()); 277 props.setProperty(java.util.jar.Pack200.Unpacker.PROGRESS,"100"); 278 pkg.reset(); // reset for the next segment, if any 279 } 280 } 281 }