1 7 8 package java.util.jar; 9 10 import java.util.zip.*; 11 import java.io.*; 12 13 27 public 28 class JarOutputStream extends ZipOutputStream { 29 private static final int JAR_MAGIC = 0xCAFE; 30 31 40 public JarOutputStream(OutputStream out, Manifest man) throws IOException { 41 super(out); 42 if (man == null) { 43 throw new NullPointerException ("man"); 44 } 45 ZipEntry e = new ZipEntry(JarFile.MANIFEST_NAME); 46 putNextEntry(e); 47 man.write(new BufferedOutputStream(this)); 48 closeEntry(); 49 } 50 51 56 public JarOutputStream(OutputStream out) throws IOException { 57 super(out); 58 } 59 60 72 public void putNextEntry(ZipEntry ze) throws IOException { 73 if (firstEntry) { 74 byte[] edata = ze.getExtra(); 77 if (edata != null && !hasMagic(edata)) { 78 byte[] tmp = new byte[edata.length + 4]; 80 System.arraycopy(tmp, 4, edata, 0, edata.length); 81 edata = tmp; 82 } else { 83 edata = new byte[4]; 84 } 85 set16(edata, 0, JAR_MAGIC); set16(edata, 2, 0); ze.setExtra(edata); 88 firstEntry = false; 89 } 90 super.putNextEntry(ze); 91 } 92 93 private boolean firstEntry = true; 94 95 99 private static boolean hasMagic(byte[] edata) { 100 try { 101 int i = 0; 102 while (i < edata.length) { 103 if (get16(edata, i) == JAR_MAGIC) { 104 return true; 105 } 106 i += get16(edata, i + 2) + 4; 107 } 108 } catch (ArrayIndexOutOfBoundsException e) { 109 } 111 return false; 112 } 113 114 118 private static int get16(byte[] b, int off) { 119 return (b[off] & 0xff) | ((b[off+1] & 0xff) << 8); 120 } 121 122 126 private static void set16(byte[] b, int off, int value) { 127 b[off+0] = (byte)value; 128 b[off+1] = (byte)(value >> 8); 129 } 130 } 131 | Popular Tags |