1 /* 2 * ProGuard -- shrinking, optimization, obfuscation, and preverification 3 * of Java bytecode. 4 * 5 * Copyright (c) 2002-2014 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 import proguard.classfile.io.ProgramClassWriter; 25 import proguard.classfile.util.SimplifiedVisitor; 26 import proguard.classfile.visitor.ClassVisitor; 27 28 import java.io.*; 29 30 /** 31 * This ClassVisitor writes out the ProgramClass objects that it visits to the 32 * given DataEntry, modified to have the correct name. 33 * 34 * @author Eric Lafortune 35 */ 36 public class DataEntryClassWriter 37 extends SimplifiedVisitor 38 implements ClassVisitor 39 { 40 private final DataEntryWriter dataEntryWriter; 41 private final DataEntry templateDataEntry; 42 43 44 /** 45 * Creates a new DataEntryClassWriter for writing to the given 46 * DataEntryWriter, based on the given template DataEntry. 47 */ DataEntryClassWriter(DataEntryWriter dataEntryWriter, DataEntry templateDataEntry)48 public DataEntryClassWriter(DataEntryWriter dataEntryWriter, 49 DataEntry templateDataEntry) 50 { 51 this.dataEntryWriter = dataEntryWriter; 52 this.templateDataEntry = templateDataEntry; 53 } 54 55 56 // Implementations for ClassVisitor. 57 visitProgramClass(ProgramClass programClass)58 public void visitProgramClass(ProgramClass programClass) 59 { 60 // Rename the data entry if necessary. 61 String actualClassName = programClass.getName(); 62 DataEntry actualDataEntry = 63 new RenamedDataEntry(templateDataEntry, 64 actualClassName + ClassConstants.CLASS_FILE_EXTENSION); 65 66 try 67 { 68 // Get the output entry corresponding to this input entry. 69 OutputStream outputStream = dataEntryWriter.getOutputStream(actualDataEntry); 70 if (outputStream != null) 71 { 72 // Write the class to the output entry. 73 DataOutputStream classOutputStream = new DataOutputStream(outputStream); 74 75 new ProgramClassWriter(classOutputStream).visitProgramClass(programClass); 76 77 classOutputStream.flush(); 78 } 79 } 80 catch (IOException e) 81 { 82 throw new RuntimeException("Can't write program class ["+actualClassName+"] to ["+actualDataEntry+"] ("+e.getMessage()+")", e); 83 } 84 } 85 } 86