001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2000-2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "Xerces" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 1999, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package com.sun.xml.stream.xerces.impl.io;
059:
060: import java.io.*;
061: import java.util.Locale;
062:
063: import com.sun.xml.stream.util.BufferAllocator;
064: import com.sun.xml.stream.util.ThreadLocalBufferAllocator;
065:
066: import com.sun.xml.stream.xerces.util.MessageFormatter;
067:
068: /**
069: * A simple ASCII byte reader. This is an optimized reader for reading
070: * byte streams that only contain 7-bit ASCII characters.
071: *
072: * @author Andy Clark, IBM
073: *
074: * @version $Id: ASCIIReader.java,v 1.4 2006/11/30 16:17:47 spericas Exp $
075: */
076: public class ASCIIReader extends Reader {
077:
078: //
079: // Constants
080: //
081:
082: /** Default byte buffer size (2048). */
083: public static final int DEFAULT_BUFFER_SIZE = 2048;
084:
085: //
086: // Data
087: //
088:
089: /** Input stream. */
090: protected InputStream fInputStream;
091:
092: /** Byte buffer. */
093: protected byte[] fBuffer;
094:
095: // message formatter; used to produce localized
096: // exception messages
097: private MessageFormatter fFormatter = null;
098:
099: //Locale to use for messages
100: private Locale 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 inputStream,
115: MessageFormatter messageFormatter, Locale 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 inputStream, int size,
129: MessageFormatter messageFormatter, Locale locale) {
130: fInputStream = inputStream;
131: BufferAllocator ba = ThreadLocalBufferAllocator
132: .getBufferAllocator();
133: fBuffer = ba.getByteBuffer(size);
134: if (fBuffer == null) {
135: fBuffer = new byte[size];
136: }
137: fFormatter = messageFormatter;
138: fLocale = locale;
139: } // <init>(InputStream,int, MessageFormatter, Locale)
140:
141: //
142: // Reader methods
143: //
144:
145: /**
146: * Read a single character. This method will block until a character is
147: * available, an I/O error occurs, or the end of the stream is reached.
148: *
149: * <p> Subclasses that intend to support efficient single-character input
150: * should override this method.
151: *
152: * @return The character read, as an integer in the range 0 to 127
153: * (<tt>0x00-0x7f</tt>), or -1 if the end of the stream has
154: * been reached
155: *
156: * @exception IOException If an I/O error occurs
157: */
158: public int read() throws IOException {
159: int b0 = fInputStream.read();
160: if (b0 > 0x80) {
161: throw new IOException(fFormatter.formatMessage(fLocale,
162: "InvalidASCII",
163: new Object[] { Integer.toString(b0) }));
164: }
165: return b0;
166: } // read():int
167:
168: /**
169: * Read characters into a portion of an array. This method will block
170: * until some input is available, an I/O error occurs, or the end of the
171: * stream is reached.
172: *
173: * @param ch Destination buffer
174: * @param offset Offset at which to start storing characters
175: * @param length Maximum number of characters to read
176: *
177: * @return The number of characters read, or -1 if the end of the
178: * stream has been reached
179: *
180: * @exception IOException If an I/O error occurs
181: */
182: public int read(char ch[], int offset, int length)
183: throws IOException {
184: if (length > fBuffer.length) {
185: length = fBuffer.length;
186: }
187: int count = fInputStream.read(fBuffer, 0, length);
188: for (int i = 0; i < count; i++) {
189: int b0 = fBuffer[i];
190: if (b0 > 0x80) {
191: throw new IOException(fFormatter.formatMessage(fLocale,
192: "InvalidASCII", new Object[] { Integer
193: .toString(b0) }));
194: }
195: ch[offset + i] = (char) b0;
196: }
197: return count;
198: } // read(char[],int,int)
199:
200: /**
201: * Skip characters. This method will block until some characters are
202: * available, an I/O error occurs, or the end of the stream is reached.
203: *
204: * @param n The number of characters to skip
205: *
206: * @return The number of characters actually skipped
207: *
208: * @exception IOException If an I/O error occurs
209: */
210: public long skip(long n) throws IOException {
211: return fInputStream.skip(n);
212: } // skip(long):long
213:
214: /**
215: * Tell whether this stream is ready to be read.
216: *
217: * @return True if the next read() is guaranteed not to block for input,
218: * false otherwise. Note that returning false does not guarantee that the
219: * next read will block.
220: *
221: * @exception IOException If an I/O error occurs
222: */
223: public boolean ready() throws IOException {
224: return false;
225: } // ready()
226:
227: /**
228: * Tell whether this stream supports the mark() operation.
229: */
230: public boolean markSupported() {
231: return fInputStream.markSupported();
232: } // markSupported()
233:
234: /**
235: * Mark the present position in the stream. Subsequent calls to reset()
236: * will attempt to reposition the stream to this point. Not all
237: * character-input streams support the mark() operation.
238: *
239: * @param readAheadLimit Limit on the number of characters that may be
240: * read while still preserving the mark. After
241: * reading this many characters, attempting to
242: * reset the stream may fail.
243: *
244: * @exception IOException If the stream does not support mark(),
245: * or if some other I/O error occurs
246: */
247: public void mark(int readAheadLimit) throws IOException {
248: fInputStream.mark(readAheadLimit);
249: } // mark(int)
250:
251: /**
252: * Reset the stream. If the stream has been marked, then attempt to
253: * reposition it at the mark. If the stream has not been marked, then
254: * attempt to reset it in some way appropriate to the particular stream,
255: * for example by repositioning it to its starting point. Not all
256: * character-input streams support the reset() operation, and some support
257: * reset() without supporting mark().
258: *
259: * @exception IOException If the stream has not been marked,
260: * or if the mark has been invalidated,
261: * or if the stream does not support reset(),
262: * or if some other I/O error occurs
263: */
264: public void reset() throws IOException {
265: fInputStream.reset();
266: } // reset()
267:
268: /**
269: * Close the stream. Once a stream has been closed, further read(),
270: * ready(), mark(), or reset() invocations will throw an IOException.
271: * Closing a previously-closed stream, however, has no effect.
272: *
273: * @exception IOException If an I/O error occurs
274: */
275: public void close() throws IOException {
276: BufferAllocator ba = ThreadLocalBufferAllocator
277: .getBufferAllocator();
278: ba.returnByteBuffer(fBuffer);
279: fBuffer = null;
280: fInputStream.close();
281: } // close()
282:
283: } // class ASCIIReader
|