01: /*
02: * ProGuard -- shrinking, optimization, obfuscation, and preverification
03: * of Java bytecode.
04: *
05: * Copyright (c) 2002-2007 Eric Lafortune (eric@graphics.cornell.edu)
06: *
07: * This program is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU General Public License as published by the Free
09: * 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 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: */
32: public class FilteredDataEntryWriter implements DataEntryWriter {
33: private final DataEntryFilter dataEntryFilter;
34: private DataEntryWriter acceptedDataEntryWriter;
35: private DataEntryWriter rejectedDataEntryWriter;
36:
37: /**
38: * Creates a new FilteredDataEntryWriter with only a writer for accepted
39: * data entries.
40: * @param dataEntryFilter the data entry filter.
41: * @param acceptedDataEntryWriter the DataEntryWriter to which the writing
42: * will be delegated if the filter accepts
43: * the data entry. May be <code>null</code>.
44: */
45: public FilteredDataEntryWriter(DataEntryFilter dataEntryFilter,
46: DataEntryWriter acceptedDataEntryWriter) {
47: this (dataEntryFilter, acceptedDataEntryWriter, null);
48: }
49:
50: /**
51: * Creates a new FilteredDataEntryWriter.
52: * @param dataEntryFilter the data entry filter.
53: * @param acceptedDataEntryWriter the DataEntryWriter to which the writing
54: * will be delegated if the filter accepts
55: * the data entry. May be <code>null</code>.
56: * @param rejectedDataEntryWriter the DataEntryWriter to which the writing
57: * will be delegated if the filter does not
58: * accept the data entry. May be
59: * <code>null</code>.
60: */
61: public FilteredDataEntryWriter(DataEntryFilter dataEntryFilter,
62: DataEntryWriter acceptedDataEntryWriter,
63: DataEntryWriter rejectedDataEntryWriter) {
64: this .dataEntryFilter = dataEntryFilter;
65: this .acceptedDataEntryWriter = acceptedDataEntryWriter;
66: this .rejectedDataEntryWriter = rejectedDataEntryWriter;
67: }
68:
69: // Implementations for DataEntryWriter.
70:
71: public OutputStream getOutputStream(DataEntry dataEntry)
72: throws IOException {
73: return getOutputStream(dataEntry, null);
74: }
75:
76: public OutputStream getOutputStream(DataEntry dataEntry,
77: Finisher finisher) throws IOException {
78: // Get the right data entry writer.
79: DataEntryWriter dataEntryWriter = dataEntryFilter
80: .accepts(dataEntry) ? acceptedDataEntryWriter
81: : rejectedDataEntryWriter;
82:
83: // Delegate to it, if it's not null.
84: return dataEntryWriter != null ? dataEntryWriter
85: .getOutputStream(dataEntry, finisher) : null;
86: }
87:
88: public void close() throws IOException {
89: if (acceptedDataEntryWriter != null) {
90: acceptedDataEntryWriter.close();
91: acceptedDataEntryWriter = null;
92: }
93:
94: if (rejectedDataEntryWriter != null) {
95: rejectedDataEntryWriter.close();
96: rejectedDataEntryWriter = null;
97: }
98: }
99: }
|