KickJava   Java API By Example, From Geeks To Geeks.

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


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

5 package SOFA.SOFAnode.TR.Impl;
6
7 import java.io.File JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.util.Date JavaDoc;
12 import java.util.jar.JarEntry JavaDoc;
13 import java.util.jar.JarInputStream JavaDoc;
14
15 import SOFA.Util.Tools;
16
17 /**
18  * Stores bundle entries (component name, implementation version and all necessary files) into
19  * a zipped stream. Actually, it looks like a JAR file with the <code>MANIFEST.INF</code> file.
20  * However, each entry in the manifest file describes rather one component (a few files)
21  * than a single file.
22  *
23  * @author Petr Panuska
24  */

25 class BundleInputStream extends JarInputStream JavaDoc {
26
27   /**
28    * A directory to store component files being unpacked from this stream (a zipped jar file).
29    * This directory (including all files included) is deleted when the associated bundle
30    * is going to be garbage collected.
31    * @see BundleImpl#finalize
32    */

33   private File JavaDoc tempDir;
34
35   /**
36    * Last JAR entry read from this stream.
37    */

38   private JarEntry JavaDoc lastEntry;
39
40   /**
41    * The name of a JAR entry that has been read last.
42    */

43   private String JavaDoc lastName;
44
45   /**
46    * Has the end of a file been reached?
47    */

48   private boolean eof;
49
50   /**
51    * Prefix (characters before the <code>"/"</code>) of a JAR entry name
52    * that has been read last from this stream.
53    */

54   private String JavaDoc lastPrefix;
55
56   /**
57    * Creates the input stream for storing bundle entries. It stores component files
58    * (described by entries) into a temporarily created directory.
59    * @param in the underlying stream.
60    * @throws IOException when some IO operation fails.
61    */

62   public BundleInputStream (InputStream JavaDoc in) throws IOException JavaDoc {
63     super(in, false);
64     tempDir = new File JavaDoc(System.getProperty("java.io.tmpdir") + File.separator + "sofa" + Long.toString(new Date JavaDoc().getTime()) + ".bar");
65     tempDir.mkdir();
66     lastEntry = null;
67     lastPrefix = null;
68     lastName = null;
69     eof = false;
70   }
71
72   /**
73    * Returns the next bundle entry (a component) from this stream.
74    * <p><b>NB</b>: Don't call the {@link #closeEntry} and {@link #read} methods!! This method already
75    * reads data and closes entries.
76    * @return the next bundle entry or <code>null</code> when the end of stream has been reached.
77    * @throws IOException when some IO operation fails.
78    */

79   public BundleEntry getNextBundleEntry () throws IOException JavaDoc {
80     if (eof) // end of stream has been reached
81
return null;
82     if (lastEntry == null) { // this method has been called for the first time
83
lastEntry = getNextJarEntry();
84       if (lastEntry == null) { // end of stream has been reached
85
eof = true;
86         return null;
87       }
88       lastName = lastEntry.getName(); // name of the JAR entry;
89
lastPrefix = lastName.substring(0, lastName.indexOf('/')); // prefix of the name
90
}
91     BundleEntry bundleEntry = new BundleEntry(lastPrefix);
92     for (; ;) {
93       String JavaDoc path = lastName.substring(0, lastName.lastIndexOf("/")); // the name without suffix
94
path = path.replace('/', File.separatorChar); // change the slash to the right file separator
95
File JavaDoc file = new File JavaDoc(tempDir, path);
96       file.mkdirs();
97       file = new File JavaDoc(tempDir, lastName);
98       FileOutputStream JavaDoc outFile = new FileOutputStream JavaDoc(file);
99
100       Tools.copyStream(this, outFile);
101
102       super.closeEntry();
103       outFile.close();
104       file.setLastModified(lastEntry.getTime()); // keep the original time of creation
105
String JavaDoc hashName = lastName.substring(lastPrefix.length()); // rest of the name
106
bundleEntry.addFile(file, hashName);
107       lastEntry = getNextJarEntry();
108       if (lastEntry == null) { // end of stream has been reached
109
eof = true;
110         return bundleEntry;
111       }
112       lastName = lastEntry.getName(); // name of the JAR entry;
113
String JavaDoc newPrefix = lastName.substring(0, lastName.indexOf('/')); // prefix of the name
114
if (!lastPrefix.equals(newPrefix)) {
115         lastPrefix = newPrefix;
116         return bundleEntry;
117       }
118     }
119   }
120
121   /**
122    * Returns the root directory created temporarily for storing files of all components
123    * being written to this input stream.
124    * @return the temporary directory where component files stored in this stream are
125    * written in.
126    */

127   File JavaDoc getTempRootDirectory () {
128     return tempDir;
129   }
130 }
131
Popular Tags