KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > proguard > io > FilteredDataEntryWriter


1 /*
2  * ProGuard -- shrinking, optimization, obfuscation, and preverification
3  * of Java bytecode.
4  *
5  * Copyright (c) 2002-2007 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 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 {
34     private 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 OutputStream getOutputStream(DataEntry dataEntry) throws IOException
78     {
79         return getOutputStream(dataEntry, null);
80     }
81
82
83     public OutputStream getOutputStream(DataEntry dataEntry,
84                                         Finisher finisher) throws IOException
85     {
86         // Get the right data entry writer.
87
DataEntryWriter dataEntryWriter = dataEntryFilter.accepts(dataEntry) ?
88             acceptedDataEntryWriter :
89             rejectedDataEntryWriter;
90
91         // Delegate to it, if it's not null.
92
return dataEntryWriter != null ?
93             dataEntryWriter.getOutputStream(dataEntry, finisher) :
94             null;
95     }
96
97
98     public void close() throws IOException
99     {
100         if (acceptedDataEntryWriter != null)
101         {
102             acceptedDataEntryWriter.close();
103             acceptedDataEntryWriter = null;
104         }
105
106         if (rejectedDataEntryWriter != null)
107         {
108             rejectedDataEntryWriter.close();
109             rejectedDataEntryWriter = null;
110         }
111     }
112 }
113
Popular Tags