1
/* $Id: FilteredDataEntryWriter.java,v 1.2 2004/08/15 12:39:30 eric Exp $
3
* ProGuard -- shrinking, optimization, and obfuscation of Java class files.
5
* Copyright (c) 2002-2004 Eric Lafortune (eric@graphics.cornell.edu)
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)
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
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
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
30
* @author Eric Lafortune
32
public class FilteredDataEntryWriter implements DataEntryWriter
34
private DataEntryFilter dataEntryFilter;
35
private DataEntryWriter acceptedDataEntryWriter;
36
private DataEntryWriter rejectedDataEntryWriter;
40
* Creates a new FilteredDataEntryWriter with only a writer for accepted
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>.
47
public FilteredDataEntryWriter(DataEntryFilter dataEntryFilter,
48
DataEntryWriter acceptedDataEntryWriter)
50
this(dataEntryFilter, acceptedDataEntryWriter, null);
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
65
public FilteredDataEntryWriter(DataEntryFilter dataEntryFilter,
66
DataEntryWriter acceptedDataEntryWriter,
67
DataEntryWriter rejectedDataEntryWriter)
69
this.dataEntryFilter = dataEntryFilter;
70
this.acceptedDataEntryWriter = acceptedDataEntryWriter;
71
this.rejectedDataEntryWriter = rejectedDataEntryWriter;
75
// Implementations for DataEntryWriter.
77
public OutputStream getOutputStream(DataEntry dataEntry) throws IOException
79
return getOutputStream(dataEntry, null);
83
public OutputStream getOutputStream(DataEntry dataEntry,
84
Finisher finisher) throws IOException
86
// Get the right data entry writer.
87
DataEntryWriter dataEntryWriter = dataEntryFilter.accepts(dataEntry) ?
88
acceptedDataEntryWriter :
89
rejectedDataEntryWriter;
91
// Delegate to it, if it's not null.
92
return dataEntryWriter != null ?
93
dataEntryWriter.getOutputStream(dataEntry, finisher) :
98
public void close() throws IOException
100
if (acceptedDataEntryWriter != null)
102
acceptedDataEntryWriter.close();
103
acceptedDataEntryWriter = null;
106
if (rejectedDataEntryWriter != null)
108
rejectedDataEntryWriter.close();
109
rejectedDataEntryWriter = null;