API Overview API Index Package Overview Direct link to this page
JDK 1.6
  java.util.jar. Manifest View Javadoc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428

/*
 * @(#)Manifest.java	1.50 06/04/21
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package java.util.jar;

import java.io.FilterInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;

/**
 * The Manifest class is used to maintain Manifest entry names and their
 * associated Attributes. There are main Manifest Attributes as well as
 * per-entry Attributes. For information on the Manifest format, please
 * see the 
 * <a href="../../../../technotes/guides/jar/jar.html">
 * Manifest format specification</a>.
 *
 * @author  David Connelly
 * @version 1.50, 04/21/06
 * @see	    Attributes
 * @since   1.2
 */
public class Manifest implements Cloneable {
    // manifest main attributes
    private Attributes attr = new Attributes();

    // manifest entries
    private Map entries = new HashMap();

    /**
     * Constructs a new, empty Manifest.
     */
    public Manifest() {
    }

    /**
     * Constructs a new Manifest from the specified input stream.
     *
     * @param is the input stream containing manifest data
     * @throws IOException if an I/O error has occured
     */
    public Manifest(InputStream is) throws IOException {
	read(is);
    }

    /**
     * Constructs a new Manifest that is a copy of the specified Manifest.
     *
     * @param man the Manifest to copy
     */
    public Manifest(Manifest man) {
	attr.putAll(man.getMainAttributes());
	entries.putAll(man.getEntries());
    }

    /**
     * Returns the main Attributes for the Manifest.
     * @return the main Attributes for the Manifest
     */
    public Attributes getMainAttributes() {
	return attr;
    }

    /**
     * Returns a Map of the entries contained in this Manifest. Each entry
     * is represented by a String name (key) and associated Attributes (value).
     * The Map permits the {@code null} key, but no entry with a null key is
     * created by {@link #read}, nor is such an entry written by using {@link
     * #write}.
     *
     * @return a Map of the entries contained in this Manifest
     */
    public Map<String,Attributes> getEntries() {
	return entries;
    }

    /**
     * Returns the Attributes for the specified entry name.
     * This method is defined as:
     * <pre>
     *	    return (Attributes)getEntries().get(name)
     * </pre>
     * Though {@code null} is a valid {@code name}, when
     * {@code getAttributes(null)} is invoked on a {@code Manifest}
     * obtained from a jar file, {@code null} will be returned.  While jar
     * files themselves do not allow {@code null}-named attributes, it is
     * possible to invoke {@link #getEntries} on a {@code Manifest}, and
     * on that result, invoke {@code put} with a null key and an
     * arbitrary value.  Subsequent invocations of
     * {@code getAttributes(null)} will return the just-{@code put}
     * value.
     * <p>
     * Note that this method does not return the manifest's main attributes;
     * see {@link #getMainAttributes}.
     *
     * @param name entry name
     * @return the Attributes for the specified entry name
     */
    public Attributes getAttributes(String name) {
	return (Attributes)getEntries().get(name);
    }

    /**
     * Clears the main Attributes as well as the entries in this Manifest.
     */
    public void clear() {
	attr.clear();
	entries.clear();
    }

    /**
     * Writes the Manifest to the specified OutputStream. 
     * Attributes.Name.MANIFEST_VERSION must be set in 
     * MainAttributes prior to invoking this method.
     *
     * @param out the output stream
     * @exception IOException if an I/O error has occurred
     * @see #getMainAttributes
     */
    public void write(OutputStream out) throws IOException {
	DataOutputStream dos = new DataOutputStream(out);
	// Write out the main attributes for the manifest
	attr.writeMain(dos);
	// Now write out the pre-entry attributes
	Iterator it = entries.entrySet().iterator();
	while (it.hasNext()) {
	    Map.Entry e = (Map.Entry)it.next();
            StringBuffer buffer = new StringBuffer("Name: ");
            String value = (String)e.getKey();
            if (value != null) {
                byte[] vb = value.getBytes("UTF8");
                value = new String(vb, 0, 0, vb.length);
            }
	    buffer.append(value);
	    buffer.append("\r\n");
            make72Safe(buffer);
            dos.writeBytes(buffer.toString());
	    ((Attributes)e.getValue()).write(dos);
	}
	dos.flush();
    }

    /**
     * Adds line breaks to enforce a maximum 72 bytes per line.
     */
    static void make72Safe(StringBuffer line) {
        int length = line.length();
        if (length > 72) {
            int index = 70;
            while (index < length - 2) {
                line.insert(index, "\r\n ");
                index += 72;
                length += 3;
            }
        }
        return;
    }

