Source Code Cross Referenced for NativeUnpack.java in  » 6.0-JDK-Modules-com.sun.java » util » com » sun » java » util » jar » pack » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules com.sun.java » util » com.sun.java.util.jar.pack 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.