1. /*
  2. * @(#)JarOutputStream.java 1.19 00/02/02
  3. *
  4. * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.util.jar;
  11. import java.util.zip.*;
  12. import java.io.*;
  13. /**
  14. * The <code>JarOutputStream</code> class is used to write the contents
  15. * of a JAR file to any output stream. It extends the class
  16. * <code>java.util.zip.ZipOutputStream</code> with support
  17. * for writing an optional <code>Manifest</code> entry. The
  18. * <code>Manifest</code> can be used to specify meta-information about
  19. * the JAR file and its entries.
  20. *
  21. * @author David Connelly
  22. * @version 1.19, 02/02/00
  23. * @see Manifest
  24. * @see java.util.zip.ZipOutputStream
  25. * @since 1.2
  26. */
  27. public
  28. class JarOutputStream extends ZipOutputStream {
  29. private static final int JAR_MAGIC = 0xCAFE;
  30. /**
  31. * Creates a new <code>JarOutputStream</code> with the specified
  32. * <code>Manifest</code>. The manifest is written as the first
  33. * entry to the output stream.
  34. *
  35. * @param out the actual output stream
  36. * @param man the optional <code>Manifest</code>
  37. * @exception IOException if an I/O error has occurred
  38. */
  39. public JarOutputStream(OutputStream out, Manifest man) throws IOException {
  40. super(out);
  41. if (man == null) {
  42. throw new NullPointerException("man");
  43. }
  44. ZipEntry e = new ZipEntry(JarFile.MANIFEST_NAME);
  45. putNextEntry(e);
  46. man.write(new BufferedOutputStream(this));
  47. closeEntry();
  48. }
  49. /**
  50. * Creates a new <code>JarOutputStream</code> with no manifest.
  51. * @param out the actual output stream
  52. * @exception IOException if an I/O error has occurred
  53. */
  54. public JarOutputStream(OutputStream out) throws IOException {
  55. super(out);
  56. }
  57. /**
  58. * Begins writing a new JAR file entry and positions the stream
  59. * to the start of the entry data. This method will also close
  60. * any previous entry. The default compression method will be
  61. * used if no compression method was specified for the entry.
  62. * The current time will be used if the entry has no set modification
  63. * time.
  64. *
  65. * @param ze the ZIP/JAR entry to be written
  66. * @exception ZipException if a ZIP error has occurred
  67. * @exception IOException if an I/O error has occurred
  68. */
  69. public void putNextEntry(ZipEntry ze) throws IOException {
  70. if (firstEntry) {
  71. // Make sure that extra field data for first JAR
  72. // entry includes JAR magic number id.
  73. byte[] edata = ze.getExtra();
  74. if (edata != null && !hasMagic(edata)) {
  75. // Prepend magic to existing extra data
  76. byte[] tmp = new byte[edata.length + 4];
  77. System.arraycopy(tmp, 4, edata, 0, edata.length);
  78. edata = tmp;
  79. } else {
  80. edata = new byte[4];
  81. }
  82. set16(edata, 0, JAR_MAGIC); // extra field id
  83. set16(edata, 2, 0); // extra field size
  84. ze.setExtra(edata);
  85. firstEntry = false;
  86. }
  87. super.putNextEntry(ze);
  88. }
  89. private boolean firstEntry = true;
  90. /*
  91. * Returns true if specified byte array contains the
  92. * jar magic extra field id.
  93. */
  94. private static boolean hasMagic(byte[] edata) {
  95. try {
  96. int i = 0;
  97. while (i < edata.length) {
  98. if (get16(edata, i) == JAR_MAGIC) {
  99. return true;
  100. }
  101. i += get16(edata, i + 2) + 4;
  102. }
  103. } catch (ArrayIndexOutOfBoundsException e) {
  104. // Invalid extra field data
  105. }
  106. return false;
  107. }
  108. /*
  109. * Fetches unsigned 16-bit value from byte array at specified offset.
  110. * The bytes are assumed to be in Intel (little-endian) byte order.
  111. */
  112. private static int get16(byte[] b, int off) {
  113. return (b[off] & 0xff) | ((b[off+1] & 0xff) << 8);
  114. }
  115. /*
  116. * Sets 16-bit value at specified offset. The bytes are assumed to
  117. * be in Intel (little-endian) byte order.
  118. */
  119. private static void set16(byte[] b, int off, int value) {
  120. b[off+0] = (byte)value;
  121. b[off+1] = (byte)(value >> 8);
  122. }
  123. }