    /**
     * Reads the Manifest from the specified InputStream. The entry
     * names and attributes read will be merged in with the current
     * manifest entries.
     *
     * @param is the input stream
     * @exception IOException if an I/O error has occurred
     */
    public void read(InputStream is) throws IOException {
	// Buffered input stream for reading manifest data
	FastInputStream fis = new FastInputStream(is);
	// Line buffer
	byte[] lbuf = new byte[512];
	// Read the main attributes for the manifest
	attr.read(fis, lbuf);
	// Total number of entries, attributes read
	int ecount = 0, acount = 0;
	// Average size of entry attributes
	int asize = 2;
	// Now parse the manifest entries
	int len;
	String name = null;
        boolean skipEmptyLines = true;
        byte[] lastline = null;

	while ((len = fis.readLine(lbuf)) != -1) {
	    if (lbuf[--len] != '\n') {
		throw new IOException("manifest line too long");
	    }
	    if (len > 0 && lbuf[len-1] == '\r') {
		--len;
	    }
            if (len == 0 && skipEmptyLines) {
                continue;
            }
            skipEmptyLines = false;

	    if (name == null) {
		name = parseName(lbuf, len);
		if (name == null) {
		    throw new IOException("invalid manifest format");                
		}
                if (fis.peek() == ' ') {
		    // name is wrapped
                    lastline = new byte[len - 6];
                    System.arraycopy(lbuf, 6, lastline, 0, len - 6);
                    continue;
                }
	    } else {
		// continuation line
                byte[] buf = new byte[lastline.length + len - 1];
                System.arraycopy(lastline, 0, buf, 0, lastline.length);
                System.arraycopy(lbuf, 1, buf, lastline.length, len - 1);
                if (fis.peek() == ' ') {
		    // name is wrapped
                    lastline = buf;
		    continue;
	        }
		name = new String(buf, 0, buf.length, "UTF8");
                lastline = null;
	    }
	    Attributes attr = getAttributes(name);
	    if (attr == null) {
		attr = new Attributes(asize);
		entries.put(name, attr);
	    }
	    attr.read(fis, lbuf);
	    ecount++;
	    acount += attr.size();
	    //XXX: Fix for when the average is 0. When it is 0, 
	    // you get an Attributes object with an initial
	    // capacity of 0, which tickles a bug in HashMap.
	    asize = Math.max(2, acount / ecount);

	    name = null;
            skipEmptyLines = true;
	}
    }

    private String parseName(byte[] lbuf, int len) {
	if (toLower(lbuf[0]) == 'n' && toLower(lbuf[1]) == 'a' &&
	    toLower(lbuf[2]) == 'm' && toLower(lbuf[3]) == 'e' &&
	    lbuf[4] == ':' && lbuf[5] == ' ') {
            try {
	        return new String(lbuf, 6, len - 6, "UTF8");
            }
            catch (Exception e) {
            }
	}
	return null;
    }

    private int toLower(int c) {
	return (c >= 'A' && c <= 'Z') ? 'a' + (c - 'A') : c;
    }

    /**
     * Returns true if the specified Object is also a Manifest and has
     * the same main Attributes and entries.
     *
     * @param o the object to be compared
     * @return true if the specified Object is also a Manifest and has
     * the same main Attributes and entries
     */
    public boolean equals(Object o) {
	if (o instanceof Manifest) {
	    Manifest m = (Manifest)o;
	    return attr.equals(m.getMainAttributes()) &&
		   entries.equals(m.getEntries());
	} else {
	    return false;
	}
    }

    /**
     * Returns the hash code for this Manifest.
     */
    public int hashCode() {
	return attr.hashCode() + entries.hashCode();
    }

    /**
     * Returns a shallow copy of this Manifest.  The shallow copy is
     * implemented as follows:
     * <pre>
     *     public Object clone() { return new Manifest(this); }
     * </pre>
     * @return a shallow copy of this Manifest
     */
    public Object clone() {
	return new Manifest(this);
    }

    /*
     * A fast buffered input stream for parsing manifest files.
     */
    static class FastInputStream extends FilterInputStream {
	private byte buf[];
	private int count = 0;
	private int pos = 0;

	FastInputStream(InputStream in) {
	    this(in, 8192);
	}

	FastInputStream(InputStream in, int size) {
	    super(in);
	    buf = new byte[size];
	}

	public int read() throws IOException {
	    if (pos >= count) {
		fill();
		if (pos >= count) {
		    return -1;
		}
	    }
	    return buf[pos++] & 0xff;
	}

	public int read(byte[] b, int off, int len) throws IOException {
	    int avail = count - pos;
	    if (avail <= 0) {
		if (len >= buf.length) {
		    return in.read(b, off, len);
		}
		fill();
		avail = count - pos;
		if (avail <= 0) {
		    return -1;
		}
	    }
	    if (len > avail) {
		len = avail;
	    }
	    System.arraycopy(buf, pos, b, off, len);
	    pos += len;
	    return len;
	}

	/*
	 * Reads 'len' bytes from the input stream, or until an end-of-line
	 * is reached. Returns the number of bytes read.
	 */
	public int readLine(byte[] b, int off, int len) throws IOException {
	    byte[] tbuf = this.buf;
	    int total = 0;
	    while (total < len) {
		int avail = count - pos;
		if (avail <= 0) {
		    fill();
		    avail = count - pos;
		    if (avail <= 0) {
			return -1;
		    }
		}
		int n = len - total;
		if (n > avail) {
		    n = avail;
		}
		int tpos = pos;
		int maxpos = tpos + n;
		while (tpos < maxpos && tbuf[tpos++] != '\n') ;
		n = tpos - pos;
		System.arraycopy(tbuf, pos, b, off, n);
		off += n;
		total += n;
		pos = tpos;
		if (tbuf[tpos-1] == '\n') {
		    break;
		}
	    }
	    return total;
	}

	public byte peek() throws IOException {
	    if (pos == count)
		fill();
	    return buf[pos];
	}

	public int readLine(byte[] b) throws IOException {
	    return readLine(b, 0, b.length);
	}

	public long skip(long n) throws IOException {
	    if (n <= 0) {
		return 0;
	    }
	    long avail = count - pos;
	    if (avail <= 0) {
		return in.skip(n);
	    }
	    if (n > avail) {
		n = avail;
	    }
	    pos += n;
	    return n;
	}

	public int available() throws IOException {
	    return (count - pos) + in.available();
	}

	public void close() throws IOException {
	    if (in != null) {
		in.close();
		in = null;
		buf = null;
	    }
	}

	private void fill() throws IOException {
	    count = pos = 0;
	    int n = in.read(buf, 0, buf.length);
	    if (n > 0) {
		count = n;
	    }
	}
    }
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar