1 21 package proguard.io; 22 23 import proguard.classfile.*; 24 25 import java.io.*; 26 27 34 public class DataEntryRewriter implements DataEntryReader 35 { 36 private ClassPool classPool; 37 private DataEntryWriter dataEntryWriter; 38 39 40 43 public DataEntryRewriter(ClassPool classPool, 44 DataEntryWriter dataEntryWriter) 45 { 46 this.classPool = classPool; 47 this.dataEntryWriter = dataEntryWriter; 48 } 49 50 51 53 public void read(DataEntry dataEntry) throws IOException 54 { 55 try 56 { 57 OutputStream outputStream = dataEntryWriter.getOutputStream(dataEntry); 59 if (outputStream != null) 60 { 61 InputStream inputStream = dataEntry.getInputStream(); 62 63 copyData(inputStream, outputStream); 65 66 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 79 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 word = new StringBuffer (); 91 92 while (true) 93 { 94 int i = reader.read(); 95 if (i < 0) 96 { 97 break; 98 } 99 100 char c = (char)i; 102 if (Character.isJavaIdentifierPart(c) || 103 c == '.' || 104 c == '-') 105 { 106 word.append(c); 108 } 109 else 110 { 111 writeUpdatedWord(writer, word.toString()); 113 word.setLength(0); 114 115 writer.write(c); 117 } 118 } 119 120 writeUpdatedWord(writer, word.toString()); 122 123 writer.flush(); 124 outputStream.flush(); 125 } 126 127 128 132 private void writeUpdatedWord(Writer writer, String word) 133 throws IOException 134 { 135 if (word.length() > 0) 136 { 137 String newWord = word; 138 139 boolean containsDots = word.indexOf('.') >= 0; 140 141 String className = containsDots ? 143 word.replace('.', ClassConstants.INTERNAL_PACKAGE_SEPARATOR) : 144 word; 145 146 Clazz clazz = classPool.getClass(className); 148 if (clazz != null) 149 { 150 String newClassName = clazz.getName(); 152 if (!className.equals(newClassName)) 153 { 154 newWord = containsDots ? 156 newClassName.replace(ClassConstants.INTERNAL_PACKAGE_SEPARATOR, '.') : 157 newClassName; 158 } 159 } 160 161 writer.write(newWord); 162 } 163 } 164 } 165 | Popular Tags |