1 16 17 package org.apache.xerces.impl.io; 18 19 import java.io.InputStream ; 20 import java.io.IOException ; 21 import java.io.Reader ; 22 import java.util.Locale ; 23 import org.apache.xerces.util.MessageFormatter; 24 import org.apache.xerces.impl.msg.XMLMessageFormatter; 25 26 36 public class ASCIIReader 37 extends Reader { 38 39 43 44 public static final int DEFAULT_BUFFER_SIZE = 2048; 45 46 50 51 protected InputStream fInputStream; 52 53 54 protected byte[] fBuffer; 55 56 private MessageFormatter fFormatter = null; 59 60 private Locale fLocale = null; 62 63 67 75 public ASCIIReader(InputStream inputStream, MessageFormatter messageFormatter, 76 Locale locale) { 77 this(inputStream, DEFAULT_BUFFER_SIZE, messageFormatter, locale); 78 } 80 89 public ASCIIReader(InputStream inputStream, int size, 90 MessageFormatter messageFormatter, Locale locale) { 91 fInputStream = inputStream; 92 fBuffer = new byte[size]; 93 fFormatter = messageFormatter; 94 fLocale = locale; 95 } 97 101 114 public int read() throws IOException { 115 int b0 = fInputStream.read(); 116 if (b0 >= 0x80) { 117 throw new MalformedByteSequenceException(fFormatter, 118 fLocale, XMLMessageFormatter.XML_DOMAIN, 119 "InvalidASCII", new Object [] {Integer.toString(b0)}); 120 } 121 return b0; 122 } 124 138 public int read(char ch[], int offset, int length) throws IOException { 139 if (length > fBuffer.length) { 140 length = fBuffer.length; 141 } 142 int count = fInputStream.read(fBuffer, 0, length); 143 for (int i = 0; i < count; i++) { 144 int b0 = fBuffer[i]; 145 if (b0 < 0) { 146 throw new MalformedByteSequenceException(fFormatter, 147 fLocale, XMLMessageFormatter.XML_DOMAIN, 148 "InvalidASCII", new Object [] {Integer.toString(b0 & 0x0FF)}); 149 } 150 ch[offset + i] = (char)b0; 151 } 152 return count; 153 } 155 165 public long skip(long n) throws IOException { 166 return fInputStream.skip(n); 167 } 169 178 public boolean ready() throws IOException { 179 return false; 180 } 182 185 public boolean markSupported() { 186 return fInputStream.markSupported(); 187 } 189 202 public void mark(int readAheadLimit) throws IOException { 203 fInputStream.mark(readAheadLimit); 204 } 206 219 public void reset() throws IOException { 220 fInputStream.reset(); 221 } 223 230 public void close() throws IOException { 231 fInputStream.close(); 232 } 234 } | Popular Tags |