Source Home >> Java Source 1.6.0 >> java.util.jar.JarOutputStream V 0.09
  • 001/*
  • 002 * @(#)JarOutputStream.java 1.24 07/01/09
  • 003 *
  • 004 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
  • 005 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  • 006 */
  • 007
  • 008package java.util.jar;
  • 009
  • 010import java.util.zip.*;
  • 011import java.io.*;
  • 012
  • 013/**
  • 014 * The <code>JarOutputStream</code> class is used to write the contents
  • 015 * of a JAR file to any output stream. It extends the class
  • 016 * <code>java.util.zip.ZipOutputStream</code> with support
  • 017 * for writing an optional <code>Manifest</code> entry. The
  • 018 * <code>Manifest</code> can be used to specify meta-information about
  • 019 * the JAR file and its entries.
  • 020 *
  • 021 * @author David Connelly
  • 022 * @version 1.24, 01/09/07
  • 023 * @see Manifest
  • 024 * @see java.util.zip.ZipOutputStream
  • 025 * @since 1.2
  • 026 */
  • 027public
  • 028class JarOutputStream extends ZipOutputStream {
  • 029 private static final int JAR_MAGIC = 0xCAFE;
  • 030
  • 031 /**
  • 032 * Creates a new <code>JarOutputStream</code> with the specified
  • 033 * <code>Manifest</code>. The manifest is written as the first
  • 034 * entry to the output stream.
  • 035 *
  • 036 * @param out the actual output stream
  • 037 * @param man the optional <code>Manifest</code>
  • 038 * @exception IOException if an I/O error has occurred
  • 039 */
  • 040 public JarOutputStream(OutputStream out, Manifest man) throws IOException {
  • 041 super(out);
  • 042 if (man == null) {
  • 043 throw new NullPointerException("man");
  • 044 }
  • 045 ZipEntry e = new ZipEntry(JarFile.MANIFEST_NAME);
  • 046 putNextEntry(e);
  • 047 man.write(new BufferedOutputStream(this));
  • 048 closeEntry();
  • 049 }
  • 050
  • 051 /**
  • 052 * Creates a new <code>JarOutputStream</code> with no manifest.
  • 053 * @param out the actual output stream
  • 054 * @exception IOException if an I/O error has occurred
  • 055 */
  • 056 public JarOutputStream(OutputStream out) throws IOException {
  • 057 super(out);
  • 058 }
  • 059
  • 060 /**
  • 061 * Begins writing a new JAR file entry and positions the stream
  • 062 * to the start of the entry data. This method will also close
  • 063 * any previous entry. The default compression method will be
  • 064 * used if no compression method was specified for the entry.
  • 065 * The current time will be used if the entry has no set modification
  • 066 * time.
  • 067 *
  • 068 * @param ze the ZIP/JAR entry to be written
  • 069 * @exception ZipException if a ZIP error has occurred
  • 070 * @exception IOException if an I/O error has occurred
  • 071 */
  • 072 public void putNextEntry(ZipEntry ze) throws IOException {
  • 073 if (firstEntry) {
  • 074 // Make sure that extra field data for first JAR
  • 075 // entry includes JAR magic number id.
  • 076 byte[] edata = ze.getExtra();
  • 077 if (edata == null || !hasMagic(edata)) {
  • 078 if (edata == null) {
  • 079 edata = new byte[4];
  • 080 } else {
  • 081 // Prepend magic to existing extra data
  • 082 byte[] tmp = new byte[edata.length + 4];
  • 083 System.arraycopy(edata, 0, tmp, 4, edata.length);
  • 084 edata = tmp;
  • 085 }
  • 086 set16(edata, 0, JAR_MAGIC); // extra field id
  • 087 set16(edata, 2, 0); // extra field size
  • 088 ze.setExtra(edata);
  • 089 }
  • 090 firstEntry = false;
  • 091 }
  • 092 super.putNextEntry(ze);
  • 093 }
  • 094
  • 095 private boolean firstEntry = true;
  • 096
  • 097 /*
  • 098 * Returns true if specified byte array contains the
  • 099 * jar magic extra field id.
  • 100 */
  • 101 private static boolean hasMagic(byte[] edata) {
  • 102 try {
  • 103 int i = 0;
  • 104 while (i < edata.length) {
  • 105 if (get16(edata, i) == JAR_MAGIC) {
  • 106 return true;
  • 107 }
  • 108 i += get16(edata, i + 2) + 4;
  • 109 }
  • 110 } catch (ArrayIndexOutOfBoundsException e) {
  • 111 // Invalid extra field data
  • 112 }
  • 113 return false;
  • 114 }
  • 115
  • 116 /*
  • 117 * Fetches unsigned 16-bit value from byte array at specified offset.
  • 118 * The bytes are assumed to be in Intel (little-endian) byte order.
  • 119 */
  • 120 private static int get16(byte[] b, int off) {
  • 121 return (b[off] & 0xff) | ((b[off+1] & 0xff) << 8);
  • 122 }
  • 123
  • 124 /*
  • 125 * Sets 16-bit value at specified offset. The bytes are assumed to
  • 126 * be in Intel (little-endian) byte order.
  • 127 */
  • 128 private static void set16(byte[] b, int off, int value) {
  • 129 b[off+0] = (byte)value;
  • 130 b[off+1] = (byte)(value >> 8);
  • 131 }
  • 132}

文件:JarOutputStream.java
包名:java.util.jar
类名:JarOutputStream
继承:ZipOutputStream
接口: