001: /*
002: * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.java.util.jar.pack;
027:
028: import java.nio.*;
029: import java.io.*;
030: import java.nio.channels.*;
031: import java.util.Date;
032: import java.util.jar.*;
033: import java.util.zip.*;
034: import java.util.*;
035:
036: //import com.sun.java.util.jar.pack.Pack200;
037:
038: class NativeUnpack {
039: // Pointer to the native unpacker obj
040: private long unpackerPtr;
041:
042: // Input stream.
043: private BufferedInputStream in;
044:
045: private static synchronized native void initIDs();
046:
047: // Starts processing at the indicated position in the buffer.
048: // If the buffer is null, the readInputFn callback is used to get bytes.
049: // Returns (s<<32|f), the number of following segments and files.
050: private synchronized native long start(ByteBuffer buf, long offset);
051:
052: // Returns true if there's another, and fills in the parts.
053: private synchronized native boolean getNextFile(Object[] parts);
054:
055: private synchronized native ByteBuffer getUnusedInput();
056:
057: // Resets the engine and frees all resources.
058: // Returns total number of bytes consumed by the engine.
059: private synchronized native long finish();
060:
061: // Setting state in the unpacker.
062: protected synchronized native boolean setOption(String opt,
063: String value);
064:
065: protected synchronized native String getOption(String opt);
066:
067: private int _verbose;
068:
069: // State for progress bar:
070: private long _byteCount; // bytes read in current segment
071: private int _segCount; // number of segs scanned
072: private int _fileCount; // number of files written
073: private long _estByteLimit; // estimate of eventual total
074: private int _estSegLimit; // ditto
075: private int _estFileLimit; // ditto
076: private int _prevPercent = -1; // for monotonicity
077:
078: private final CRC32 _crc32 = new CRC32();
079: private byte[] _buf = new byte[1 << 14];
080:
081: private UnpackerImpl _p200;
082: private PropMap _props;
083:
084: static {
085: // If loading from stand alone build uncomment this.
086: // System.loadLibrary("unpack");
087: java.security.AccessController
088: .doPrivileged(new sun.security.action.LoadLibraryAction(
089: "unpack"));
090: initIDs();
091: }
092:
093: NativeUnpack(UnpackerImpl p200) {
094: super ();
095: _p200 = p200;
096: _props = p200._props;
097: p200._nunp = this ;
098: }
099:
100: // for JNI callbacks
101: static private Object currentInstance() {
102: UnpackerImpl p200 = (UnpackerImpl) Utils.currentInstance.get();
103: return (p200 == null) ? null : p200._nunp;
104: }
105:
106: // Callback from the unpacker engine to get more data.
107: private long readInputFn(ByteBuffer pbuf, long minlen)
108: throws IOException {
109: if (in == null)
110: return 0; // nothing is readable
111: long maxlen = pbuf.capacity() - pbuf.position();
112: assert (minlen <= maxlen); // don't talk nonsense
113: long numread = 0;
114: int steps = 0;
115: while (numread < minlen) {
116: steps++;
117: // read available input, up to buf.length or maxlen
118: int readlen = _buf.length;
119: if (readlen > (maxlen - numread))
120: readlen = (int) (maxlen - numread);
121: int nr = in.read(_buf, 0, readlen);
122: if (nr <= 0)
123: break;
124: numread += nr;
125: assert (numread <= maxlen);
126: // %%% get rid of this extra copy by using nio?
127: pbuf.put(_buf, 0, nr);
128: }
129: if (_verbose > 1)
130: Utils.log.fine("readInputFn(" + minlen + "," + maxlen
131: + ") => " + numread + " steps=" + steps);
132: if (maxlen > 100) {
133: _estByteLimit = _byteCount + maxlen;
134: } else {
135: _estByteLimit = (_byteCount + numread) * 20;
136: }
137: _byteCount += numread;
138: updateProgress();
139: return numread;
140: }
141:
142: private void updateProgress() {
143: // Progress is a combination of segment reading and file writing.
144: final double READ_WT = 0.33;
145: final double WRITE_WT = 0.67;
146: double readProgress = _segCount;
147: if (_estByteLimit > 0 && _byteCount > 0)
148: readProgress += (double) _byteCount / _estByteLimit;
149: double writeProgress = _fileCount;
150: double scaledProgress = READ_WT * readProgress
151: / Math.max(_estSegLimit, 1) + WRITE_WT * writeProgress
152: / Math.max(_estFileLimit, 1);
153: int percent = (int) Math.round(100 * scaledProgress);
154: if (percent > 100)
155: percent = 100;
156: if (percent > _prevPercent) {
157: _prevPercent = percent;
158: _props.setInteger(Pack200.Unpacker.PROGRESS, percent);
159: if (_verbose > 0)
160: Utils.log.info("progress = " + percent);
161: }
162: }
163:
164: private void copyInOption(String opt) {
165: String val = _props.getProperty(opt);
166: if (_verbose > 0)
167: Utils.log.info("set " + opt + "=" + val);
168: if (val != null) {
169: boolean set = setOption(opt, val);
170: if (!set)
171: Utils.log.warning("Invalid option " + opt + "=" + val);
172: }
173: }
174:
175: void run(InputStream inRaw, JarOutputStream jstream,
176: ByteBuffer presetInput) throws IOException {
177: BufferedInputStream in = new BufferedInputStream(inRaw);
178: this .in = in; // for readInputFn to see
179: _verbose = _props.getInteger(Utils.DEBUG_VERBOSE);
180: // Fix for BugId: 4902477, -unpack.modification.time = 1059010598000
181: // TODO eliminate and fix in unpack.cpp
182:
183: final int modtime = Pack200.Packer.KEEP.equals(_props
184: .getProperty(Utils.UNPACK_MODIFICATION_TIME, "0")) ? Constants.NO_MODTIME
185: : _props.getTime(Utils.UNPACK_MODIFICATION_TIME);
186:
187: copyInOption(Utils.DEBUG_VERBOSE);
188: copyInOption(Pack200.Unpacker.DEFLATE_HINT);
189: if (modtime == Constants.NO_MODTIME) // Dont pass KEEP && NOW
190: copyInOption(Utils.UNPACK_MODIFICATION_TIME);
191: updateProgress(); // reset progress bar
192: for (;;) {
193: // Read the packed bits.
194: long counts = start(presetInput, 0);
195: _byteCount = _estByteLimit = 0; // reset partial scan counts
196: ++_segCount; // just finished scanning a whole segment...
197: int nextSeg = (int) (counts >>> 32);
198: int nextFile = (int) (counts >>> 0);
199:
200: // Estimate eventual total number of segments and files.
201: _estSegLimit = _segCount + nextSeg;
202: double filesAfterThisSeg = _fileCount + nextFile;
203: _estFileLimit = (int) ((filesAfterThisSeg * _estSegLimit) / _segCount);
204:
205: // Write the files.
206: int[] intParts = { 0, 0, 0, 0 };
207: // intParts = {size.hi/lo, mod, defl}
208: Object[] parts = { intParts, null, null, null };
209: // parts = { {intParts}, name, data0/1 }
210: while (getNextFile(parts)) {
211: //BandStructure.printArrayTo(System.out, intParts, 0, parts.length);
212: String name = (String) parts[1];
213: long size = ((long) intParts[0] << 32)
214: + (((long) intParts[1] << 32) >>> 32);
215:
216: long mtime = (modtime != Constants.NO_MODTIME) ? modtime
217: : intParts[2];
218: boolean deflateHint = (intParts[3] != 0);
219: ByteBuffer data0 = (ByteBuffer) parts[2];
220: ByteBuffer data1 = (ByteBuffer) parts[3];
221: writeEntry(jstream, name, mtime, size, deflateHint,
222: data0, data1);
223: ++_fileCount;
224: updateProgress();
225: }
226: long consumed = finish();
227: if (_verbose > 0)
228: Utils.log.info("bytes consumed = " + consumed);
229: presetInput = getUnusedInput();
230: if (presetInput == null
231: && !Utils.isPackMagic(Utils.readMagic(in))) {
232: break;
233: }
234: if (_verbose > 0) {
235: if (presetInput != null)
236: Utils.log.info("unused input = " + presetInput);
237: }
238: }
239: }
240:
241: void run(InputStream in, JarOutputStream jstream)
242: throws IOException {
243: run(in, jstream, null);
244: }
245:
246: void run(File inFile, JarOutputStream jstream) throws IOException {
247: // %%% maybe memory-map the file, and pass it straight into unpacker
248: ByteBuffer mappedFile = null;
249: FileInputStream fis = new FileInputStream(inFile);
250: run(fis, jstream, mappedFile);
251: fis.close();
252: // Note: caller is responsible to finish with jstream.
253: }
254:
255: private void writeEntry(JarOutputStream j, String name, long mtime,
256: long lsize, boolean deflateHint, ByteBuffer data0,
257: ByteBuffer data1) throws IOException {
258: int size = (int) lsize;
259: if (size != lsize)
260: throw new IOException("file too large: " + lsize);
261:
262: CRC32 crc32 = _crc32;
263:
264: if (_verbose > 1)
265: Utils.log.fine("Writing entry: " + name + " size=" + size
266: + (deflateHint ? " deflated" : ""));
267:
268: if (_buf.length < size) {
269: int newSize = size;
270: while (newSize < _buf.length) {
271: newSize <<= 1;
272: if (newSize <= 0) {
273: newSize = size;
274: break;
275: }
276: }
277: _buf = new byte[newSize];
278: }
279: assert (_buf.length >= size);
280:
281: int fillp = 0;
282: if (data0 != null) {
283: int size0 = data0.capacity();
284: data0.get(_buf, fillp, size0);
285: fillp += size0;
286: }
287: if (data1 != null) {
288: int size1 = data1.capacity();
289: data1.get(_buf, fillp, size1);
290: fillp += size1;
291: }
292: while (fillp < size) {
293: // Fill in rest of data from the stream itself.
294: int nr = in.read(_buf, fillp, size - fillp);
295: if (nr <= 0)
296: throw new IOException("EOF at end of archive");
297: fillp += nr;
298: }
299:
300: ZipEntry z = new ZipEntry(name);
301: z.setTime((long) mtime * 1000);
302:
303: if (size == 0) {
304: z.setMethod(ZipOutputStream.STORED);
305: z.setSize(0);
306: z.setCrc(0);
307: z.setCompressedSize(0);
308: } else if (!deflateHint) {
309: z.setMethod(ZipOutputStream.STORED);
310: z.setSize(size);
311: z.setCompressedSize(size);
312: crc32.reset();
313: crc32.update(_buf, 0, size);
314: z.setCrc(crc32.getValue());
315: } else {
316: z.setMethod(Deflater.DEFLATED);
317: z.setSize(size);
318: }
319:
320: j.putNextEntry(z);
321:
322: if (size > 0)
323: j.write(_buf, 0, size);
324:
325: j.closeEntry();
326: if (_verbose > 0)
327: Utils.log.info("Writing " + Utils.zeString(z));
328: }
329: }
|