KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > xmlparser > ASCIIReader


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.jasper.xmlparser;
19
20 import java.io.InputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.Reader JavaDoc;
23 import org.apache.jasper.compiler.Localizer;
24
25 /**
26  * A simple ASCII byte reader. This is an optimized reader for reading
27  * byte streams that only contain 7-bit ASCII characters.
28  *
29  * @author Andy Clark, IBM
30  *
31  * @version $Id: ASCIIReader.java 467222 2006-10-24 03:17:11Z markt $
32  */

33 public class ASCIIReader
34     extends Reader JavaDoc {
35
36     //
37
// Constants
38
//
39

40     /** Default byte buffer size (2048). */
41     public static final int DEFAULT_BUFFER_SIZE = 2048;
42
43     //
44
// Data
45
//
46

47     /** Input stream. */
48     protected InputStream JavaDoc fInputStream;
49
50     /** Byte buffer. */
51     protected byte[] fBuffer;
52
53     //
54
// Constructors
55
//
56

57     /**
58      * Constructs an ASCII reader from the specified input stream
59      * and buffer size.
60      *
61      * @param inputStream The input stream.
62      * @param size The initial buffer size.
63      */

64     public ASCIIReader(InputStream JavaDoc inputStream, int size) {
65         fInputStream = inputStream;
66         fBuffer = new byte[size];
67     }
68
69     //
70
// Reader methods
71
//
72

73     /**
74      * Read a single character. This method will block until a character is
75      * available, an I/O error occurs, or the end of the stream is reached.
76      *
77      * <p> Subclasses that intend to support efficient single-character input
78      * should override this method.
79      *
80      * @return The character read, as an integer in the range 0 to 127
81      * (<tt>0x00-0x7f</tt>), or -1 if the end of the stream has
82      * been reached
83      *
84      * @exception IOException If an I/O error occurs
85      */

86     public int read() throws IOException JavaDoc {
87         int b0 = fInputStream.read();
88         if (b0 > 0x80) {
89             throw new IOException JavaDoc(Localizer.getMessage("jsp.error.xml.invalidASCII",
90                                Integer.toString(b0)));
91         }
92         return b0;
93     } // read():int
94

95     /**
96      * Read characters into a portion of an array. This method will block
97      * until some input is available, an I/O error occurs, or the end of the
98      * stream is reached.
99      *
100      * @param ch Destination buffer
101      * @param offset Offset at which to start storing characters
102      * @param length Maximum number of characters to read
103      *
104      * @return The number of characters read, or -1 if the end of the
105      * stream has been reached
106      *
107      * @exception IOException If an I/O error occurs
108      */

109     public int read(char ch[], int offset, int length) throws IOException JavaDoc {
110         if (length > fBuffer.length) {
111             length = fBuffer.length;
112         }
113         int count = fInputStream.read(fBuffer, 0, length);
114         for (int i = 0; i < count; i++) {
115             int b0 = fBuffer[i];
116             if (b0 > 0x80) {
117                 throw new IOException JavaDoc(Localizer.getMessage("jsp.error.xml.invalidASCII",
118                                Integer.toString(b0)));
119             }
120             ch[offset + i] = (char)b0;
121         }
122         return count;
123     } // read(char[],int,int)
124

125     /**
126      * Skip characters. This method will block until some characters are
127      * available, an I/O error occurs, or the end of the stream is reached.
128      *
129      * @param n The number of characters to skip
130      *
131      * @return The number of characters actually skipped
132      *
133      * @exception IOException If an I/O error occurs
134      */

135     public long skip(long n) throws IOException JavaDoc {
136         return fInputStream.skip(n);
137     } // skip(long):long
138

139     /**
140      * Tell whether this stream is ready to be read.
141      *
142      * @return True if the next read() is guaranteed not to block for input,
143      * false otherwise. Note that returning false does not guarantee that the
144      * next read will block.
145      *
146      * @exception IOException If an I/O error occurs
147      */

148     public boolean ready() throws IOException JavaDoc {
149     return false;
150     } // ready()
151

152     /**
153      * Tell whether this stream supports the mark() operation.
154      */

155     public boolean markSupported() {
156     return fInputStream.markSupported();
157     } // markSupported()
158

159     /**
160      * Mark the present position in the stream. Subsequent calls to reset()
161      * will attempt to reposition the stream to this point. Not all
162      * character-input streams support the mark() operation.
163      *
164      * @param readAheadLimit Limit on the number of characters that may be
165      * read while still preserving the mark. After
166      * reading this many characters, attempting to
167      * reset the stream may fail.
168      *
169      * @exception IOException If the stream does not support mark(),
170      * or if some other I/O error occurs
171      */

172     public void mark(int readAheadLimit) throws IOException JavaDoc {
173     fInputStream.mark(readAheadLimit);
174     } // mark(int)
175

176     /**
177      * Reset the stream. If the stream has been marked, then attempt to
178      * reposition it at the mark. If the stream has not been marked, then
179      * attempt to reset it in some way appropriate to the particular stream,
180      * for example by repositioning it to its starting point. Not all
181      * character-input streams support the reset() operation, and some support
182      * reset() without supporting mark().
183      *
184      * @exception IOException If the stream has not been marked,
185      * or if the mark has been invalidated,
186      * or if the stream does not support reset(),
187      * or if some other I/O error occurs
188      */

189     public void reset() throws IOException JavaDoc {
190         fInputStream.reset();
191     } // reset()
192

193     /**
194      * Close the stream. Once a stream has been closed, further read(),
195      * ready(), mark(), or reset() invocations will throw an IOException.
196      * Closing a previously-closed stream, however, has no effect.
197      *
198      * @exception IOException If an I/O error occurs
199      */

200      public void close() throws IOException JavaDoc {
201          fInputStream.close();
202      } // close()
203

204 } // class ASCIIReader
205
Popular Tags