KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > io > DataEntryRewriter


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.classfile.*;
24
25 import java.io.*;
26
27 /**
28  * This DataEntryReader writes the resource data entries that it reads to a
29  * given DataEntryWriter, updating their contents based on the renamed classes
30  * in the given ClassPool.
31  *
32  * @author Eric Lafortune
33  */

34 public class DataEntryRewriter implements DataEntryReader
35 {
36     private ClassPool classPool;
37     private DataEntryWriter dataEntryWriter;
38
39
40     /**
41      * Creates a new DataEntryRewriter.
42      */

43     public DataEntryRewriter(ClassPool classPool,
44                              DataEntryWriter dataEntryWriter)
45     {
46         this.classPool = classPool;
47         this.dataEntryWriter = dataEntryWriter;
48     }
49
50
51     // Implementations for DataEntryReader.
52

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

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

83     private void copyData(InputStream inputStream,
84                           OutputStream outputStream)
85     throws IOException
86     {
87         Reader reader = new BufferedReader(new InputStreamReader(inputStream));
88         Writer writer = new BufferedWriter(new OutputStreamWriter(outputStream));
89
90         StringBuffer JavaDoc word = new StringBuffer JavaDoc();
91
92         while (true)
93         {
94             int i = reader.read();
95             if (i < 0)
96             {
97                 break;
98             }
99
100             // Is the character part of a word?
101
char c = (char)i;
102             if (Character.isJavaIdentifierPart(c) ||
103                 c == '.' ||
104                 c == '-')
105             {
106                 // Collect the characters in this word.
107
word.append(c);
108             }
109             else
110             {
111                 // Write out the updated word, if any.
112
writeUpdatedWord(writer, word.toString());
113                 word.setLength(0);
114
115                 // Write out the character that terminated it.
116
writer.write(c);
117             }
118         }
119
120         // Write out the final word.
121
writeUpdatedWord(writer, word.toString());
122
123         writer.flush();
124         outputStream.flush();
125     }
126
127
128     /**
129      * Writes the given word to the given writer, after having adapted it,
130      * based on the renamed class names.
131      */

132     private void writeUpdatedWord(Writer writer, String JavaDoc word)
133     throws IOException
134     {
135         if (word.length() > 0)
136         {
137             String JavaDoc newWord = word;
138
139             boolean containsDots = word.indexOf('.') >= 0;
140
141             // Replace dots by forward slashes.
142
String JavaDoc className = containsDots ?
143                 word.replace('.', ClassConstants.INTERNAL_PACKAGE_SEPARATOR) :
144                 word;
145
146             // Find the class corrsponding to the word.
147
Clazz clazz = classPool.getClass(className);
148             if (clazz != null)
149             {
150                 // Update the word if necessary.
151
String JavaDoc newClassName = clazz.getName();
152                 if (!className.equals(newClassName))
153                 {
154                     // Replace forward slashes by dots.
155
newWord = containsDots ?
156                         newClassName.replace(ClassConstants.INTERNAL_PACKAGE_SEPARATOR, '.') :
157                         newClassName;
158                 }
159             }
160
161             writer.write(newWord);
162         }
163     }
164 }
165
Popular Tags