KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonicsystems > jarjar > util > StandaloneJarProcessor


1 /*
2   Jar Jar Links - A utility to repackage and embed Java libraries
3   Copyright (C) 2004 Tonic Systems, Inc.
4
5   This program is free software; you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation; either version 2 of the License, or
8   (at your option) any later version.
9
10   This program is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program; see the file COPYING. if not, write to
17   the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18   Boston, MA 02111-1307 USA
19 */

20
21 package com.tonicsystems.jarjar.util;
22
23 import java.util.jar.JarEntry JavaDoc;
24 import java.util.jar.JarFile JavaDoc;
25 import java.util.jar.JarOutputStream JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.zip.ZipOutputStream JavaDoc;
28 import java.io.*;
29
30 public class StandaloneJarProcessor
31 {
32     public static void run(File JavaDoc from, File JavaDoc to, JarProcessor proc) throws IOException {
33         JarFile JavaDoc in = new JarFile JavaDoc(from);
34         JarOutputStream JavaDoc out = new JarOutputStream JavaDoc(new FileOutputStream(to));
35         byte[] buf = new byte[0x2000];
36         EntryStruct struct = new EntryStruct();
37         Enumeration JavaDoc e = in.entries();
38         while (e.hasMoreElements()) {
39             JarEntry JavaDoc entry = (JarEntry JavaDoc)e.nextElement();
40             struct.in = in.getInputStream(entry);
41             struct.name = entry.getName();
42             struct.time = entry.getTime();
43             struct.file = from;
44             if (proc.process(struct)) {
45                 entry = new JarEntry JavaDoc(struct.name);
46                 entry.setTime(struct.time);
47                 entry.setCompressedSize(-1);
48                 out.putNextEntry(entry);
49                 pipe(struct.in, out, buf);
50                 struct.in.close();
51             }
52         }
53         out.close();
54         out = null;
55     }
56
57     private static void pipe(InputStream is, OutputStream JavaDoc out, byte[] buf) throws IOException {
58         for (;;) {
59             int amt = is.read(buf);
60             if (amt < 0)
61                 break;
62             out.write(buf, 0, amt);
63         }
64     }
65 }
66
Popular Tags