KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > io > DataEntryCopier


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21 package proguard.io;
22
23 import proguard.util.*;
24
25 import java.io.*;
26
27
28 /**
29  * This DataEntryReader writes the ZIP entries and files that it reads to a
30  * given DataEntryWriter.
31  *
32  * @author Eric Lafortune
33  */

34 public class DataEntryCopier implements DataEntryReader
35 {
36     private static final int BUFFER_SIZE = 1024;
37
38     private DataEntryWriter dataEntryWriter;
39     private byte[] buffer = new byte[BUFFER_SIZE];
40
41
42
43     public DataEntryCopier(DataEntryWriter dataEntryWriter)
44     {
45         this.dataEntryWriter = dataEntryWriter;
46     }
47
48
49     // Implementations for DataEntryReader.
50

51     public void read(DataEntry dataEntry) throws IOException
52     {
53         try
54         {
55             // Get the output entry corresponding to this input entry.
56
OutputStream outputStream = dataEntryWriter.getOutputStream(dataEntry);
57             if (outputStream != null)
58             {
59                 InputStream inputStream = dataEntry.getInputStream();
60
61                 // Copy the data from the input entry to the output entry.
62
copyData(inputStream, outputStream);
63
64                 // Close the data entries.
65
dataEntry.closeInputStream();
66             }
67         }
68         catch (IOException ex)
69         {
70             System.err.println("Warning: can't write resource [" + dataEntry.getName() + "] (" + ex.getMessage() + ")");
71         }
72     }
73
74
75     // Small utility methods.
76

77     /**
78      * Copies all data that it can read from the given input stream to the
79      * given output stream.
80      */

81     private void copyData(InputStream inputStream,
82                           OutputStream outputStream)
83     throws IOException
84     {
85         while (true)
86         {
87             int count = inputStream.read(buffer);
88             if (count < 0)
89             {
90                 break;
91             }
92             outputStream.write(buffer, 0, count);
93         }
94
95         outputStream.flush();
96     }
97
98
99     /**
100      * A main method for testing file/jar/war/directory copying.
101      */

102     public static void main(String JavaDoc[] args)
103     {
104         try
105         {
106             String JavaDoc input = args[0];
107             String JavaDoc output = args[1];
108
109             boolean outputIsJar = output.endsWith(".jar");
110             boolean outputIsWar = output.endsWith(".war");
111             boolean outputIsEar = output.endsWith(".ear");
112             boolean outputIsZip = output.endsWith(".zip");
113
114             DataEntryWriter writer = new DirectoryWriter(new File(output),
115                                                          outputIsJar ||
116                                                          outputIsWar ||
117                                                          outputIsEar ||
118                                                          outputIsZip);
119
120         if (!outputIsJar)
121         {
122             // Zip up any zips, if necessary.
123
DataEntryWriter zipWriter = new JarWriter(writer);
124             if (outputIsZip)
125             {
126                 // Always zip.
127
writer = zipWriter;
128             }
129             else
130             {
131                 // Only zip up zips.
132
writer = new FilteredDataEntryWriter(new DataEntryParentFilter(
133                                                      new DataEntryNameFilter(
134                                                      new ExtensionMatcher(".zip"))),
135                                                      zipWriter,
136                                                      writer);
137             }
138
139             // Zip up any wars, if necessary.
140
DataEntryWriter warWriter = new JarWriter(writer);
141             if (outputIsWar)
142             {
143                 // Always zip.
144
writer = warWriter;
145             }
146             else
147             {
148                 // Only zip up wars.
149
writer = new FilteredDataEntryWriter(new DataEntryParentFilter(
150                                                      new DataEntryNameFilter(
151                                                      new ExtensionMatcher(".war"))),
152                                                      warWriter,
153                                                      writer);
154             }
155         }
156
157         // Zip up any jars, if necessary.
158
DataEntryWriter jarWriter = new JarWriter(writer);
159         if (outputIsJar)
160         {
161             // Always zip.
162
writer = jarWriter;
163         }
164         else
165         {
166             // Only zip up jars.
167
writer = new FilteredDataEntryWriter(new DataEntryParentFilter(
168                                                  new DataEntryNameFilter(
169                                                  new ExtensionMatcher(".jar"))),
170                                                  jarWriter,
171                                                  writer);
172         }
173
174
175             // Create the copying DataEntryReader.
176
DataEntryReader reader = new DataEntryCopier(writer);
177
178
179             boolean inputIsJar = input.endsWith(".jar");
180             boolean inputIsWar = input.endsWith(".war");
181             boolean inputIsZip = input.endsWith(".zip");
182
183             // Unzip any jars, if necessary.
184
DataEntryReader jarReader = new JarReader(reader);
185             if (inputIsJar)
186             {
187                 // Always unzip.
188
reader = jarReader;
189             }
190             else
191             {
192                 // Only unzip jar entries.
193
reader = new FilteredDataEntryReader(new DataEntryNameFilter(
194                                                      new ExtensionMatcher(".jar")),
195                                                      jarReader,
196                                                      reader);
197
198                 // Unzip any wars, if necessary.
199
DataEntryReader warReader = new JarReader(reader);
200                 if (inputIsWar)
201                 {
202                     // Always unzip.
203
reader = warReader;
204                 }
205                 else
206                 {
207                     // Only unzip war entries.
208
reader = new FilteredDataEntryReader(new DataEntryNameFilter(
209                                                          new ExtensionMatcher(".war")),
210                                                          warReader,
211                                                          reader);
212                 }
213
214                 // Unzip any zips, if necessary.
215
DataEntryReader zipReader = new JarReader(reader);
216                 if (inputIsZip)
217                 {
218                     // Always unzip.
219
reader = zipReader;
220                 }
221                 else
222                 {
223                     // Only unzip zip entries.
224
reader = new FilteredDataEntryReader(new DataEntryNameFilter(
225                                                          new ExtensionMatcher(".zip")),
226                                                          zipReader,
227                                                          reader);
228                 }
229             }
230
231             DirectoryPump directoryReader = new DirectoryPump(new File(input));
232
233             directoryReader.pumpDataEntries(reader);
234
235             writer.close();
236         }
237         catch (Exception JavaDoc ex)
238         {
239             ex.printStackTrace();
240         }
241     }
242 }
243
Popular Tags