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 64 72 public class UCSReader extends Reader { 73 74 78 82 public static final int DEFAULT_BUFFER_SIZE = 8192; 83 84 public static final short UCS2LE = 1; 85 public static final short UCS2BE = 2; 86 public static final short UCS4LE = 4; 87 public static final short UCS4BE = 8; 88 89 93 94 protected InputStream fInputStream; 95 96 97 protected byte[] fBuffer; 98 99 protected short fEncoding; 101 102 106 114 public UCSReader(InputStream inputStream, short encoding) { 115 this(inputStream, DEFAULT_BUFFER_SIZE, encoding); 116 } 118 127 public UCSReader(InputStream inputStream, int size, short encoding) { 128 fInputStream = inputStream; 129 fBuffer = new byte[size]; 130 fEncoding = encoding; 131 } 133 137 150 public int read() throws IOException { 151 int b0 = fInputStream.read() & 0xff; 152 if (b0 == 0xff) 153 return -1; 154 int b1 = fInputStream.read() & 0xff; 155 if (b1 == 0xff) 156 return -1; 157 if(fEncoding >=4) { 158 int b2 = fInputStream.read() & 0xff; 159 if (b2 == 0xff) 160 return -1; 161 int b3 = fInputStream.read() & 0xff; 162 if (b3 == 0xff) 163 return -1; 164 System.err.println("b0 is " + (b0 & 0xff) + " b1 " + (b1 & 0xff) + " b2 " + (b2 & 0xff) + " b3 " + (b3 & 0xff)); 165 if (fEncoding == UCS4BE) 166 return (b0<<24)+(b1<<16)+(b2<<8)+b3; 167 else 168 return (b3<<24)+(b2<<16)+(b1<<8)+b0; 169 } else { if (fEncoding == UCS2BE) 171 return (b0<<8)+b1; 172 else 173 return (b1<<8)+b0; 174 } 175 } 177 191 public int read(char ch[], int offset, int length) throws IOException { 192 int byteLength = length << ((fEncoding >= 4)?2:1); 193 if (byteLength > fBuffer.length) { 194 byteLength = fBuffer.length; 195 } 196 int count = fInputStream.read(fBuffer, 0, byteLength); 197 if(count == -1) return -1; 198 if(fEncoding >= 4) { int numToRead = (4 - (count & 3) & 3); 202 for(int i=0; i<numToRead; i++) { 203 int charRead = fInputStream.read(); 204 if(charRead == -1) { for (int j = i;j<numToRead; j++) 206 fBuffer[count+j] = 0; 207 break; 208 } else { 209 fBuffer[count+i] = (byte)charRead; 210 } 211 } 212 count += numToRead; 213 } else { 214 int numToRead = count & 1; 215 if(numToRead != 0) { 216 count++; 217 int charRead = fInputStream.read(); 218 if(charRead == -1) { fBuffer[count] = 0; 220 } else { 221 fBuffer[count] = (byte)charRead; 222 } 223 } 224 } 225 226 int numChars = count >> ((fEncoding >= 4)?2:1); 228 int curPos = 0; 229 for (int i = 0; i < numChars; i++) { 230 int b0 = fBuffer[curPos++] & 0xff; 231 int b1 = fBuffer[curPos++] & 0xff; 232 if(fEncoding >=4) { 233 int b2 = fBuffer[curPos++] & 0xff; 234 int b3 = fBuffer[curPos++] & 0xff; 235 if (fEncoding == UCS4BE) 236 ch[offset+i] = (char)((b0<<24)+(b1<<16)+(b2<<8)+b3); 237 else 238 ch[offset+i] = (char)((b3<<24)+(b2<<16)+(b1<<8)+b0); 239 } else { if (fEncoding == UCS2BE) 241 ch[offset+i] = (char)((b0<<8)+b1); 242 else 243 ch[offset+i] = (char)((b1<<8)+b0); 244 } 245 } 246 return numChars; 247 } 249 259 public long skip(long n) throws IOException { 260 int charWidth = (fEncoding >=4)?2:1; 267 long bytesSkipped = fInputStream.skip(n<<charWidth); 268 if((bytesSkipped & (charWidth | 1)) == 0) return bytesSkipped >> charWidth; 269 return (bytesSkipped >> charWidth) + 1; 270 } 272 281 public boolean ready() throws IOException { 282 return false; 283 } 285 288 public boolean markSupported() { 289 return fInputStream.markSupported(); 290 } 292 305 public void mark(int readAheadLimit) throws IOException { 306 fInputStream.mark(readAheadLimit); 307 } 309 322 public void reset() throws IOException { 323 fInputStream.reset(); 324 } 326 333 public void close() throws IOException { 334 fInputStream.close(); 335 } 337 } | Popular Tags |