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