KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > util > jar > JarEntryOutputStream


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.util.jar;
19
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStream JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.jar.JarEntry JavaDoc;
27 import java.util.jar.JarOutputStream JavaDoc;
28
29 /**
30  * An output stream that is used by EnhancedJarFile to write entries to a jar.
31  * This implementation uses a ByteArrayOutputStream to buffer the output
32  * until the stream is closed. When the stream is closed, the output is written
33  * to the jar.
34  *
35  * Copyright 2002 Sapient
36  * @since carbon 1.0
37  * @author Douglas Voet, April 2002
38  * @version $Revision: 1.9 $($Author: dvoet $ / $Date: 2003/05/05 21:21:23 $)
39  */

40 public class JarEntryOutputStream extends ByteArrayOutputStream JavaDoc {
41
42     private EnhancedJarFile jar;
43     private String JavaDoc jarEntryName;
44
45     /**
46      * Constructor
47      *
48      * @param jar the EnhancedJarFile that this instance will write to
49      * @param jarEntryName the name of the entry to be written
50      */

51     public JarEntryOutputStream(
52         EnhancedJarFile jar,
53         String JavaDoc jarEntryName) {
54         super();
55
56         this.jarEntryName = jarEntryName;
57         this.jar = jar;
58     }
59
60     /**
61      * Closes the stream and writes entry to the jar
62      */

63     public void close() throws IOException JavaDoc {
64         writeToJar();
65         super.close();
66     }
67
68     /**
69      * Writes the entry to a the jar file. This is done by creating a
70      * temporary jar file, copying the contents of the existing jar to the
71      * temp jar, skipping the entry named by this.jarEntryName if it exists.
72      * Then, if the stream was written to, then contents are written as a
73      * new entry. Last, a callback is made to the EnhancedJarFile to
74      * swap the temp jar in for the old jar.
75      */

76     private void writeToJar() throws IOException JavaDoc {
77
78         File JavaDoc jarDir = new File JavaDoc(this.jar.getName()).getParentFile();
79         // create new jar
80
File JavaDoc newJarFile = File.createTempFile("config", ".jar", jarDir);
81         newJarFile.deleteOnExit();
82         JarOutputStream JavaDoc jarOutputStream =
83             new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(newJarFile));
84
85         try {
86             Enumeration JavaDoc entries = this.jar.entries();
87
88             // copy all current entries into the new jar
89
while (entries.hasMoreElements()) {
90                 JarEntry JavaDoc nextEntry = (JarEntry JavaDoc) entries.nextElement();
91                 // skip the entry named jarEntryName
92
if (!this.jarEntryName.equals(nextEntry.getName())) {
93                     // the next 3 lines of code are a work around for
94
// bug 4682202 in the java.sun.com bug parade, see:
95
// http://developer.java.sun.com/developer/bugParade/bugs/4682202.html
96
JarEntry JavaDoc entryCopy = new JarEntry JavaDoc(nextEntry);
97                     entryCopy.setCompressedSize(-1);
98                     jarOutputStream.putNextEntry(entryCopy);
99
100                     InputStream JavaDoc intputStream =
101                         this.jar.getInputStream(nextEntry);
102                     // write the data
103
for (int data = intputStream.read();
104                         data != -1;
105                         data = intputStream.read()) {
106
107                         jarOutputStream.write(data);
108                     }
109                 }
110             }
111
112             // write the new or modified entry to the jar
113
if (size() > 0) {
114                 jarOutputStream.putNextEntry(new JarEntry JavaDoc(this.jarEntryName));
115                 jarOutputStream.write(super.buf, 0, size());
116                 jarOutputStream.closeEntry();
117             }
118         } finally {
119             // close close everything up
120
try {
121                 if (jarOutputStream != null) {
122                     jarOutputStream.close();
123                 }
124             } catch (IOException JavaDoc ioe) {
125                 // eat it, just wanted to close stream
126
}
127         }
128
129         // swap the jar
130
this.jar.swapJars(newJarFile);
131     }
132
133 }
Popular Tags