Source Code Cross Referenced for ExtraFieldUtils.java in  » IDE » tIDE » org » apache » tools » zip » 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 » IDE » tIDE » org.apache.tools.zip 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright  2001-2002,2004 The Apache Software Foundation
003:         *
004:         *  Licensed under the Apache License, Version 2.0 (the "License");
005:         *  you may not use this file except in compliance with the License.
006:         *  You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         *  Unless required by applicable law or agreed to in writing, software
011:         *  distributed under the License is distributed on an "AS IS" BASIS,
012:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         *  See the License for the specific language governing permissions and
014:         *  limitations under the License.
015:         *
016:         */
017:
018:        package org.apache.tools.zip;
019:
020:        import java.util.Hashtable;
021:        import java.util.Vector;
022:        import java.util.zip.ZipException;
023:
024:        /**
025:         * ZipExtraField related methods
026:         *
027:         * @author Stefan Bodewig
028:         * @version $Revision: 1.5.2.3 $
029:         */
030:        public class ExtraFieldUtils {
031:
032:            /**
033:             * Static registry of known extra fields.
034:             *
035:             * @since 1.1
036:             */
037:            private static Hashtable<ZipShort, Class> implementations;
038:
039:            static {
040:                implementations = new Hashtable<ZipShort, Class>();
041:                register(AsiExtraField.class);
042:            }
043:
044:            /**
045:             * Register a ZipExtraField implementation.
046:             *
047:             * <p>The given class must have a no-arg constructor and implement
048:             * the {@link ZipExtraField ZipExtraField interface}.</p>
049:             *
050:             * @since 1.1
051:             */
052:            public static void register(Class c) {
053:                try {
054:                    ZipExtraField ze = (ZipExtraField) c.newInstance();
055:                    implementations.put(ze.getHeaderId(), c);
056:                } catch (ClassCastException cc) {
057:                    throw new RuntimeException(c
058:                            + " doesn\'t implement ZipExtraField");
059:                } catch (InstantiationException ie) {
060:                    throw new RuntimeException(c + " is not a concrete class");
061:                } catch (IllegalAccessException ie) {
062:                    throw new RuntimeException(c
063:                            + "\'s no-arg constructor is not public");
064:                }
065:            }
066:
067:            /**
068:             * Create an instance of the approriate ExtraField, falls back to
069:             * {@link UnrecognizedExtraField UnrecognizedExtraField}.
070:             *
071:             * @since 1.1
072:             */
073:            public static ZipExtraField createExtraField(ZipShort headerId)
074:                    throws InstantiationException, IllegalAccessException {
075:                Class c = implementations.get(headerId);
076:                if (c != null) {
077:                    return (ZipExtraField) c.newInstance();
078:                }
079:                UnrecognizedExtraField u = new UnrecognizedExtraField();
080:                u.setHeaderId(headerId);
081:                return u;
082:            }
083:
084:            /**
085:             * Split the array into ExtraFields and populate them with the
086:             * give data.
087:             *
088:             * @since 1.1
089:             */
090:            public static ZipExtraField[] parse(byte[] data)
091:                    throws ZipException {
092:                Vector<ZipExtraField> v = new Vector<ZipExtraField>();
093:                int start = 0;
094:                while (start <= data.length - 4) {
095:                    ZipShort headerId = new ZipShort(data, start);
096:                    int length = (new ZipShort(data, start + 2)).getValue();
097:                    if (start + 4 + length > data.length) {
098:                        throw new ZipException("data starting at " + start
099:                                + " is in unknown format");
100:                    }
101:                    try {
102:                        ZipExtraField ze = createExtraField(headerId);
103:                        ze.parseFromLocalFileData(data, start + 4, length);
104:                        v.addElement(ze);
105:                    } catch (InstantiationException ie) {
106:                        throw new ZipException(ie.getMessage());
107:                    } catch (IllegalAccessException iae) {
108:                        throw new ZipException(iae.getMessage());
109:                    }
110:                    start += (length + 4);
111:                }
112:                if (start != data.length) { // array not exhausted
113:                    throw new ZipException("data starting at " + start
114:                            + " is in unknown format");
115:                }
116:
117:                ZipExtraField[] result = new ZipExtraField[v.size()];
118:                v.copyInto(result);
119:                return result;
120:            }
121:
122:            /**
123:             * Merges the local file data fields of the given ZipExtraFields.
124:             *
125:             * @since 1.1
126:             */
127:            public static byte[] mergeLocalFileDataData(ZipExtraField[] data) {
128:                int sum = 4 * data.length;
129:                for (int i = 0; i < data.length; i++) {
130:                    sum += data[i].getLocalFileDataLength().getValue();
131:                }
132:                byte[] result = new byte[sum];
133:                int start = 0;
134:                for (int i = 0; i < data.length; i++) {
135:                    System.arraycopy(data[i].getHeaderId().getBytes(), 0,
136:                            result, start, 2);
137:                    System.arraycopy(data[i].getLocalFileDataLength()
138:                            .getBytes(), 0, result, start + 2, 2);
139:                    byte[] local = data[i].getLocalFileDataData();
140:                    System.arraycopy(local, 0, result, start + 4, local.length);
141:                    start += (local.length + 4);
142:                }
143:                return result;
144:            }
145:
146:            /**
147:             * Merges the central directory fields of the given ZipExtraFields.
148:             *
149:             * @since 1.1
150:             */
151:            public static byte[] mergeCentralDirectoryData(ZipExtraField[] data) {
152:                int sum = 4 * data.length;
153:                for (int i = 0; i < data.length; i++) {
154:                    sum += data[i].getCentralDirectoryLength().getValue();
155:                }
156:                byte[] result = new byte[sum];
157:                int start = 0;
158:                for (int i = 0; i < data.length; i++) {
159:                    System.arraycopy(data[i].getHeaderId().getBytes(), 0,
160:                            result, start, 2);
161:                    System.arraycopy(data[i].getCentralDirectoryLength()
162:                            .getBytes(), 0, result, start + 2, 2);
163:                    byte[] local = data[i].getCentralDirectoryData();
164:                    System.arraycopy(local, 0, result, start + 4, local.length);
165:                    start += (local.length + 4);
166:                }
167:                return result;
168:            }
169:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.