KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > ant > zip > ExtraFieldUtils


1 /*
2  * Copyright 2001-2002,2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package net.sourceforge.groboutils.codecoverage.v2.ant.zip;
19
20 import java.util.Hashtable JavaDoc;
21 import java.util.Vector JavaDoc;
22 import java.util.zip.ZipException JavaDoc;
23
24 /**
25  * ZipExtraField related methods
26  *
27  * @author Stefan Bodewig
28  * @version $Revision: 1.1 $
29  */

30 public class ExtraFieldUtils {
31
32     /**
33      * Static registry of known extra fields.
34      *
35      * @since 1.1
36      */

37     private static Hashtable JavaDoc implementations;
38
39     static {
40         implementations = new Hashtable JavaDoc();
41         register(AsiExtraField.class);
42     }
43
44     /**
45      * Register a ZipExtraField implementation.
46      *
47      * <p>The given class must have a no-arg constructor and implement
48      * the {@link ZipExtraField ZipExtraField interface}.</p>
49      *
50      * @since 1.1
51      */

52     public static void register(Class JavaDoc c) {
53         try {
54             ZipExtraField ze = (ZipExtraField) c.newInstance();
55             implementations.put(ze.getHeaderId(), c);
56         } catch (ClassCastException JavaDoc cc) {
57             throw new RuntimeException JavaDoc(c + " doesn\'t implement ZipExtraField");
58         } catch (InstantiationException JavaDoc ie) {
59             throw new RuntimeException JavaDoc(c + " is not a concrete class");
60         } catch (IllegalAccessException JavaDoc ie) {
61             throw new RuntimeException JavaDoc(c + "\'s no-arg constructor is not public");
62         }
63     }
64
65     /**
66      * Create an instance of the approriate ExtraField, falls back to
67      * {@link UnrecognizedExtraField UnrecognizedExtraField}.
68      *
69      * @since 1.1
70      */

71     public static ZipExtraField createExtraField(ZipShort headerId)
72         throws InstantiationException JavaDoc, IllegalAccessException JavaDoc {
73         Class JavaDoc c = (Class JavaDoc) implementations.get(headerId);
74         if (c != null) {
75             return (ZipExtraField) c.newInstance();
76         }
77         UnrecognizedExtraField u = new UnrecognizedExtraField();
78         u.setHeaderId(headerId);
79         return u;
80     }
81
82     /**
83      * Split the array into ExtraFields and populate them with the
84      * give data.
85      *
86      * @since 1.1
87      */

88     public static ZipExtraField[] parse(byte[] data) throws ZipException JavaDoc {
89         Vector JavaDoc v = new Vector JavaDoc();
90         int start = 0;
91         while (start <= data.length - 4) {
92             ZipShort headerId = new ZipShort(data, start);
93             int length = (new ZipShort(data, start + 2)).getValue();
94             if (start + 4 + length > data.length) {
95                 throw new ZipException JavaDoc("data starting at " + start
96                     + " is in unknown format");
97             }
98             try {
99                 ZipExtraField ze = createExtraField(headerId);
100                 ze.parseFromLocalFileData(data, start + 4, length);
101                 v.addElement(ze);
102             } catch (InstantiationException JavaDoc ie) {
103                 throw new ZipException JavaDoc(ie.getMessage());
104             } catch (IllegalAccessException JavaDoc iae) {
105                 throw new ZipException JavaDoc(iae.getMessage());
106             }
107             start += (length + 4);
108         }
109         if (start != data.length) { // array not exhausted
110
throw new ZipException JavaDoc("data starting at " + start
111                 + " is in unknown format");
112         }
113
114         ZipExtraField[] result = new ZipExtraField[v.size()];
115         v.copyInto(result);
116         return result;
117     }
118
119     /**
120      * Merges the local file data fields of the given ZipExtraFields.
121      *
122      * @since 1.1
123      */

124     public static byte[] mergeLocalFileDataData(ZipExtraField[] data) {
125         int sum = 4 * data.length;
126         for (int i = 0; i < data.length; i++) {
127             sum += data[i].getLocalFileDataLength().getValue();
128         }
129         byte[] result = new byte[sum];
130         int start = 0;
131         for (int i = 0; i < data.length; i++) {
132             System.arraycopy(data[i].getHeaderId().getBytes(),
133                              0, result, start, 2);
134             System.arraycopy(data[i].getLocalFileDataLength().getBytes(),
135                              0, result, start + 2, 2);
136             byte[] local = data[i].getLocalFileDataData();
137             System.arraycopy(local, 0, result, start + 4, local.length);
138             start += (local.length + 4);
139         }
140         return result;
141     }
142
143     /**
144      * Merges the central directory fields of the given ZipExtraFields.
145      *
146      * @since 1.1
147      */

148     public static byte[] mergeCentralDirectoryData(ZipExtraField[] data) {
149         int sum = 4 * data.length;
150         for (int i = 0; i < data.length; i++) {
151             sum += data[i].getCentralDirectoryLength().getValue();
152         }
153         byte[] result = new byte[sum];
154         int start = 0;
155         for (int i = 0; i < data.length; i++) {
156             System.arraycopy(data[i].getHeaderId().getBytes(),
157                              0, result, start, 2);
158             System.arraycopy(data[i].getCentralDirectoryLength().getBytes(),
159                              0, result, start + 2, 2);
160             byte[] local = data[i].getCentralDirectoryData();
161             System.arraycopy(local, 0, result, start + 4, local.length);
162             start += (local.length + 4);
163         }
164         return result;
165     }
166 }
167
Popular Tags