KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > filters > BaseFilterReader


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.filters;
19
20 import java.io.FilterReader JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.Reader JavaDoc;
23 import java.io.StringReader JavaDoc;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.util.FileUtils;
26
27 /**
28  * Base class for core filter readers.
29  *
30  */

31 public abstract class BaseFilterReader extends FilterReader JavaDoc {
32     /** Buffer size used when reading */
33     private static final int BUFFER_SIZE = 8192;
34
35     /** Have the parameters passed been interpreted? */
36     private boolean initialized = false;
37
38     /** The Ant project this filter is part of. */
39     private Project project = null;
40
41     /**
42      * Constructor used by Ant's introspection mechanism.
43      * The original filter reader is only used for chaining
44      * purposes, never for filtering purposes (and indeed
45      * it would be useless for filtering purposes, as it has
46      * no real data to filter). ChainedReaderHelper uses
47      * this placeholder instance to create a chain of real filters.
48      */

49     public BaseFilterReader() {
50         super(new StringReader JavaDoc(""));
51         FileUtils.close(this);
52     }
53
54     /**
55      * Creates a new filtered reader.
56      *
57      * @param in A Reader object providing the underlying stream.
58      * Must not be <code>null</code>.
59      *
60      */

61     public BaseFilterReader(final Reader JavaDoc in) {
62         super(in);
63     }
64
65     /**
66      * Reads characters into a portion of an array. This method will block
67      * until some input is available, an I/O error occurs, or the end of the
68      * stream is reached.
69      *
70      * @param cbuf Destination buffer to write characters to.
71      * Must not be <code>null</code>.
72      * @param off Offset at which to start storing characters.
73      * @param len Maximum number of characters to read.
74      *
75      * @return the number of characters read, or -1 if the end of the
76      * stream has been reached
77      *
78      * @exception IOException If an I/O error occurs
79      */

80     public final int read(final char[] cbuf, final int off,
81                           final int len) throws IOException JavaDoc {
82         for (int i = 0; i < len; i++) {
83             final int ch = read();
84             if (ch == -1) {
85                 if (i == 0) {
86                     return -1;
87                 } else {
88                     return i;
89                 }
90             }
91             cbuf[off + i] = (char) ch;
92         }
93         return len;
94     }
95
96     /**
97      * Skips characters. This method will block until some characters are
98      * available, an I/O error occurs, or the end of the stream is reached.
99      *
100      * @param n The number of characters to skip
101      *
102      * @return the number of characters actually skipped
103      *
104      * @exception IllegalArgumentException If <code>n</code> is negative.
105      * @exception IOException If an I/O error occurs
106      */

107     public final long skip(final long n)
108         throws IOException JavaDoc, IllegalArgumentException JavaDoc {
109         if (n < 0L) {
110             throw new IllegalArgumentException JavaDoc("skip value is negative");
111         }
112
113         for (long i = 0; i < n; i++) {
114             if (read() == -1) {
115                 return i;
116             }
117         }
118         return n;
119     }
120
121     /**
122      * Sets the initialized status.
123      *
124      * @param initialized Whether or not the filter is initialized.
125      */

126     protected final void setInitialized(final boolean initialized) {
127         this.initialized = initialized;
128     }
129
130     /**
131      * Returns the initialized status.
132      *
133      * @return whether or not the filter is initialized
134      */

135     protected final boolean getInitialized() {
136         return initialized;
137     }
138
139     /**
140      * Sets the project to work with.
141      *
142      * @param project The project this filter is part of.
143      * Should not be <code>null</code>.
144      */

145     public final void setProject(final Project project) {
146         this.project = project;
147     }
148
149     /**
150      * Returns the project this filter is part of.
151      *
152      * @return the project this filter is part of
153      */

154     protected final Project getProject() {
155         return project;
156     }
157
158     /**
159      * Reads a line of text ending with '\n' (or until the end of the stream).
160      * The returned String retains the '\n'.
161      *
162      * @return the line read, or <code>null</code> if the end of the stream
163      * has already been reached
164      *
165      * @exception IOException if the underlying reader throws one during
166      * reading
167      */

168     protected final String JavaDoc readLine() throws IOException JavaDoc {
169         int ch = in.read();
170
171         if (ch == -1) {
172             return null;
173         }
174
175         StringBuffer JavaDoc line = new StringBuffer JavaDoc();
176
177         while (ch != -1) {
178             line.append ((char) ch);
179             if (ch == '\n') {
180                 break;
181             }
182             ch = in.read();
183         }
184         return line.toString();
185     }
186
187     /**
188      * Reads to the end of the stream, returning the contents as a String.
189      *
190      * @return the remaining contents of the reader, as a String
191      *
192      * @exception IOException if the underlying reader throws one during
193      * reading
194      */

195     protected final String JavaDoc readFully() throws IOException JavaDoc {
196         return FileUtils.readFully(in, BUFFER_SIZE);
197     }
198 }
199
Popular Tags