JarOutputStreampublic class JarOutputStream extends ZipOutputStream The JarOutputStream class is used to write the contents
of a JAR file to any output stream. It extends the class
java.util.zip.ZipOutputStream with support
for writing an optional Manifest entry. The
Manifest can be used to specify meta-information about
the JAR file and its entries. |
Fields Summary |
---|
private static final int | JAR_MAGIC | private boolean | firstEntry |
Constructors Summary |
---|
public JarOutputStream(OutputStream out, Manifest man)Creates a new JarOutputStream with the specified
Manifest . The manifest is written as the first
entry to the output stream.
super(out);
if (man == null) {
throw new NullPointerException("man");
}
ZipEntry e = new ZipEntry(JarFile.MANIFEST_NAME);
putNextEntry(e);
man.write(new BufferedOutputStream(this));
closeEntry();
| public JarOutputStream(OutputStream out)Creates a new JarOutputStream with no manifest.
super(out);
|
Methods Summary |
---|
private static int | get16(byte[] b, int off)
return (b[off] & 0xff) | ((b[off+1] & 0xff) << 8);
| private static boolean | hasMagic(byte[] edata)
/*
* Returns true if specified byte array contains the
* jar magic extra field id.
*/
try {
int i = 0;
while (i < edata.length) {
if (get16(edata, i) == JAR_MAGIC) {
return true;
}
i += get16(edata, i + 2) + 4;
}
} catch (ArrayIndexOutOfBoundsException e) {
// Invalid extra field data
}
return false;
| public void | putNextEntry(java.util.zip.ZipEntry ze)Begins writing a new JAR file entry and positions the stream
to the start of the entry data. This method will also close
any previous entry. The default compression method will be
used if no compression method was specified for the entry.
The current time will be used if the entry has no set modification
time.
if (firstEntry) {
// Make sure that extra field data for first JAR
// entry includes JAR magic number id.
byte[] edata = ze.getExtra();
if (edata != null && !hasMagic(edata)) {
// Prepend magic to existing extra data
byte[] tmp = new byte[edata.length + 4];
System.arraycopy(tmp, 4, edata, 0, edata.length);
edata = tmp;
} else {
edata = new byte[4];
}
set16(edata, 0, JAR_MAGIC); // extra field id
set16(edata, 2, 0); // extra field size
ze.setExtra(edata);
firstEntry = false;
}
super.putNextEntry(ze);
| private static void | set16(byte[] b, int off, int value)
b[off+0] = (byte)value;
b[off+1] = (byte)(value >> 8);
|
|