1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.xerces.impl.io; 19 20 import java.io.IOException; 21 import java.io.InputStream; 22 import java.io.Reader; 23 24 /** 25 * <p>Reader for the ISO-8859-1 encoding.</p> 26 * 27 * @xerces.internal 28 * 29 * @author Michael Glavassevich, IBM 30 * 31 * @version $Id: Latin1Reader.java 446716 2006-09-15 20:28:48Z mrglavas $ 32 */ 33 public class Latin1Reader 34 extends Reader { 35 36 // 37 // Constants 38 // 39 40 /** Default byte buffer size (2048). */ 41 public static final int DEFAULT_BUFFER_SIZE = 2048; 42 43 // 44 // Data 45 // 46 47 /** Input stream. */ 48 protected final InputStream fInputStream; 49 50 /** Byte buffer. */ 51 protected final byte[] fBuffer; 52 53 // 54 // Constructors 55 // 56 57 /** 58 * Constructs an ISO-8859-1 reader from the specified input stream 59 * using the default buffer size. 60 * 61 * @param inputStream The input stream. 62 */ 63 public Latin1Reader(InputStream inputStream) { 64 this(inputStream, DEFAULT_BUFFER_SIZE); 65 } // <init>(InputStream) 66 67 /** 68 * Constructs an ISO-8859-1 reader from the specified input stream 69 * and buffer size. 70 * 71 * @param inputStream The input stream. 72 * @param size The initial buffer size. 73 */ 74 public Latin1Reader(InputStream inputStream, int size) { 75 this(inputStream, new byte[size]); 76 } // <init>(InputStream, int) 77 78 /** 79 * Constructs an ISO-8859-1 reader from the specified input stream and buffer. 80 * 81 * @param inputStream The input stream. 82 * @param buffer The byte buffer. 83 */ 84 public Latin1Reader(InputStream inputStream, byte [] buffer) { 85 fInputStream = inputStream; 86 fBuffer = buffer; 87 } // <init>(InputStream, byte[]) 88 89 // 90 // Reader methods 91 // 92 93 /** 94 * Read a single character. This method will block until a character is 95 * available, an I/O error occurs, or the end of the stream is reached. 96 * 97 * <p> Subclasses that intend to support efficient single-character input 98 * should override this method. 99 * 100 * @return The character read, as an integer in the range 0 to 255 101 * (<tt>0x00-0xff</tt>), or -1 if the end of the stream has 102 * been reached 103 * 104 * @exception IOException If an I/O error occurs 105 */ 106 public int read() throws IOException { 107 return fInputStream.read(); 108 } // read():int 109 110 /** 111 * Read characters into a portion of an array. This method will block 112 * until some input is available, an I/O error occurs, or the end of the 113 * stream is reached. 114 * 115 * @param ch Destination buffer 116 * @param offset Offset at which to start storing characters 117 * @param length Maximum number of characters to read 118 * 119 * @return The number of characters read, or -1 if the end of the 120 * stream has been reached 121 * 122 * @exception IOException If an I/O error occurs 123 */ 124 public int read(char ch[], int offset, int length) 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