You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-dev@jakarta.apache.org by cm...@apache.org on 2003/05/14 15:11:56 UTC
cvs commit: jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment JarArchive.java
cmlenz 2003/05/14 06:11:56
Modified: integration/ant/src/java/org/apache/cactus/integration/ant/deployment
JarArchive.java
Log:
- Close the InputStream passed into the constructor
- Add a method for getting an InputStream to an arbritrary resource in the archive
Revision Changes Path
1.2 +61 -9 jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/JarArchive.java
Index: JarArchive.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/integration/ant/src/java/org/apache/cactus/integration/ant/deployment/JarArchive.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- JarArchive.java 14 May 2003 12:23:11 -0000 1.1
+++ JarArchive.java 14 May 2003 13:11:56 -0000 1.2
@@ -89,7 +89,7 @@
/**
* Constructor.
*
- * @param theFile The web application archive
+ * @param theFile The archive file
* @throws IOException If there was a problem reading the WAR
*/
public JarArchive(File theFile)
@@ -101,20 +101,31 @@
/**
* Constructor.
*
- * @param theInputStream The input stream for the web application archive
+ * @param theInputStream The input stream for the archive (it will be closed
+ * after the constructor returns)
* @throws IOException If there was a problem reading the WAR
*/
public JarArchive(InputStream theInputStream)
throws IOException
{
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- byte[] buffer = new byte[2048];
- int bytesRead = -1;
- while ((bytesRead = theInputStream.read(buffer)) != -1)
+ try
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ byte[] buffer = new byte[2048];
+ int bytesRead = -1;
+ while ((bytesRead = theInputStream.read(buffer)) != -1)
+ {
+ baos.write(buffer, 0, bytesRead);
+ }
+ this.content = new ByteArrayInputStream(baos.toByteArray());
+ }
+ finally
{
- baos.write(buffer, 0, bytesRead);
+ if (theInputStream != null)
+ {
+ theInputStream.close();
+ }
}
- this.content = new ByteArrayInputStream(baos.toByteArray());
}
// Public Methods ----------------------------------------------------------
@@ -145,6 +156,47 @@
if (entryName.equals(theName))
{
return entry.getName();
+ }
+ }
+ }
+ finally
+ {
+ if (in != null)
+ {
+ in.close();
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Returns a resource from the archive as input stream.
+ *
+ * @param thePath The path to the resource in the archive
+ * @return An input stream containing the specified resource, or
+ * <code>null</code> if the resource was not found in the JAR
+ * @throws IOException If an I/O error occurs
+ */
+ public InputStream getResource(String thePath)
+ throws IOException
+ {
+ JarInputStream in = null;
+ try
+ {
+ in = getContentAsStream();
+ ZipEntry zipEntry = null;
+ while ((zipEntry = in.getNextEntry()) != null)
+ {
+ if (thePath.equals(zipEntry.getName()))
+ {
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ byte bytes[] = new byte[2048];
+ int bytesRead = -1;
+ while ((bytesRead = in.read(bytes)) != -1)
+ {
+ buffer.write(bytes, 0, bytesRead);
+ }
+ return new ByteArrayInputStream(buffer.toByteArray());
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: cactus-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: cactus-dev-help@jakarta.apache.org