KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > impl > io > ASCIIReader


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.sun.org.apache.xerces.internal.impl.io;
59
60 import java.io.InputStream JavaDoc;
61 import java.io.IOException JavaDoc;
62 import java.io.Reader JavaDoc;
63 import java.util.Locale JavaDoc;
64 import com.sun.org.apache.xerces.internal.util.MessageFormatter;
65 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;
66
67 /**
68  * A simple ASCII byte reader. This is an optimized reader for reading
69  * byte streams that only contain 7-bit ASCII characters.
70  *
71  * @author Andy Clark, IBM
72  *
73  * @version $Id: ASCIIReader.java,v 1.7 2004/03/04 19:27:13 mrglavas Exp $
74  */

75 public class ASCIIReader
76     extends Reader JavaDoc {
77
78     //
79
// Constants
80
//
81

82     /** Default byte buffer size (2048). */
83     public static final int DEFAULT_BUFFER_SIZE = 2048;
84
85     //
86
// Data
87
//
88

89     /** Input stream. */
90     protected InputStream JavaDoc fInputStream;
91
92     /** Byte buffer. */
93     protected byte[] fBuffer;
94
95     // message formatter; used to produce localized
96
// exception messages
97
private MessageFormatter fFormatter = null;
98
99     //Locale to use for messages
100
private Locale JavaDoc fLocale = null;
101
102     //
103
// Constructors
104
//
105

106     /**
107      * Constructs an ASCII reader from the specified input stream
108      * using the default buffer size.
109      *
110      * @param inputStream The input stream.
111      * @param messageFormatter the MessageFormatter to use to message reporting.
112      * @param locale the Locale for which messages are to be reported
113      */

114     public ASCIIReader(InputStream JavaDoc inputStream, MessageFormatter messageFormatter,
115             Locale JavaDoc locale) {
116         this(inputStream, DEFAULT_BUFFER_SIZE, messageFormatter, locale);
117     } // <init>(InputStream, MessageFormatter, Locale)
118

119     /**
120      * Constructs an ASCII reader from the specified input stream
121      * and buffer size.
122      *
123      * @param inputStream The input stream.
124      * @param size The initial buffer size.
125      * @param messageFormatter the MessageFormatter to use to message reporting.
126      * @param locale the Locale for which messages are to be reported
127      */

128     public ASCIIReader(InputStream JavaDoc inputStream, int size,
129             MessageFormatter messageFormatter, Locale JavaDoc locale) {
130         fInputStream = inputStream;
131         fBuffer = new byte[size];
132         fFormatter = messageFormatter;
133         fLocale = locale;
134     } // <init>(InputStream,int, MessageFormatter, Locale)
135

136     //
137
// Reader methods
138
//
139

140     /**
141      * Read a single character. This method will block until a character is
142      * available, an I/O error occurs, or the end of the stream is reached.
143      *
144      * <p> Subclasses that intend to support efficient single-character input
145      * should override this method.
146      *
147      * @return The character read, as an integer in the range 0 to 127
148      * (<tt>0x00-0x7f</tt>), or -1 if the end of the stream has
149      * been reached
150      *
151      * @exception IOException If an I/O error occurs
152      */

153     public int read() throws IOException JavaDoc {
154         int b0 = fInputStream.read();
155         if (b0 >= 0x80) {
156             throw new MalformedByteSequenceException(fFormatter,
157                 fLocale, XMLMessageFormatter.XML_DOMAIN,
158                 "InvalidASCII", new Object JavaDoc [] {Integer.toString(b0)});
159         }
160         return b0;
161     } // read():int
162

163     /**
164      * Read characters into a portion of an array. This method will block
165      * until some input is available, an I/O error occurs, or the end of the
166      * stream is reached.
167      *
168      * @param ch Destination buffer
169      * @param offset Offset at which to start storing characters
170      * @param length Maximum number of characters to read
171      *
172      * @return The number of characters read, or -1 if the end of the
173      * stream has been reached
174      *
175      * @exception IOException If an I/O error occurs
176      */

177     public int read(char ch[], int offset, int length) throws IOException JavaDoc {
178         if (length > fBuffer.length) {
179             length = fBuffer.length;
180         }
181         int count = fInputStream.read(fBuffer, 0, length);
182         for (int i = 0; i < count; i++) {
183             int b0 = fBuffer[i];
184             if (b0 < 0) {
185                 throw new MalformedByteSequenceException(fFormatter,
186                     fLocale, XMLMessageFormatter.XML_DOMAIN,
187                     "InvalidASCII", new Object JavaDoc [] {Integer.toString(b0 & 0x0FF)});
188             }
189             ch[offset + i] = (char)b0;
190         }
191         return count;
192     } // read(char[],int,int)
193

194     /**
195      * Skip characters. This method will block until some characters are
196      * available, an I/O error occurs, or the end of the stream is reached.
197      *
198      * @param n The number of characters to skip
199      *
200      * @return The number of characters actually skipped
201      *
202      * @exception IOException If an I/O error occurs
203      */

204     public long skip(long n) throws IOException JavaDoc {
205         return fInputStream.skip(n);
206     } // skip(long):long
207

208     /**
209      * Tell whether this stream is ready to be read.
210      *
211      * @return True if the next read() is guaranteed not to block for input,
212      * false otherwise. Note that returning false does not guarantee that the
213      * next read will block.
214      *
215      * @exception IOException If an I/O error occurs
216      */

217     public boolean ready() throws IOException JavaDoc {
218         return false;
219     } // ready()
220

221     /**
222      * Tell whether this stream supports the mark() operation.
223      */

224     public boolean markSupported() {
225         return fInputStream.markSupported();
226     } // markSupported()
227

228     /**
229      * Mark the present position in the stream. Subsequent calls to reset()
230      * will attempt to reposition the stream to this point. Not all
231      * character-input streams support the mark() operation.
232      *
233      * @param readAheadLimit Limit on the number of characters that may be
234      * read while still preserving the mark. After
235      * reading this many characters, attempting to
236      * reset the stream may fail.
237      *
238      * @exception IOException If the stream does not support mark(),
239      * or if some other I/O error occurs
240      */

241     public void mark(int readAheadLimit) throws IOException JavaDoc {
242         fInputStream.mark(readAheadLimit);
243     } // mark(int)
244

245     /**
246      * Reset the stream. If the stream has been marked, then attempt to
247      * reposition it at the mark. If the stream has not been marked, then
248      * attempt to reset it in some way appropriate to the particular stream,
249      * for example by repositioning it to its starting point. Not all
250      * character-input streams support the reset() operation, and some support
251      * reset() without supporting mark().
252      *
253      * @exception IOException If the stream has not been marked,
254      * or if the mark has been invalidated,
255      * or if the stream does not support reset(),
256      * or if some other I/O error occurs
257      */

258     public void reset() throws IOException JavaDoc {
259         fInputStream.reset();
260     } // reset()
261

262     /**
263      * Close the stream. Once a stream has been closed, further read(),
264      * ready(), mark(), or reset() invocations will throw an IOException.
265      * Closing a previously-closed stream, however, has no effect.
266      *
267      * @exception IOException If an I/O error occurs
268      */

269      public void close() throws IOException JavaDoc {
270          fInputStream.close();
271      } // close()
272

273 } // class ASCIIReader
274
Popular Tags