1 17 18 package org.sape.carbon.core.util.jar; 19 20 import java.io.ByteArrayOutputStream ; 21 import java.io.File ; 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.util.Enumeration ; 26 import java.util.jar.JarEntry ; 27 import java.util.jar.JarOutputStream ; 28 29 40 public class JarEntryOutputStream extends ByteArrayOutputStream { 41 42 private EnhancedJarFile jar; 43 private String jarEntryName; 44 45 51 public JarEntryOutputStream( 52 EnhancedJarFile jar, 53 String jarEntryName) { 54 super(); 55 56 this.jarEntryName = jarEntryName; 57 this.jar = jar; 58 } 59 60 63 public void close() throws IOException { 64 writeToJar(); 65 super.close(); 66 } 67 68 76 private void writeToJar() throws IOException { 77 78 File jarDir = new File (this.jar.getName()).getParentFile(); 79 File newJarFile = File.createTempFile("config", ".jar", jarDir); 81 newJarFile.deleteOnExit(); 82 JarOutputStream jarOutputStream = 83 new JarOutputStream (new FileOutputStream (newJarFile)); 84 85 try { 86 Enumeration entries = this.jar.entries(); 87 88 while (entries.hasMoreElements()) { 90 JarEntry nextEntry = (JarEntry ) entries.nextElement(); 91 if (!this.jarEntryName.equals(nextEntry.getName())) { 93 JarEntry entryCopy = new JarEntry (nextEntry); 97 entryCopy.setCompressedSize(-1); 98 jarOutputStream.putNextEntry(entryCopy); 99 100 InputStream intputStream = 101 this.jar.getInputStream(nextEntry); 102 for (int data = intputStream.read(); 104 data != -1; 105 data = intputStream.read()) { 106 107 jarOutputStream.write(data); 108 } 109 } 110 } 111 112 if (size() > 0) { 114 jarOutputStream.putNextEntry(new JarEntry (this.jarEntryName)); 115 jarOutputStream.write(super.buf, 0, size()); 116 jarOutputStream.closeEntry(); 117 } 118 } finally { 119 try { 121 if (jarOutputStream != null) { 122 jarOutputStream.close(); 123 } 124 } catch (IOException ioe) { 125 } 127 } 128 129 this.jar.swapJars(newJarFile); 131 } 132 133 } | Popular Tags |