001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.io;
019:
020: /**
021: * FilterReader is a class which takes a Reader and <em>filters</em> the input
022: * in some way. The filtered view may be a buffered view or one which
023: * uncompresses data before returning characters read.
024: *
025: * @see FilterWriter
026: */
027: public abstract class FilterReader extends Reader {
028:
029: /**
030: * The target Reader which is being filtered.
031: */
032: protected Reader in;
033:
034: /**
035: * Constructs a new FilterReader on the Reader <code>in</code>. All reads
036: * are now filtered through this Reader.
037: *
038: * @param in
039: * The non-null Reader to filter reads on.
040: */
041: protected FilterReader(Reader in) {
042: super (in);
043: this .in = in;
044: }
045:
046: /**
047: * Close this FilterReader. This implementation closes the target Reader.
048: *
049: * @throws IOException
050: * If an error occurs attempting to close this Reader.
051: */
052: @Override
053: public void close() throws IOException {
054: synchronized (lock) {
055: in.close();
056: }
057: }
058:
059: /**
060: * Set a Mark position in this FilterReader. The parameter
061: * <code>readLimit</code> indicates how many characters can be read before
062: * a mark is invalidated. Sending reset() will reposition the Reader back to
063: * the marked position provided <code>readLimit</code> has not been
064: * surpassed.
065: * <p>
066: * This implementation sets a mark in the target Reader.
067: *
068: * @param readlimit
069: * the number of characters to be able to read before
070: * invalidating the mark.
071: *
072: * @throws IOException
073: * If an error occurs attempting mark this Reader.
074: */
075: @Override
076: public synchronized void mark(int readlimit) throws IOException {
077: synchronized (lock) {
078: in.mark(readlimit);
079: }
080: }
081:
082: /**
083: * Answers a boolean indicating whether or not this FilterReader supports
084: * mark() and reset(). This implementation answers whether or not the target
085: * Reader supports marking.
086: *
087: * @return indicates whether or not mark() and reset() are supported.
088: */
089: @Override
090: public boolean markSupported() {
091: synchronized (lock) {
092: return in.markSupported();
093: }
094: }
095:
096: /**
097: * Reads a single char from this FilterReader and returns the result as an
098: * int. The 2 lowest order bytes are returned or -1 of the end of reader was
099: * encountered. This implementation returns a char from the target Reader.
100: *
101: * @return The byte read or -1 if end of reader.
102: *
103: * @throws IOException
104: * If an error occurs attempting to read from this Reader.
105: */
106: @Override
107: public int read() throws IOException {
108: synchronized (lock) {
109: return in.read();
110: }
111: }
112:
113: /**
114: * Reads at most <code>count</code> chars from this FilterReader and
115: * stores them in char array <code>buffer</code> starting at offset
116: * <code>offset</code>. Answer the number of chars actually read or -1 if
117: * no chars were read and end of reader was encountered. This implementation
118: * reads chars from the target reader.
119: *
120: * @param buffer
121: * the char array in which to store the read chars.
122: * @param offset
123: * the offset in <code>buffer</code> to store the read chars.
124: * @param count
125: * the maximum number of chars to store in <code>buffer</code>.
126: * @return the number of chars actually read or -1 if end of reader.
127: *
128: * @throws IOException
129: * If an error occurs attempting to read from this Reader.
130: */
131: @Override
132: public int read(char[] buffer, int offset, int count)
133: throws IOException {
134: synchronized (lock) {
135: return in.read(buffer, offset, count);
136: }
137: }
138:
139: /**
140: * Answers a <code>boolean</code> indicating whether or not this Reader is
141: * ready to be read without blocking. If the result is <code>true</code>,
142: * the next <code>read()</code> will not block. If the result is
143: * <code>false</code> this Reader may or may not block when
144: * <code>read()</code> is sent.
145: *
146: * @return <code>true</code> if the receiver will not block when
147: * <code>read()</code> is called, <code>false</code> if unknown
148: * or blocking will occur.
149: *
150: * @throws IOException
151: * If the Reader is already closed or some other IO error
152: * occurs.
153: */
154: @Override
155: public boolean ready() throws IOException {
156: synchronized (lock) {
157: return in.ready();
158: }
159: }
160:
161: /**
162: * Reset this Readers position to the last <code>mark()</code> location.
163: * Invocations of <code>read()/skip()</code> will occur from this new
164: * location. If this Reader was not marked, the implementation of
165: * <code>reset()</code> is implementation specific. See the comment for
166: * the specific Reader subclass for implementation details. The default
167: * action is to throw <code>IOException</code>.
168: *
169: * @throws IOException
170: * if a problem occurred or the target Reader does not support
171: * <code>mark()/reset()</code>.
172: */
173: @Override
174: public void reset() throws IOException {
175: synchronized (lock) {
176: in.reset();
177: }
178: }
179:
180: /**
181: * Skips <code>count</code> number of characters in this Reader.
182: * Subsequent <code>read()</code>'s will not return these characters
183: * unless <code>reset()</code> is used. The default implementation is to
184: * skip chars in the filtered Reader.
185: *
186: * @param count
187: * the maximum number of characters to skip.
188: * @return the number of characters actually skipped.
189: *
190: * @throws IOException
191: * If the Reader is already closed or some other IO error
192: * occurs.
193: */
194: @Override
195: public long skip(long count) throws IOException {
196: synchronized (lock) {
197: return in.skip(count);
198: }
199: }
200: }
|