KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > TR > Impl > BundleOutputStream


1 /**
2  * BundleOutputStream.java is a part of the SOFA project.
3  * This file was created by pepan on 21.3.2003.
4  */

5 package SOFA.SOFAnode.TR.Impl;
6
7 import java.io.File JavaDoc;
8 import java.io.FileInputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.OutputStream JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.Hashtable JavaDoc;
13 import java.util.jar.JarEntry JavaDoc;
14 import java.util.jar.JarOutputStream JavaDoc;
15
16 import SOFA.Util.Tools;
17
18 /**
19  * A stream used for saving the contents of a bundle. It saves the bundle to a zipped
20  * stream with a manifest file (JAR file).
21  * @author Petr Panuska
22  */

23 class BundleOutputStream extends JarOutputStream JavaDoc {
24
25   /**
26    * Creates this stream.
27    * @param out the output stream lying under this one.
28    * @throws IOException when some IO operation fails.
29    */

30   public BundleOutputStream (OutputStream JavaDoc out) throws IOException JavaDoc {
31     super(out);
32   }
33
34   /**
35    * Puts the next bundle entry (all files of a component) to this stream.
36    * @param be the bundle entry being written to this stream.
37    * @throws IOException when some IO operation fails.
38    */

39   public void putNextBundleEntry (BundleEntry be) throws IOException JavaDoc {
40     Hashtable JavaDoc files = be.getFiles().getFiles();
41
42     Enumeration JavaDoc paths = files.keys();
43     while (paths.hasMoreElements()) {
44       String JavaDoc path = (String JavaDoc) paths.nextElement(); // the path in the jar file
45
File JavaDoc file = (File JavaDoc) files.get(path); // the file in SOFAnode's file system
46
long fileLength = file.length();
47
48       path = path.replace('\\', '/'); // if there were some backslashes, in the JarEntry it must be a slash
49
JarEntry JavaDoc jarEntry = new JarEntry JavaDoc(be.getName() + path); //location serves here as a prefix of path in a jar file!!!
50
jarEntry.setSize(fileLength);
51       jarEntry.setTime(file.lastModified()); // keep original time of creation
52
putNextEntry(jarEntry);
53
54       FileInputStream JavaDoc fileStream = new FileInputStream JavaDoc(file);
55
56       Tools.copyStream(fileStream, this);
57
58       fileStream.close();
59       closeEntry();
60     }
61   }
62
63 }
64
Popular Tags