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. package com.sun.org.apache.xerces.internal.impl.io;
  58. import java.io.InputStream;
  59. import java.io.IOException;
  60. import java.io.Reader;
  61. import java.util.Locale;
  62. import com.sun.org.apache.xerces.internal.util.MessageFormatter;
  63. import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter;
  64. /**
  65. * A simple ASCII byte reader. This is an optimized reader for reading
  66. * byte streams that only contain 7-bit ASCII characters.
  67. *
  68. * @author Andy Clark, IBM
  69. *
  70. * @version $Id: ASCIIReader.java,v 1.7 2004/03/04 19:27:13 mrglavas Exp $
  71. */
  72. public class ASCIIReader
  73. extends Reader {
  74. //
  75. // Constants
  76. //
  77. /** Default byte buffer size (2048). */
  78. public static final int DEFAULT_BUFFER_SIZE = 2048;
  79. //
  80. // Data
  81. //
  82. /** Input stream. */
  83. protected InputStream fInputStream;
  84. /** Byte buffer. */
  85. protected byte[] fBuffer;
  86. // message formatter; used to produce localized
  87. // exception messages
  88. private MessageFormatter fFormatter = null;
  89. //Locale to use for messages
  90. private Locale fLocale = null;
  91. //
  92. // Constructors
  93. //
  94. /**
  95. * Constructs an ASCII reader from the specified input stream
  96. * using the default buffer size.
  97. *
  98. * @param inputStream The input stream.
  99. * @param messageFormatter the MessageFormatter to use to message reporting.
  100. * @param locale the Locale for which messages are to be reported
  101. */
  102. public ASCIIReader(InputStream inputStream, MessageFormatter messageFormatter,
  103. Locale locale) {
  104. this(inputStream, DEFAULT_BUFFER_SIZE, messageFormatter, locale);
  105. } // <init>(InputStream, MessageFormatter, Locale)
  106. /**
  107. * Constructs an ASCII reader from the specified input stream
  108. * and buffer size.
  109. *
  110. * @param inputStream The input stream.
  111. * @param size The initial buffer size.
  112. * @param messageFormatter the MessageFormatter to use to message reporting.
  113. * @param locale the Locale for which messages are to be reported
  114. */
  115. public ASCIIReader(InputStream inputStream, int size,
  116. MessageFormatter messageFormatter, Locale locale) {
  117. fInputStream = inputStream;
  118. fBuffer = new byte[size];
  119. fFormatter = messageFormatter;
  120. fLocale = locale;
  121. } // <init>(InputStream,int, MessageFormatter, Locale)
  122. //
  123. // Reader methods
  124. //
  125. /**
  126. * Read a single character. This method will block until a character is
  127. * available, an I/O error occurs, or the end of the stream is reached.
  128. *
  129. * <p> Subclasses that intend to support efficient single-character input
  130. * should override this method.
  131. *
  132. * @return The character read, as an integer in the range 0 to 127
  133. * (<tt>0x00-0x7f</tt>), or -1 if the end of the stream has
  134. * been reached
  135. *
  136. * @exception IOException If an I/O error occurs
  137. */
  138. public int read() throws IOException {
  139. int b0 = fInputStream.read();
  140. if (b0 >= 0x80) {
  141. throw new MalformedByteSequenceException(fFormatter,
  142. fLocale, XMLMessageFormatter.XML_DOMAIN,
  143. "InvalidASCII", new Object [] {Integer.toString(b0)});
  144. }
  145. return b0;
  146. } // read():int
  147. /**
  148. * Read characters into a portion of an array. This method will block
  149. * until some input is available, an I/O error occurs, or the end of the
  150. * stream is reached.
  151. *
  152. * @param ch Destination buffer
  153. * @param offset Offset at which to start storing characters
  154. * @param length Maximum number of characters to read
  155. *
  156. * @return The number of characters read, or -1 if the end of the
  157. * stream has been reached
  158. *
  159. * @exception IOException If an I/O error occurs
  160. */
  161. public int read(char ch[], int offset, int length) throws IOException {
  162. if (length > fBuffer.length) {
  163. length = fBuffer.length;
  164. }
  165. int count = fInputStream.read(fBuffer, 0, length);
  166. for (int i = 0; i < count; i++) {
  167. int b0 = fBuffer[i];
  168. if (b0 < 0) {
  169. throw new MalformedByteSequenceException(fFormatter,
  170. fLocale, XMLMessageFormatter.XML_DOMAIN,
  171. "InvalidASCII", new Object [] {Integer.toString(b0 & 0x0FF)});
  172. }
  173. ch[offset + i] = (char)b0;
  174. }
  175. return count;
  176. } // read(char[],int,int)
  177. /**
  178. * Skip characters. This method will block until some characters are
  179. * available, an I/O error occurs, or the end of the stream is reached.
  180. *
  181. * @param n The number of characters to skip
  182. *
  183. * @return The number of characters actually skipped
  184. *
  185. * @exception IOException If an I/O error occurs
  186. */
  187. public long skip(long n) throws IOException {
  188. return fInputStream.skip(n);
  189. } // skip(long):long
  190. /**
  191. * Tell whether this stream is ready to be read.
  192. *
  193. * @return True if the next read() is guaranteed not to block for input,
  194. * false otherwise. Note that returning false does not guarantee that the
  195. * next read will block.
  196. *
  197. * @exception IOException If an I/O error occurs
  198. */
  199. public boolean ready() throws IOException {
  200. return false;
  201. } // ready()
  202. /**
  203. * Tell whether this stream supports the mark() operation.
  204. */
  205. public boolean markSupported() {
  206. return fInputStream.markSupported();
  207. } // markSupported()
  208. /**
  209. * Mark the present position in the stream. Subsequent calls to reset()
  210. * will attempt to reposition the stream to this point. Not all
  211. * character-input streams support the mark() operation.
  212. *
  213. * @param readAheadLimit Limit on the number of characters that may be
  214. * read while still preserving the mark. After
  215. * reading this many characters, attempting to
  216. * reset the stream may fail.
  217. *
  218. * @exception IOException If the stream does not support mark(),
  219. * or if some other I/O error occurs
  220. */
  221. public void mark(int readAheadLimit) throws IOException {
  222. fInputStream.mark(readAheadLimit);
  223. } // mark(int)
  224. /**
  225. * Reset the stream. If the stream has been marked, then attempt to
  226. * reposition it at the mark. If the stream has not been marked, then
  227. * attempt to reset it in some way appropriate to the particular stream,
  228. * for example by repositioning it to its starting point. Not all
  229. * character-input streams support the reset() operation, and some support
  230. * reset() without supporting mark().
  231. *
  232. * @exception IOException If the stream has not been marked,
  233. * or if the mark has been invalidated,
  234. * or if the stream does not support reset(),
  235. * or if some other I/O error occurs
  236. */
  237. public void reset() throws IOException {
  238. fInputStream.reset();
  239. } // reset()
  240. /**
  241. * Close the stream. Once a stream has been closed, further read(),
  242. * ready(), mark(), or reset() invocations will throw an IOException.
  243. * Closing a previously-closed stream, however, has no effect.
  244. *
  245. * @exception IOException If an I/O error occurs
  246. */
  247. public void close() throws IOException {
  248. fInputStream.close();
  249. } // close()
  250. } // class ASCIIReader