1/* 2 * ProGuard -- shrinking, optimization, obfuscation, and preverification 3 * of Java bytecode. 4 * 5 * Copyright (c) 2002-2009 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 */ 21package proguard.io; 22 23import java.io.*; 24 25/** 26 * This DataEntryWriter delegates to one of two other DataEntryWriter instances, 27 * depending on whether the data entry passes through a given data entry filter 28 * or not. 29 * 30 * @author Eric Lafortune 31 */ 32public class FilteredDataEntryWriter implements DataEntryWriter 33{ 34 private final DataEntryFilter dataEntryFilter; 35 private DataEntryWriter acceptedDataEntryWriter; 36 private DataEntryWriter rejectedDataEntryWriter; 37 38 39 /** 40 * Creates a new FilteredDataEntryWriter with only a writer for accepted 41 * data entries. 42 * @param dataEntryFilter the data entry filter. 43 * @param acceptedDataEntryWriter the DataEntryWriter to which the writing 44 * will be delegated if the filter accepts 45 * the data entry. May be <code>null</code>. 46 */ 47 public FilteredDataEntryWriter(DataEntryFilter dataEntryFilter, 48 DataEntryWriter acceptedDataEntryWriter) 49 { 50 this(dataEntryFilter, acceptedDataEntryWriter, null); 51 } 52 53 54 /** 55 * Creates a new FilteredDataEntryWriter. 56 * @param dataEntryFilter the data entry filter. 57 * @param acceptedDataEntryWriter the DataEntryWriter to which the writing 58 * will be delegated if the filter accepts 59 * the data entry. May be <code>null</code>. 60 * @param rejectedDataEntryWriter the DataEntryWriter to which the writing 61 * will be delegated if the filter does not 62 * accept the data entry. May be 63 * <code>null</code>. 64 */ 65 public FilteredDataEntryWriter(DataEntryFilter dataEntryFilter, 66 DataEntryWriter acceptedDataEntryWriter, 67 DataEntryWriter rejectedDataEntryWriter) 68 { 69 this.dataEntryFilter = dataEntryFilter; 70 this.acceptedDataEntryWriter = acceptedDataEntryWriter; 71 this.rejectedDataEntryWriter = rejectedDataEntryWriter; 72 } 73 74 75 // Implementations for DataEntryWriter. 76 77 public boolean createDirectory(DataEntry dataEntry) throws IOException 78 { 79 // Get the right data entry writer. 80 DataEntryWriter dataEntryWriter = dataEntryFilter.accepts(dataEntry) ? 81 acceptedDataEntryWriter : 82 rejectedDataEntryWriter; 83 84 // Delegate to it, if it's not null. 85 return dataEntryWriter != null && 86 dataEntryWriter.createDirectory(dataEntry); 87 } 88 89 90 public OutputStream getOutputStream(DataEntry dataEntry) throws IOException 91 { 92 return getOutputStream(dataEntry, null); 93 } 94 95 96 public OutputStream getOutputStream(DataEntry dataEntry, 97 Finisher finisher) throws IOException 98 { 99 // Get the right data entry writer. 100 DataEntryWriter dataEntryWriter = dataEntryFilter.accepts(dataEntry) ? 101 acceptedDataEntryWriter : 102 rejectedDataEntryWriter; 103 104 // Delegate to it, if it's not null. 105 return dataEntryWriter != null ? 106 dataEntryWriter.getOutputStream(dataEntry, finisher) : 107 null; 108 } 109 110 111 public void close() throws IOException 112 { 113 if (acceptedDataEntryWriter != null) 114 { 115 acceptedDataEntryWriter.close(); 116 acceptedDataEntryWriter = null; 117 } 118 119 if (rejectedDataEntryWriter != null) 120 { 121 rejectedDataEntryWriter.close(); 122 rejectedDataEntryWriter = null; 123 } 124 } 125} 126