1 23 24 29 30 package com.sun.enterprise.deployment.deploy.shared; 31 32 import java.io.InputStream ; 33 import java.io.OutputStream ; 34 import java.io.IOException ; 35 import java.io.ByteArrayInputStream ; 36 import java.io.ByteArrayOutputStream ; 37 import java.io.BufferedInputStream ; 38 import java.io.BufferedOutputStream ; 39 import java.io.File ; 40 import java.io.FileInputStream ; 41 import java.io.FileNotFoundException ; 42 import java.net.URI ; 43 44 import java.util.Vector ; 45 import java.util.Enumeration ; 46 47 import java.util.jar.Manifest ; 48 import java.util.zip.ZipEntry ; 49 import java.util.jar.JarInputStream ; 50 import java.util.jar.JarOutputStream ; 51 52 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive; 53 import com.sun.enterprise.util.shared.ArchivistUtils; 54 55 59 public class MemoryMappedArchive extends AbstractArchive { 60 61 byte[] file; 62 63 64 protected MemoryMappedArchive() { 65 } 67 68 69 public MemoryMappedArchive(InputStream is) throws IOException { 70 read(is); 71 } 72 73 public byte[] getByteArray() { 74 return file; 75 } 76 77 private void read(InputStream is) throws IOException { 78 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 79 ArchivistUtils.copy(new BufferedInputStream (is), new BufferedOutputStream (baos)); 80 file = baos.toByteArray(); 81 82 } 83 84 public void open(String path) throws IOException { 85 File in = new File (path); 86 if (!in.exists()) { 87 throw new FileNotFoundException (path); 88 } 89 FileInputStream is = new FileInputStream (in); 90 read(is); 91 } 92 93 public MemoryMappedArchive(AbstractArchive source) throws IOException { 95 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 96 JarOutputStream jos = new JarOutputStream (new BufferedOutputStream (baos)); 97 for (Enumeration elements = source.entries();elements.hasMoreElements();) { 98 String elementName = (String ) elements.nextElement(); 99 InputStream is = source.getEntry(elementName); 100 jos.putNextEntry(new ZipEntry (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 115 public OutputStream putNextEntry(String name) throws IOException { 116 return null; 117 } 118 119 122 public void close() throws IOException { 123 } 124 125 131 public void closeEntry(AbstractArchive os) throws IOException { 132 } 133 134 138 public void closeEntry() throws IOException { 139 } 140 141 144 public boolean delete() { 145 return false; 146 } 147 148 152 public Enumeration entries() { 153 Vector entries = new Vector (); 154 try { 155 JarInputStream jis = new JarInputStream (new ByteArrayInputStream (file)); 156 ZipEntry ze; 157 while ((ze=jis.getNextEntry())!=null) { 158 entries.add(ze.getName()); 159 } 160 jis.close(); 161 } catch(IOException ioe) { 162 ioe.printStackTrace(); 163 } 164 return entries.elements(); 165 } 166 167 172 public Enumeration entries(Enumeration embeddedArchives) { 173 return entries(); 175 } 176 177 180 public boolean exists() { 181 return false; 182 } 183 184 187 public String getArchiveUri() { 188 return null; 189 } 190 191 195 public long getArchiveSize() throws NullPointerException , SecurityException { 196 return(file.length); 197 } 198 199 public URI getURI() { 200 return null; 201 } 202 203 208 public AbstractArchive getEmbeddedArchive(String name) throws IOException { 209 InputStream 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 223 public InputStream getEntry(String name) throws IOException { 224 JarInputStream jis = new JarInputStream (new ByteArrayInputStream (file)); 225 ZipEntry ze; 226 while ((ze=jis.getNextEntry())!=null) { 227 if (ze.getName().equals(name)) 228 return new BufferedInputStream (jis); 229 } 230 return null; 231 } 232 233 236 public Manifest getManifest() throws IOException { 237 JarInputStream jis = new JarInputStream (new ByteArrayInputStream (file)); 238 Manifest m = jis.getManifest(); 239 jis.close(); 240 return m; 241 } 242 243 248 public boolean renameTo(String name) { 249 return false; 250 } 251 252 } 253 | Popular Tags |