KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployment > spi > JarUtils


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.deployment.spi;
23
24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.jar.JarEntry JavaDoc;
31 import java.util.jar.JarInputStream JavaDoc;
32 import java.util.jar.JarOutputStream JavaDoc;
33
34 import org.jboss.logging.Logger;
35
36 /**
37  * A collection of jar utilities
38  *
39  * @author thomas.diesler@jboss.org
40  * @version $Revision: 38480 $
41  */

42 public class JarUtils
43 {
44    // deployment logging
45
private static final Logger log = Logger.getLogger(JarUtils.class);
46
47    /**
48     * Add jar contents to the deployment archive under the given prefix
49     */

50    public static String JavaDoc[] addJar(JarOutputStream JavaDoc outputStream, String JavaDoc prefix, File JavaDoc jar) throws IOException JavaDoc
51    {
52       log.trace("addJar: " + jar);
53       ArrayList JavaDoc tmp = new ArrayList JavaDoc();
54       FileInputStream JavaDoc fis = new FileInputStream JavaDoc(jar);
55       JarInputStream JavaDoc jis = new JarInputStream JavaDoc(fis);
56       JarEntry JavaDoc entry = jis.getNextJarEntry();
57       while (entry != null)
58       {
59          if (entry.isDirectory() == false)
60          {
61             String JavaDoc entryName = prefix + entry.getName();
62             tmp.add(entryName);
63             addJarEntry(outputStream, entryName, jis);
64          }
65          entry = jis.getNextJarEntry();
66       }
67       jis.close();
68       String JavaDoc[] names = new String JavaDoc[tmp.size()];
69       tmp.toArray(names);
70       return names;
71    }
72
73    /**
74     * Add a jar entry to the deployment archive
75     */

76    public static void addJarEntry(JarOutputStream JavaDoc outputStream, String JavaDoc entryName, InputStream JavaDoc inputStream) throws IOException JavaDoc
77    {
78       log.trace("addJarEntry: " + entryName);
79       outputStream.putNextEntry(new JarEntry JavaDoc(entryName));
80       copyStream(outputStream, inputStream);
81    }
82
83    /**
84     * Copies the input stream to the output stream
85     */

86    public static void copyStream(OutputStream JavaDoc outputStream, InputStream JavaDoc inputStream) throws IOException JavaDoc
87    {
88       byte[] bytes = new byte[4096];
89       int read = inputStream.read(bytes, 0, 4096);
90       while (read > 0)
91       {
92          outputStream.write(bytes, 0, read);
93          read = inputStream.read(bytes, 0, 4096);
94       }
95    }
96
97 }
98
Popular Tags