1 57 58 package com.sun.org.apache.xerces.internal.impl.io; 59 60 import java.io.InputStream ; 61 import java.io.IOException ; 62 import java.io.Reader ; 63 import java.util.Locale ; 64 import com.sun.org.apache.xerces.internal.util.MessageFormatter; 65 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; 66 67 75 public class ASCIIReader 76 extends Reader { 77 78 82 83 public static final int DEFAULT_BUFFER_SIZE = 2048; 84 85 89 90 protected InputStream fInputStream; 91 92 93 protected byte[] fBuffer; 94 95 private MessageFormatter fFormatter = null; 98 99 private Locale fLocale = null; 101 102 106 114 public ASCIIReader(InputStream inputStream, MessageFormatter messageFormatter, 115 Locale locale) { 116 this(inputStream, DEFAULT_BUFFER_SIZE, messageFormatter, locale); 117 } 119 128 public ASCIIReader(InputStream inputStream, int size, 129 MessageFormatter messageFormatter, Locale locale) { 130 fInputStream = inputStream; 131 fBuffer = new byte[size]; 132 fFormatter = messageFormatter; 133 fLocale = locale; 134 } 136 140 153 public int read() throws IOException { 154 int b0 = fInputStream.read(); 155 if (b0 >= 0x80) { 156 throw new MalformedByteSequenceException(fFormatter, 157 fLocale, XMLMessageFormatter.XML_DOMAIN, 158 "InvalidASCII", new Object [] {Integer.toString(b0)}); 159 } 160 return b0; 161 } 163 177 public int read(char ch[], int offset, int length) throws IOException { 178 if (length > fBuffer.length) { 179 length = fBuffer.length; 180 } 181 int count = fInputStream.read(fBuffer, 0, length); 182 for (int i = 0; i < count; i++) { 183 int b0 = fBuffer[i]; 184 if (b0 < 0) { 185 throw new MalformedByteSequenceException(fFormatter, 186 fLocale, XMLMessageFormatter.XML_DOMAIN, 187 "InvalidASCII", new Object [] {Integer.toString(b0 & 0x0FF)}); 188 } 189 ch[offset + i] = (char)b0; 190 } 191 return count; 192 } 194 204 public long skip(long n) throws IOException { 205 return fInputStream.skip(n); 206 } 208 217 public boolean ready() throws IOException { 218 return false; 219 } 221 224 public boolean markSupported() { 225 return fInputStream.markSupported(); 226 } 228 241 public void mark(int readAheadLimit) throws IOException { 242 fInputStream.mark(readAheadLimit); 243 } 245 258 public void reset() throws IOException { 259 fInputStream.reset(); 260 } 262 269 public void close() throws IOException { 270 fInputStream.close(); 271 } 273 } | Popular Tags |