001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.xerces.impl.io;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.io.Reader;
023:
024: /**
025: * <p>Reader for the ISO-8859-1 encoding.</p>
026: *
027: * @xerces.internal
028: *
029: * @author Michael Glavassevich, IBM
030: *
031: * @version $Id: Latin1Reader.java 446716 2006-09-15 20:28:48Z mrglavas $
032: */
033: public class Latin1Reader extends Reader {
034:
035: //
036: // Constants
037: //
038:
039: /** Default byte buffer size (2048). */
040: public static final int DEFAULT_BUFFER_SIZE = 2048;
041:
042: //
043: // Data
044: //
045:
046: /** Input stream. */
047: protected final InputStream fInputStream;
048:
049: /** Byte buffer. */
050: protected final byte[] fBuffer;
051:
052: //
053: // Constructors
054: //
055:
056: /**
057: * Constructs an ISO-8859-1 reader from the specified input stream
058: * using the default buffer size.
059: *
060: * @param inputStream The input stream.
061: */
062: public Latin1Reader(InputStream inputStream) {
063: this (inputStream, DEFAULT_BUFFER_SIZE);
064: } // <init>(InputStream)
065:
066: /**
067: * Constructs an ISO-8859-1 reader from the specified input stream
068: * and buffer size.
069: *
070: * @param inputStream The input stream.
071: * @param size The initial buffer size.
072: */
073: public Latin1Reader(InputStream inputStream, int size) {
074: this (inputStream, new byte[size]);
075: } // <init>(InputStream, int)
076:
077: /**
078: * Constructs an ISO-8859-1 reader from the specified input stream and buffer.
079: *
080: * @param inputStream The input stream.
081: * @param buffer The byte buffer.
082: */
083: public Latin1Reader(InputStream inputStream, byte[] buffer) {
084: fInputStream = inputStream;
085: fBuffer = buffer;
086: } // <init>(InputStream, byte[])
087:
088: //
089: // Reader methods
090: //
091:
092: /**
093: * Read a single character. This method will block until a character is
094: * available, an I/O error occurs, or the end of the stream is reached.
095: *
096: * <p> Subclasses that intend to support efficient single-character input
097: * should override this method.
098: *
099: * @return The character read, as an integer in the range 0 to 255
100: * (<tt>0x00-0xff</tt>), or -1 if the end of the stream has
101: * been reached
102: *
103: * @exception IOException If an I/O error occurs
104: */
105: public int read() throws IOException {
106: return fInputStream.read();
107: } // read():int
108:
109: /**
110: * Read characters into a portion of an array. This method will block
111: * until some input is available, an I/O error occurs, or the end of the
112: * stream is reached.
113: *
114: * @param ch Destination buffer
115: * @param offset Offset at which to start storing characters
116: * @param length Maximum number of characters to read
117: *
118: * @return The number of characters read, or -1 if the end of the
119: * stream has been reached
120: *
121: * @exception IOException If an I/O error occurs
122: */
123: public int read(char ch[], int offset, int length)
124: throws IOException {
125: if (length > fBuffer.length) {
126: length = fBuffer.length;
127: }
128: int count = fInputStream.read(fBuffer, 0, length);
129: for (int i = 0; i < count; ++i) {
130: ch[offset + i] = (char) (fBuffer[i] & 0xff);
131: }
132: return count;
133: } // read(char[],int,int)
134:
135: /**
136: * Skip characters. This method will block until some characters are
137: * available, an I/O error occurs, or the end of the stream is reached.
138: *
139: * @param n The number of characters to skip
140: *
141: * @return The number of characters actually skipped
142: *
143: * @exception IOException If an I/O error occurs
144: */
145: public long skip(long n) throws IOException {
146: return fInputStream.skip(n);
147: } // skip(long):long
148:
149: /**
150: * Tell whether this stream is ready to be read.
151: *
152: * @return True if the next read() is guaranteed not to block for input,
153: * false otherwise. Note that returning false does not guarantee that the
154: * next read will block.
155: *
156: * @exception IOException If an I/O error occurs
157: */
158: public boolean ready() throws IOException {
159: return false;
160: } // ready()
161:
162: /**
163: * Tell whether this stream supports the mark() operation.
164: */
165: public boolean markSupported() {
166: return fInputStream.markSupported();
167: } // markSupported()
168:
169: /**
170: * Mark the present position in the stream. Subsequent calls to reset()
171: * will attempt to reposition the stream to this point. Not all
172: * character-input streams support the mark() operation.
173: *
174: * @param readAheadLimit Limit on the number of characters that may be
175: * read while still preserving the mark. After
176: * reading this many characters, attempting to
177: * reset the stream may fail.
178: *
179: * @exception IOException If the stream does not support mark(),
180: * or if some other I/O error occurs
181: */
182: public void mark(int readAheadLimit) throws IOException {
183: fInputStream.mark(readAheadLimit);
184: } // mark(int)
185:
186: /**
187: * Reset the stream. If the stream has been marked, then attempt to
188: * reposition it at the mark. If the stream has not been marked, then
189: * attempt to reset it in some way appropriate to the particular stream,
190: * for example by repositioning it to its starting point. Not all
191: * character-input streams support the reset() operation, and some support
192: * reset() without supporting mark().
193: *
194: * @exception IOException If the stream has not been marked,
195: * or if the mark has been invalidated,
196: * or if the stream does not support reset(),
197: * or if some other I/O error occurs
198: */
199: public void reset() throws IOException {
200: fInputStream.reset();
201: } // reset()
202:
203: /**
204: * Close the stream. Once a stream has been closed, further read(),
205: * ready(), mark(), or reset() invocations will throw an IOException.
206: * Closing a previously-closed stream, however, has no effect.
207: *
208: * @exception IOException If an I/O error occurs
209: */
210: public void close() throws IOException {
211: fInputStream.close();
212: } // close()
213:
214: } // class Latin1Reader
|