KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > deploy > shared > MemoryMappedArchive


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * MemoryMappedArchive.java
26  *
27  * Created on September 6, 2002, 2:58 PM
28  */

29
30 package com.sun.enterprise.deployment.deploy.shared;
31
32 import java.io.InputStream JavaDoc;
33 import java.io.OutputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.ByteArrayInputStream JavaDoc;
36 import java.io.ByteArrayOutputStream JavaDoc;
37 import java.io.BufferedInputStream JavaDoc;
38 import java.io.BufferedOutputStream JavaDoc;
39 import java.io.File JavaDoc;
40 import java.io.FileInputStream JavaDoc;
41 import java.io.FileNotFoundException JavaDoc;
42 import java.net.URI JavaDoc;
43
44 import java.util.Vector JavaDoc;
45 import java.util.Enumeration JavaDoc;
46
47 import java.util.jar.Manifest JavaDoc;
48 import java.util.zip.ZipEntry JavaDoc;
49 import java.util.jar.JarInputStream JavaDoc;
50 import java.util.jar.JarOutputStream JavaDoc;
51
52 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive;
53 import com.sun.enterprise.util.shared.ArchivistUtils;
54
55 /**
56  *
57  * @author Jerome Dochez
58  */

59 public class MemoryMappedArchive extends AbstractArchive {
60     
61     byte[] file;
62     
63     /** Creates a new instance of MemoryMappedArchive */
64     protected MemoryMappedArchive() {
65     // for use by subclasses
66
}
67
68     /** Creates a new instance of MemoryMappedArchive */
69     public MemoryMappedArchive(InputStream JavaDoc is) throws IOException JavaDoc {
70         read(is);
71     }
72
73     public byte[] getByteArray() {
74         return file;
75     }
76     
77     private void read(InputStream JavaDoc is) throws IOException JavaDoc{
78         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
79         ArchivistUtils.copy(new BufferedInputStream JavaDoc(is), new BufferedOutputStream JavaDoc(baos));
80         file = baos.toByteArray();
81         
82     }
83     
84     public void open(String JavaDoc path) throws IOException JavaDoc {
85         File JavaDoc in = new File JavaDoc(path);
86         if (!in.exists()) {
87             throw new FileNotFoundException JavaDoc(path);
88         }
89         FileInputStream JavaDoc is = new FileInputStream JavaDoc(in);
90         read(is);
91     }
92     
93     // copy constructor
94
public MemoryMappedArchive(AbstractArchive source) throws IOException JavaDoc {
95         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
96         JarOutputStream JavaDoc jos = new JarOutputStream JavaDoc(new BufferedOutputStream JavaDoc(baos));
97         for (Enumeration JavaDoc elements = source.entries();elements.hasMoreElements();) {
98             String JavaDoc elementName = (String JavaDoc) elements.nextElement();
99             InputStream JavaDoc is = source.getEntry(elementName);
100             jos.putNextEntry(new ZipEntry JavaDoc(elementName));
101             ArchivistUtils.copyWithoutClose(is, jos);
102             is.close();
103             jos.flush();
104             jos.closeEntry();
105         }
106         jos.close();
107         file = baos.toByteArray();
108     }
109     
110     /**
111      * @returns an @see java.io.OutputStream for a new entry in this
112      * current abstract archive.
113      * @param the entry name
114      */

115     public OutputStream JavaDoc putNextEntry(String JavaDoc name) throws IOException JavaDoc {
116         return null;
117     }
118     
119     /**
120      * close the abstract archive
121      */

122     public void close() throws IOException JavaDoc {
123     }
124     
125     /**
126      * close a previously returned @see java.io.OutputStream returned
127      * by an addEntry call
128      *
129      * @param the output stream to close
130      */

131     public void closeEntry(AbstractArchive os) throws IOException JavaDoc {
132     }
133     
134     /**
135      * close a previously returned @see java.io.OutputStream returned
136      * by an addEntry call
137      */

138     public void closeEntry() throws IOException JavaDoc {
139     }
140     
141     /**
142      * delete the archive
143      */

144     public boolean delete() {
145         return false;
146     }
147     
148     /**
149      * @return an @see java.util.Enumeration of entries in this abstract
150      * archive
151      */

152     public Enumeration JavaDoc entries() {
153         Vector JavaDoc entries = new Vector JavaDoc();
154         try {
155             JarInputStream JavaDoc jis = new JarInputStream JavaDoc(new ByteArrayInputStream JavaDoc(file));
156             ZipEntry JavaDoc ze;
157             while ((ze=jis.getNextEntry())!=null) {
158                 entries.add(ze.getName());
159             }
160             jis.close();
161         } catch(IOException JavaDoc ioe) {
162             ioe.printStackTrace();
163         }
164         return entries.elements();
165     }
166     
167     /**
168      * @return an @see java.util.Enumeration of entries in this abstract
169      * archive, providing the list of embedded archive to not count their
170      * entries as part of this archive
171      */

172      public Enumeration JavaDoc entries(Enumeration JavaDoc embeddedArchives) {
173     // jar file are not recursive
174
return entries();
175     }
176     
177     /**
178      * @return true if this archive exists
179      */

180     public boolean exists() {
181         return false;
182     }
183     
184     /**
185      * @return the archive uri
186      */

187     public String JavaDoc getArchiveUri() {
188         return null;
189     }
190     
191     /**
192      * Get the size of the archive
193      * @return tje the size of this archive or -1 on error
194      */

195     public long getArchiveSize() throws NullPointerException JavaDoc, SecurityException JavaDoc {
196         return(file.length);
197     }
198     
199     public URI JavaDoc getURI() {
200         return null;
201     }
202     
203     /**
204      * create or obtain an embedded archive within this abstraction.
205      *
206      * @param the name of the embedded archive.
207      */

208     public AbstractArchive getEmbeddedArchive(String JavaDoc name) throws IOException JavaDoc {
209         InputStream JavaDoc is = getEntry(name);
210         if (is!=null) {
211             AbstractArchive archive = new MemoryMappedArchive(is);
212             is.close();
213             return archive;
214         }
215         return null;
216     }
217     
218     /**
219      * @return a @see java.io.InputStream for an existing entry in
220      * the current abstract archive
221      * @param the entry name
222      */

223     public InputStream JavaDoc getEntry(String JavaDoc name) throws IOException JavaDoc {
224         JarInputStream JavaDoc jis = new JarInputStream JavaDoc(new ByteArrayInputStream JavaDoc(file));
225         ZipEntry JavaDoc ze;
226         while ((ze=jis.getNextEntry())!=null) {
227             if (ze.getName().equals(name))
228                 return new BufferedInputStream JavaDoc(jis);
229         }
230         return null;
231     }
232     
233     /**
234      * @return the manifest information for this abstract archive
235      */

236     public Manifest JavaDoc getManifest() throws IOException JavaDoc {
237         JarInputStream JavaDoc jis = new JarInputStream JavaDoc(new ByteArrayInputStream JavaDoc(file));
238         Manifest JavaDoc m = jis.getManifest();
239         jis.close();
240         return m;
241     }
242     
243     /**
244      * rename the archive
245      *
246      * @param name the archive name
247      */

248     public boolean renameTo(String JavaDoc name) {
249         return false;
250     }
251     
252 }
253
Popular Tags