Code example for JarOutputStream

Methods: closeEntry, putNextEntry

0
    } else { 
      putNextEntry(new ZipEntry(name));
      BufferedOutputStream s = new BufferedOutputStream(outputJar);
      writeClassTo(cf, mods, s);
      s.flush();
      outputJar.closeEntry();
      return true; 
    } 
  } 
 
  /** 
   * Set the JAR Comment for the output JAR. 
   */ 
  final public void setJARComment(String comment) throws IOException, IllegalStateException {
    makeOutputJar(); 
    outputJar.setComment(comment);
  } 
 
  final void makeOutputJar() throws IOException, IllegalStateException { 
    if (outputJar == null) {
      if (outputFile == null) {
        throw new IllegalStateException("Output file was not set");
      } 
 
      outputJar = new JarOutputStream(new FileOutputStream(outputFile));
    } 
  } 
 
  /** 
   * Skip the last class returned in every future traversal of the class list. 
   */ 
  final public void setIgnore(boolean ignore) throws IllegalArgumentException {
    if (inputIndex == 0) {
      throw new IllegalArgumentException("Must get a class before ignoring it");
    } 
 
    ignoringInputs.set(inputIndex - 1);
  } 
 
  private static byte[] cachedBuf;
 
  private static synchronized byte[] makeBuf() { 
    if (cachedBuf != null) {
      byte[] r = cachedBuf;
      cachedBuf = null;
      return r;
    } else { 
      return new byte[60000]; 
    } 
  } 
 
  private static synchronized void releaseBuf(byte[] buf) {
    cachedBuf = buf;
  } 
 
  public static void copyStream(InputStream in, OutputStream out) throws IllegalArgumentException, IOException {
    if (in == null) {
      throw new IllegalArgumentException("in == null");
    } 
    byte[] buf = makeBuf();
    try { 
      while (true) { 
        int read = in.read(buf);
        if (read < 0) {
          return; 
        } 
        out.write(buf, 0, read);
      } 
    } finally { 
      releaseBuf(buf);
    } 
  } 
 
  /** 
   * Add a raw ZipEntry to the output JAR. Call endOutputJarEntry() when you're done. 
   *  
   * @return the OutputStream to be used to write the entry contents 
   */ 
  final public OutputStream addOutputJarEntry(ZipEntry ze) throws IOException, IllegalStateException {
    if (outputJar == null) {
      throw new IllegalStateException("output jar is null");
    } 
    putNextEntry(ze);
    return outputJar;
  } 
 
  /** 
   * Complete and flush the entry initiated by addOutputJarEntry. 
   */ 
  final public void endOutputJarEntry() throws IOException, IllegalStateException { 
    if (outputJar == null) {
      throw new IllegalStateException("output jar is null");
    } 
    outputJar.closeEntry();
  } 
 
  /** 
   * Call this to copy any unmodified classes to the output. This is called automatically by close(); you should only call this if 
   * you want to write an entry to the JAR file *after* the unmodified classes. This will only ever be called once per output JAR. 
   */ 
  final public void writeUnmodifiedClasses() throws IOException, IllegalStateException { 
    passUnmodifiedClasses = false;
    makeOutputJar(); 
    for (int i = 0; i < inputs.size(); i++) {
      Input in = inputs.get(i);
      if (!in.isClass()) {
        if (in instanceof JarInput) {
          JarInput jin = (JarInput) in;
          ZipEntry entry = jin.getEntry();
          InputStream s = jin.open();
          try { 
            ZipEntry newEntry = new ZipEntry(entry.getName());
            newEntry.setComment(entry.getComment());
            newEntry.setExtra(entry.getExtra());
            newEntry.setTime(entry.getTime());
            putNextEntry(newEntry);
            copyStream(s, outputJar);
            outputJar.closeEntry();
          } finally { 
            s.close();
          } 
        } else { 
          throw new Error("Unknown non-class input: " + in);
        } 
      } else { 
        String name = in.getClassName();
        if (name == null) {
          BufferedInputStream s = new BufferedInputStream(in.open(), 65536);
          try { 
            Object cl = makeClassFromStream(s);
            String entryName = toEntryName(getClassName(cl));
            if (!entryNames.contains(entryName)) {
              putNextEntry(new ZipEntry(entryName));
              BufferedOutputStream clOut = new BufferedOutputStream(outputJar);
              writeClassTo(cl, null, clOut);
              clOut.flush();
              outputJar.closeEntry();
            } 
          } finally { 
            s.close();
          } 
        } else { 
          String entryName = toEntryName(name);
          if (!entryNames.contains(entryName)) {
            BufferedInputStream s = new BufferedInputStream(in.open());
            try { 
              putNextEntry(new ZipEntry(entryName));
              BufferedOutputStream clOut = new BufferedOutputStream(outputJar);
              copyStream(s, clOut);
              clOut.flush();
              outputJar.closeEntry();
            } finally { 
              s.close();
            } 
          } 
        } 
      } 
    } 
  } 
 
  /** 
   * Call this when you're done modifying classes. 
   */ 
  final public void close() throws IOException, IllegalStateException { 
    if (passUnmodifiedClasses) {
      writeUnmodifiedClasses(); 
    } 
 
    if (outputJar != null) {
      outputJar.close();
    } 
 
    if (cachedJar != null) {
      cachedJar.close();
    } 
  } 
 
  private void putNextEntry(ZipEntry newEntry) throws IOException, IllegalStateException {
    if (outputJar == null) {
      throw new IllegalStateException();
    } 
    outputJar.putNextEntry(newEntry);
    entryNames.add(newEntry.getName());
    if (manifestBuilder != null) {
      manifestBuilder.addEntry(newEntry);
    } 
  }