J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2004 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | package com.sun.java.util.jar.pack; |
| 28 | |
| 29 | import java.nio.*; |
| 30 | import java.io.*; |
| 31 | import java.nio.channels.*; |
| 32 | import java.util.Date; |
| 33 | import java.util.jar.*; |
| 34 | import java.util.zip.*; |
| 35 | import java.util.*; |
| 36 | //import com.sun.java.util.jar.pack.Pack200; |
| 37 | |
| 38 | |
| 39 | class NativeUnpack { |
| 40 | // Pointer to the native unpacker obj |
| 41 | private long unpackerPtr; |
| 42 | |
| 43 | // Input stream. |
| 44 | private BufferedInputStream in; |
| 45 | |
| 46 | private static synchronized native void initIDs(); |
| 47 | |
| 48 | // Starts processing at the indicated position in the buffer. |
| 49 | // If the buffer is null, the readInputFn callback is used to get bytes. |
| 50 | // Returns (s<<32|f), the number of following segments and files. |
| 51 | private synchronized native long start(ByteBuffer buf, long offset); |
| 52 | |
| 53 | // Returns true if there's another, and fills in the parts. |
| 54 | private synchronized native boolean getNextFile(Object[] parts); |
| 55 | |
| 56 | private synchronized native ByteBuffer getUnusedInput(); |
| 57 | |
| 58 | // Resets the engine and frees all resources. |
| 59 | // Returns total number of bytes consumed by the engine. |
| 60 | private synchronized native long finish(); |
| 61 | |
| 62 | // Setting state in the unpacker. |
| 63 | protected synchronized native boolean setOption(String opt, String value); |
| 64 | protected synchronized native String getOption(String opt); |
| 65 | |
| 66 | private int _verbose; |
| 67 | |
| 68 | // State for progress bar: |
| 69 | private long _byteCount; // bytes read in current segment |
| 70 | private int _segCount; // number of segs scanned |
| 71 | private int _fileCount; // number of files written |
| 72 | private long _estByteLimit; // estimate of eventual total |
| 73 | private int _estSegLimit; // ditto |
| 74 | private int _estFileLimit; // ditto |
| 75 | private int _prevPercent = -1; // for monotonicity |
| 76 | |
| 77 | private final CRC32 _crc32 = new CRC32(); |
| 78 | private byte[] _buf = new byte[1<<14]; |
| 79 | |
| 80 | private UnpackerImpl _p200; |
| 81 | private PropMap _props; |
| 82 | |
| 83 | static { |
| 84 | // If loading from stand alone build uncomment this. |
| 85 | // System.loadLibrary("unpack"); |
| 86 | java.security.AccessController.doPrivileged( |
| 87 | new sun.security.action.LoadLibraryAction("unpack")); |
| 88 | initIDs(); |
| 89 | } |
| 90 | |
| 91 | NativeUnpack(UnpackerImpl p200) { |
| 92 | super(); |
| 93 | _p200 = p200; |
| 94 | _props = p200._props; |
| 95 | p200._nunp = this; |
| 96 | } |
| 97 | |
| 98 | // for JNI callbacks |
| 99 | static private Object currentInstance() { |
| 100 | UnpackerImpl p200 = (UnpackerImpl) Utils.currentInstance.get(); |
| 101 | return (p200 == null)? null: p200._nunp; |
| 102 | } |
| 103 | |
| 104 | // Callback from the unpacker engine to get more data. |
| 105 | private long readInputFn(ByteBuffer pbuf, long minlen) throws IOException { |
| 106 | if (in == null) return 0; // nothing is readable |
| 107 | long maxlen = pbuf.capacity() - pbuf.position(); |
| 108 | assert(minlen <= maxlen); // don't talk nonsense |
| 109 | long numread = 0; |
| 110 | int steps = 0; |
| 111 | while (numread < minlen) { |
| 112 | steps++; |
| 113 | // read available input, up to buf.length or maxlen |
| 114 | int readlen = _buf.length; |
| 115 | if (readlen > (maxlen - numread)) |
| 116 | readlen = (int)(maxlen - numread); |
| 117 | int nr = in.read(_buf, 0, readlen); |
| 118 | if (nr <= 0) break; |
| 119 | numread += nr; |
| 120 | assert(numread <= maxlen); |
| 121 | // %%% get rid of this extra copy by using nio? |
| 122 | pbuf.put(_buf, 0, nr); |
| 123 | } |
| 124 | if (_verbose > 1) |
| 125 | Utils.log.fine("readInputFn("+minlen+","+maxlen+") => "+numread+" steps="+steps); |
| 126 | if (maxlen > 100) { |
| 127 | _estByteLimit = _byteCount + maxlen; |
| 128 | } else { |
| 129 | _estByteLimit = (_byteCount + numread) * 20; |
| 130 | } |
| 131 | _byteCount += numread; |
| 132 | updateProgress(); |
| 133 | return numread; |
| 134 | } |
| 135 | |
| 136 | private void updateProgress() { |
| 137 | // Progress is a combination of segment reading and file writing. |
| 138 | final double READ_WT = 0.33; |
| 139 | final double WRITE_WT = 0.67; |
| 140 | double readProgress = _segCount; |
| 141 | if (_estByteLimit > 0 && _byteCount > 0) |
| 142 | readProgress += (double)_byteCount / _estByteLimit; |
| 143 | double writeProgress = _fileCount; |
| 144 | double scaledProgress |
| 145 | = READ_WT * readProgress / Math.max(_estSegLimit,1) |
| 146 | + WRITE_WT * writeProgress / Math.max(_estFileLimit,1); |
| 147 | int percent = (int) Math.round(100*scaledProgress); |
| 148 | if (percent > 100) percent = 100; |
| 149 | if (percent > _prevPercent) { |
| 150 | _prevPercent = percent; |
| 151 | _props.setInteger(Pack200.Unpacker.PROGRESS, percent); |
| 152 | if (_verbose > 0) |
| 153 | Utils.log.info("progress = "+percent); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | private void copyInOption(String opt) { |
| 158 | String val = _props.getProperty(opt); |
| 159 | if (_verbose > 0) |
| 160 | Utils.log.info("set "+opt+"="+val); |
| 161 | if (val != null) { |
| 162 | boolean set = setOption(opt, val); |
| 163 | if (!set) |
| 164 | Utils.log.warning("Invalid option "+opt+"="+val); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void run(InputStream inRaw, JarOutputStream jstream, |
| 169 | ByteBuffer presetInput) throws IOException { |
| 170 | BufferedInputStream in = new BufferedInputStream(inRaw); |
| 171 | this.in = in; // for readInputFn to see |
| 172 | _verbose = _props.getInteger(Utils.DEBUG_VERBOSE); |
| 173 | // Fix for BugId: 4902477, -unpack.modification.time = 1059010598000 |
| 174 | // TODO eliminate and fix in unpack.cpp |
| 175 | |
| 176 | final int modtime = Pack200.Packer.KEEP.equals(_props.getProperty(Utils.UNPACK_MODIFICATION_TIME, "0")) ? |
| 177 | Constants.NO_MODTIME : _props.getTime(Utils.UNPACK_MODIFICATION_TIME); |
| 178 | |
| 179 | copyInOption(Utils.DEBUG_VERBOSE); |
| 180 | copyInOption(Pack200.Unpacker.DEFLATE_HINT); |
| 181 | if (modtime == Constants.NO_MODTIME) // Dont pass KEEP && NOW |
| 182 | copyInOption(Utils.UNPACK_MODIFICATION_TIME); |
| 183 | updateProgress(); // reset progress bar |
| 184 | for (;;) { |
| 185 | // Read the packed bits. |
| 186 | long counts = start(presetInput, 0); |
| 187 | _byteCount = _estByteLimit = 0; // reset partial scan counts |
| 188 | ++_segCount; // just finished scanning a whole segment... |
| 189 | int nextSeg = (int)( counts >>> 32 ); |
| 190 | int nextFile = (int)( counts >>> 0 ); |
| 191 | |
| 192 | // Estimate eventual total number of segments and files. |
| 193 | _estSegLimit = _segCount + nextSeg; |
| 194 | double filesAfterThisSeg = _fileCount + nextFile; |
| 195 | _estFileLimit = (int)( (filesAfterThisSeg * |
| 196 | _estSegLimit) / _segCount ); |
| 197 | |
| 198 | // Write the files. |
| 199 | int[] intParts = { 0,0, 0, 0 }; |
| 200 | // intParts = {size.hi/lo, mod, defl} |
| 201 | Object[] parts = { intParts, null, null, null }; |
| 202 | // parts = { {intParts}, name, data0/1 } |
| 203 | while (getNextFile(parts)) { |
| 204 | //BandStructure.printArrayTo(System.out, intParts, 0, parts.length); |
| 205 | String name = (String) parts[1]; |
| 206 | long size = ( (long)intParts[0] << 32) |
| 207 | + (((long)intParts[1] << 32) >>> 32); |
| 208 | |
| 209 | long mtime = (modtime != Constants.NO_MODTIME ) ? |
| 210 | modtime : intParts[2] ; |
| 211 | boolean deflateHint = (intParts[3] != 0); |
| 212 | ByteBuffer data0 = (ByteBuffer) parts[2]; |
| 213 | ByteBuffer data1 = (ByteBuffer) parts[3]; |
| 214 | writeEntry(jstream, name, mtime, size, deflateHint, |
| 215 | data0, data1); |
| 216 | ++_fileCount; |
| 217 | updateProgress(); |
| 218 | } |
| 219 | long consumed = finish(); |
| 220 | if (_verbose > 0) |
| 221 | Utils.log.info("bytes consumed = "+consumed); |
| 222 | presetInput = getUnusedInput(); |
| 223 | if (presetInput == null && |
| 224 | !Utils.isPackMagic(Utils.readMagic(in))) { |
| 225 | break; |
| 226 | } |
| 227 | if (_verbose > 0 ) { |
| 228 | if (presetInput != null) |
| 229 | Utils.log.info("unused input = "+presetInput); |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void run(InputStream in, JarOutputStream jstream) throws IOException { |
| 235 | run(in, jstream, null); |
| 236 | } |
| 237 | |
| 238 | void run(File inFile, JarOutputStream jstream) throws IOException { |
| 239 | // %%% maybe memory-map the file, and pass it straight into unpacker |
| 240 | ByteBuffer mappedFile = null; |
| 241 | FileInputStream fis = new FileInputStream(inFile); |
| 242 | run(fis, jstream, mappedFile); |
| 243 | fis.close(); |
| 244 | // Note: caller is responsible to finish with jstream. |
| 245 | } |
| 246 | |
| 247 | private void writeEntry(JarOutputStream j, String name, |
| 248 | long mtime, long lsize, boolean deflateHint, |
| 249 | ByteBuffer data0, ByteBuffer data1) throws IOException { |
| 250 | int size = (int)lsize; |
| 251 | if (size != lsize) |
| 252 | throw new IOException("file too large: "+lsize); |
| 253 | |
| 254 | CRC32 crc32 = _crc32; |
| 255 | |
| 256 | if (_verbose > 1) |
| 257 | Utils.log.fine("Writing entry: "+name+" size="+size |
| 258 | +(deflateHint?" deflated":"")); |
| 259 | |
| 260 | if (_buf.length < size) { |
| 261 | int newSize = size; |
| 262 | while (newSize < _buf.length) { |
| 263 | newSize <<= 1; |
| 264 | if (newSize <= 0) { |
| 265 | newSize = size; |
| 266 | break; |
| 267 | } |
| 268 | } |
| 269 | _buf = new byte[newSize]; |
| 270 | } |
| 271 | assert(_buf.length >= size); |
| 272 | |
| 273 | int fillp = 0; |
| 274 | if (data0 != null) { |
| 275 | int size0 = data0.capacity(); |
| 276 | data0.get(_buf, fillp, size0); |
| 277 | fillp += size0; |
| 278 | } |
| 279 | if (data1 != null) { |
| 280 | int size1 = data1.capacity(); |
| 281 | data1.get(_buf, fillp, size1); |
| 282 | fillp += size1; |
| 283 | } |
| 284 | while (fillp < size) { |
| 285 | // Fill in rest of data from the stream itself. |
| 286 | int nr = in.read(_buf, fillp, size - fillp); |
| 287 | if (nr <= 0) throw new IOException("EOF at end of archive"); |
| 288 | fillp += nr; |
| 289 | } |
| 290 | |
| 291 | ZipEntry z = new ZipEntry(name); |
| 292 | z.setTime( (long)mtime * 1000); |
| 293 | |
| 294 | if (size == 0) { |
| 295 | z.setMethod(ZipOutputStream.STORED); |
| 296 | z.setSize(0); |
| 297 | z.setCrc(0); |
| 298 | z.setCompressedSize(0); |
| 299 | } else if (!deflateHint) { |
| 300 | z.setMethod(ZipOutputStream.STORED); |
| 301 | z.setSize(size); |
| 302 | z.setCompressedSize(size); |
| 303 | crc32.reset(); |
| 304 | crc32.update(_buf, 0, size); |
| 305 | z.setCrc(crc32.getValue()); |
| 306 | } else { |
| 307 | z.setMethod(Deflater.DEFLATED); |
| 308 | z.setSize(size); |
| 309 | } |
| 310 | |
| 311 | j.putNextEntry(z); |
| 312 | |
| 313 | if (size > 0) |
| 314 | j.write(_buf, 0, size); |
| 315 | |
| 316 | j.closeEntry(); |
| 317 | if (_verbose > 0) Utils.log.info("Writing " + Utils.zeString(z)); |
| 318 | } |
| 319 | } |