KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > impl > io > ASCIIReader


1 /*
2  * Copyright 2000-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.xerces.impl.io;
18
19 import java.io.InputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.Reader JavaDoc;
22 import java.util.Locale JavaDoc;
23 import org.apache.xerces.util.MessageFormatter;
24 import org.apache.xerces.impl.msg.XMLMessageFormatter;
25
26 /**
27  * A simple ASCII byte reader. This is an optimized reader for reading
28  * byte streams that only contain 7-bit ASCII characters.
29  *
30  * @xerces.internal
31  *
32  * @author Andy Clark, IBM
33  *
34  * @version $Id: ASCIIReader.java,v 1.8 2004/10/04 22:07:41 mrglavas Exp $
35  */

36 public class ASCIIReader
37     extends Reader JavaDoc {
38
39     //
40
// Constants
41
//
42

43     /** Default byte buffer size (2048). */
44     public static final int DEFAULT_BUFFER_SIZE = 2048;
45
46     //
47
// Data
48
//
49

50     /** Input stream. */
51     protected InputStream JavaDoc fInputStream;
52
53     /** Byte buffer. */
54     protected byte[] fBuffer;
55
56     // message formatter; used to produce localized
57
// exception messages
58
private MessageFormatter fFormatter = null;
59
60     //Locale to use for messages
61
private Locale JavaDoc fLocale = null;
62
63     //
64
// Constructors
65
//
66

67     /**
68      * Constructs an ASCII reader from the specified input stream
69      * using the default buffer size.
70      *
71      * @param inputStream The input stream.
72      * @param messageFormatter the MessageFormatter to use to message reporting.
73      * @param locale the Locale for which messages are to be reported
74      */

75     public ASCIIReader(InputStream JavaDoc inputStream, MessageFormatter messageFormatter,
76             Locale JavaDoc locale) {
77         this(inputStream, DEFAULT_BUFFER_SIZE, messageFormatter, locale);
78     } // <init>(InputStream, MessageFormatter, Locale)
79

80     /**
81      * Constructs an ASCII reader from the specified input stream
82      * and buffer size.
83      *
84      * @param inputStream The input stream.
85      * @param size The initial buffer size.
86      * @param messageFormatter the MessageFormatter to use to message reporting.
87      * @param locale the Locale for which messages are to be reported
88      */

89     public ASCIIReader(InputStream JavaDoc inputStream, int size,
90             MessageFormatter messageFormatter, Locale JavaDoc locale) {
91         fInputStream = inputStream;
92         fBuffer = new byte[size];
93         fFormatter = messageFormatter;
94         fLocale = locale;
95     } // <init>(InputStream,int, MessageFormatter, Locale)
96

97     //
98
// Reader methods
99
//
100

101     /**
102      * Read a single character. This method will block until a character is
103      * available, an I/O error occurs, or the end of the stream is reached.
104      *
105      * <p> Subclasses that intend to support efficient single-character input
106      * should override this method.
107      *
108      * @return The character read, as an integer in the range 0 to 127
109      * (<tt>0x00-0x7f</tt>), or -1 if the end of the stream has
110      * been reached
111      *
112      * @exception IOException If an I/O error occurs
113      */

114     public int read() throws IOException JavaDoc {
115         int b0 = fInputStream.read();
116         if (b0 >= 0x80) {
117             throw new MalformedByteSequenceException(fFormatter,
118                 fLocale, XMLMessageFormatter.XML_DOMAIN,
119                 "InvalidASCII", new Object JavaDoc [] {Integer.toString(b0)});
120         }
121         return b0;
122     } // read():int
123

124     /**
125      * Read characters into a portion of an array. This method will block
126      * until some input is available, an I/O error occurs, or the end of the
127      * stream is reached.
128      *
129      * @param ch Destination buffer
130      * @param offset Offset at which to start storing characters
131      * @param length Maximum number of characters to read
132      *
133      * @return The number of characters read, or -1 if the end of the
134      * stream has been reached
135      *
136      * @exception IOException If an I/O error occurs
137      */

138     public int read(char ch[], int offset, int length) throws IOException JavaDoc {
139         if (length > fBuffer.length) {
140             length = fBuffer.length;
141         }
142         int count = fInputStream.read(fBuffer, 0, length);
143         for (int i = 0; i < count; i++) {
144             int b0 = fBuffer[i];
145             if (b0 < 0) {
146                 throw new MalformedByteSequenceException(fFormatter,
147                     fLocale, XMLMessageFormatter.XML_DOMAIN,
148                     "InvalidASCII", new Object JavaDoc [] {Integer.toString(b0 & 0x0FF)});
149             }
150             ch[offset + i] = (char)b0;
151         }
152         return count;
153     } // read(char[],int,int)
154

155     /**
156      * Skip characters. This method will block until some characters are
157      * available, an I/O error occurs, or the end of the stream is reached.
158      *
159      * @param n The number of characters to skip
160      *
161      * @return The number of characters actually skipped
162      *
163      * @exception IOException If an I/O error occurs
164      */

165     public long skip(long n) throws IOException JavaDoc {
166         return fInputStream.skip(n);
167     } // skip(long):long
168

169     /**
170      * Tell whether this stream is ready to be read.
171      *
172      * @return True if the next read() is guaranteed not to block for input,
173      * false otherwise. Note that returning false does not guarantee that the
174      * next read will block.
175      *
176      * @exception IOException If an I/O error occurs
177      */

178     public boolean ready() throws IOException JavaDoc {
179         return false;
180     } // ready()
181

182     /**
183      * Tell whether this stream supports the mark() operation.
184      */

185     public boolean markSupported() {
186         return fInputStream.markSupported();
187     } // markSupported()
188

189     /**
190      * Mark the present position in the stream. Subsequent calls to reset()
191      * will attempt to reposition the stream to this point. Not all
192      * character-input streams support the mark() operation.
193      *
194      * @param readAheadLimit Limit on the number of characters that may be
195      * read while still preserving the mark. After
196      * reading this many characters, attempting to
197      * reset the stream may fail.
198      *
199      * @exception IOException If the stream does not support mark(),
200      * or if some other I/O error occurs
201      */

202     public void mark(int readAheadLimit) throws IOException JavaDoc {
203         fInputStream.mark(readAheadLimit);
204     } // mark(int)
205

206     /**
207      * Reset the stream. If the stream has been marked, then attempt to
208      * reposition it at the mark. If the stream has not been marked, then
209      * attempt to reset it in some way appropriate to the particular stream,
210      * for example by repositioning it to its starting point. Not all
211      * character-input streams support the reset() operation, and some support
212      * reset() without supporting mark().
213      *
214      * @exception IOException If the stream has not been marked,
215      * or if the mark has been invalidated,
216      * or if the stream does not support reset(),
217      * or if some other I/O error occurs
218      */

219     public void reset() throws IOException JavaDoc {
220         fInputStream.reset();
221     } // reset()
222

223     /**
224      * Close the stream. Once a stream has been closed, further read(),
225      * ready(), mark(), or reset() invocations will throw an IOException.
226      * Closing a previously-closed stream, however, has no effect.
227      *
228      * @exception IOException If an I/O error occurs
229      */

230      public void close() throws IOException JavaDoc {
231          fInputStream.close();
232      } // close()
233

234 } // class ASCIIReader
235
Popular Tags