1 | /*
|
---|
2 | * Copyright 2002,2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | package org.apache.tools.ant.filters;
|
---|
18 |
|
---|
19 | import java.io.FilterReader;
|
---|
20 | import java.io.IOException;
|
---|
21 | import java.io.Reader;
|
---|
22 | import java.io.StringReader;
|
---|
23 | import org.apache.tools.ant.Project;
|
---|
24 | import org.apache.tools.ant.util.FileUtils;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Base class for core filter readers.
|
---|
28 | *
|
---|
29 | */
|
---|
30 | public abstract class BaseFilterReader extends FilterReader {
|
---|
31 | /** Buffer size used when reading */
|
---|
32 | private static final int BUFFER_SIZE = 8192;
|
---|
33 |
|
---|
34 | /** Have the parameters passed been interpreted? */
|
---|
35 | private boolean initialized = false;
|
---|
36 |
|
---|
37 | /** The Ant project this filter is part of. */
|
---|
38 | private Project project = null;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Constructor used by Ant's introspection mechanism.
|
---|
42 | * The original filter reader is only used for chaining
|
---|
43 | * purposes, never for filtering purposes (and indeed
|
---|
44 | * it would be useless for filtering purposes, as it has
|
---|
45 | * no real data to filter). ChainedReaderHelper uses
|
---|
46 | * this placeholder instance to create a chain of real filters.
|
---|
47 | */
|
---|
48 | public BaseFilterReader() {
|
---|
49 | super(new StringReader(new String()));
|
---|
50 | try {
|
---|
51 | close();
|
---|
52 | } catch (IOException ioe) {
|
---|
53 | // Ignore
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Creates a new filtered reader.
|
---|
59 | *
|
---|
60 | * @param in A Reader object providing the underlying stream.
|
---|
61 | * Must not be <code>null</code>.
|
---|
62 | *
|
---|
63 | */
|
---|
64 | public BaseFilterReader(final Reader in) {
|
---|
65 | super(in);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Reads characters into a portion of an array. This method will block
|
---|
70 | * until some input is available, an I/O error occurs, or the end of the
|
---|
71 | * stream is reached.
|
---|
72 | *
|
---|
73 | * @param cbuf Destination buffer to write characters to.
|
---|
74 | * Must not be <code>null</code>.
|
---|
75 | * @param off Offset at which to start storing characters.
|
---|
76 | * @param len Maximum number of characters to read.
|
---|
77 | *
|
---|
78 | * @return the number of characters read, or -1 if the end of the
|
---|
79 | * stream has been reached
|
---|
80 | *
|
---|
81 | * @exception IOException If an I/O error occurs
|
---|
82 | */
|
---|
83 | public final int read(final char[] cbuf, final int off,
|
---|
84 | final int len) throws IOException {
|
---|
85 | for (int i = 0; i < len; i++) {
|
---|
86 | final int ch = read();
|
---|
87 | if (ch == -1) {
|
---|
88 | if (i == 0) {
|
---|
89 | return -1;
|
---|
90 | } else {
|
---|
91 | return i;
|
---|
92 | }
|
---|
93 | }
|
---|
94 | cbuf[off + i] = (char) ch;
|
---|
95 | }
|
---|
96 | return len;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Skips characters. This method will block until some characters are
|
---|
101 | * available, an I/O error occurs, or the end of the stream is reached.
|
---|
102 | *
|
---|
103 | * @param n The number of characters to skip
|
---|
104 | *
|
---|
105 | * @return the number of characters actually skipped
|
---|
106 | *
|
---|
107 | * @exception IllegalArgumentException If <code>n</code> is negative.
|
---|
108 | * @exception IOException If an I/O error occurs
|
---|
109 | */
|
---|
110 | public final long skip(final long n)
|
---|
111 | throws IOException, IllegalArgumentException {
|
---|
112 | if (n < 0L) {
|
---|
113 | throw new IllegalArgumentException("skip value is negative");
|
---|
114 | }
|
---|
115 |
|
---|
116 | for (long i = 0; i < n; i++) {
|
---|
117 | if (read() == -1) {
|
---|
118 | return i;
|
---|
119 | }
|
---|
120 | }
|
---|
121 | return n;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Sets the initialized status.
|
---|
126 | *
|
---|
127 | * @param initialized Whether or not the filter is initialized.
|
---|
128 | */
|
---|
129 | protected final void setInitialized(final boolean initialized) {
|
---|
130 | this.initialized = initialized;
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Returns the initialized status.
|
---|
135 | *
|
---|
136 | * @return whether or not the filter is initialized
|
---|
137 | */
|
---|
138 | protected final boolean getInitialized() {
|
---|
139 | return initialized;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Sets the project to work with.
|
---|
144 | *
|
---|
145 | * @param project The project this filter is part of.
|
---|
146 | * Should not be <code>null</code>.
|
---|
147 | */
|
---|
148 | public final void setProject(final Project project) {
|
---|
149 | this.project = project;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Returns the project this filter is part of.
|
---|
154 | *
|
---|
155 | * @return the project this filter is part of
|
---|
156 | */
|
---|
157 | protected final Project getProject() {
|
---|
158 | return project;
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Reads a line of text ending with '\n' (or until the end of the stream).
|
---|
163 | * The returned String retains the '\n'.
|
---|
164 | *
|
---|
165 | * @return the line read, or <code>null</code> if the end of the stream
|
---|
166 | * has already been reached
|
---|
167 | *
|
---|
168 | * @exception IOException if the underlying reader throws one during
|
---|
169 | * reading
|
---|
170 | */
|
---|
171 | protected final String readLine() throws IOException {
|
---|
172 | int ch = in.read();
|
---|
173 |
|
---|
174 | if (ch == -1) {
|
---|
175 | return null;
|
---|
176 | }
|
---|
177 |
|
---|
178 | StringBuffer line = new StringBuffer();
|
---|
179 |
|
---|
180 | while (ch != -1) {
|
---|
181 | line.append ((char) ch);
|
---|
182 | if (ch == '\n') {
|
---|
183 | break;
|
---|
184 | }
|
---|
185 | ch = in.read();
|
---|
186 | }
|
---|
187 | return line.toString();
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Reads to the end of the stream, returning the contents as a String.
|
---|
192 | *
|
---|
193 | * @return the remaining contents of the reader, as a String
|
---|
194 | *
|
---|
195 | * @exception IOException if the underlying reader throws one during
|
---|
196 | * reading
|
---|
197 | */
|
---|
198 | protected final String readFully() throws IOException {
|
---|
199 | return FileUtils.readFully(in, BUFFER_SIZE);
|
---|
200 | }
|
---|
201 | }
|
---|