1 20 package org.apache.cactus.integration.ant.deployment; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import java.util.jar.JarInputStream ; 31 import java.util.zip.ZipEntry ; 32 33 39 public class DefaultJarArchive implements JarArchive 40 { 41 43 46 private byte content[]; 47 48 50 56 public DefaultJarArchive(File theFile) 57 throws IOException 58 { 59 this(new FileInputStream (theFile)); 60 } 61 62 69 public DefaultJarArchive(InputStream theInputStream) 70 throws IOException 71 { 72 try 73 { 74 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 75 byte[] buffer = new byte[2048]; 76 int bytesRead = -1; 77 while ((bytesRead = theInputStream.read(buffer)) != -1) 78 { 79 baos.write(buffer, 0, bytesRead); 80 } 81 this.content = baos.toByteArray(); 82 } 83 finally 84 { 85 if (theInputStream != null) 86 { 87 theInputStream.close(); 88 } 89 } 90 } 91 92 94 97 public boolean containsClass(String theClassName) 98 throws IOException 99 { 100 String resourceName = theClassName.replace('.', '/') + ".class"; 101 return (getResource(resourceName) != null); 102 } 103 104 107 public final String findResource(String theName) 108 throws IOException 109 { 110 JarInputStream in = null; 111 try 112 { 113 in = new JarInputStream (getContentAsStream()); 114 ZipEntry entry = null; 115 while ((entry = in.getNextEntry()) != null) 116 { 117 String entryName = entry.getName(); 118 int lastSlashIndex = entryName.lastIndexOf('/'); 119 if (lastSlashIndex >= 0) 120 { 121 entryName = entryName.substring(lastSlashIndex + 1); 122 } 123 if (entryName.equals(theName)) 124 { 125 return entry.getName(); 126 } 127 } 128 } 129 finally 130 { 131 if (in != null) 132 { 133 in.close(); 134 } 135 } 136 return null; 137 } 138 139 142 public final InputStream getResource(String thePath) 143 throws IOException 144 { 145 JarInputStream in = null; 146 try 147 { 148 in = getContentAsStream(); 149 ZipEntry zipEntry = null; 150 while ((zipEntry = in.getNextEntry()) != null) 151 { 152 if (thePath.equals(zipEntry.getName())) 153 { 154 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 155 byte bytes[] = new byte[2048]; 156 int bytesRead = -1; 157 while ((bytesRead = in.read(bytes)) != -1) 158 { 159 buffer.write(bytes, 0, bytesRead); 160 } 161 return new ByteArrayInputStream (buffer.toByteArray()); 162 } 163 } 164 } 165 finally 166 { 167 if (in != null) 168 { 169 in.close(); 170 } 171 } 172 return null; 173 } 174 175 178 public final List getResources(String thePath) 179 throws IOException 180 { 181 List resources = new ArrayList (); 182 JarInputStream in = null; 183 try 184 { 185 in = getContentAsStream(); 186 ZipEntry zipEntry = null; 187 while ((zipEntry = in.getNextEntry()) != null) 188 { 189 if ((zipEntry.getName().startsWith(thePath) 190 && !zipEntry.getName().equals(thePath))) 191 { 192 resources.add(zipEntry.getName()); 193 } 194 } 195 } 196 finally 197 { 198 if (in != null) 199 { 200 in.close(); 201 } 202 } 203 return resources; 204 } 205 206 208 214 protected final JarInputStream getContentAsStream() 215 throws IOException 216 { 217 return new JarInputStream (new ByteArrayInputStream (this.content)); 218 } 219 } 220 | Popular Tags |