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 23 33 public class UCSReader extends Reader { 34 35 39 43 public static final int DEFAULT_BUFFER_SIZE = 8192; 44 45 public static final short UCS2LE = 1; 46 public static final short UCS2BE = 2; 47 public static final short UCS4LE = 4; 48 public static final short UCS4BE = 8; 49 50 54 55 protected InputStream fInputStream; 56 57 58 protected byte[] fBuffer; 59 60 protected short fEncoding; 62 63 67 75 public UCSReader(InputStream inputStream, short encoding) { 76 this(inputStream, DEFAULT_BUFFER_SIZE, encoding); 77 } 79 88 public UCSReader(InputStream inputStream, int size, short encoding) { 89 fInputStream = inputStream; 90 fBuffer = new byte[size]; 91 fEncoding = encoding; 92 } 94 98 111 public int read() throws IOException { 112 int b0 = fInputStream.read() & 0xff; 113 if (b0 == 0xff) 114 return -1; 115 int b1 = fInputStream.read() & 0xff; 116 if (b1 == 0xff) 117 return -1; 118 if(fEncoding >=4) { 119 int b2 = fInputStream.read() & 0xff; 120 if (b2 == 0xff) 121 return -1; 122 int b3 = fInputStream.read() & 0xff; 123 if (b3 == 0xff) 124 return -1; 125 System.err.println("b0 is " + (b0 & 0xff) + " b1 " + (b1 & 0xff) + " b2 " + (b2 & 0xff) + " b3 " + (b3 & 0xff)); 126 if (fEncoding == UCS4BE) 127 return (b0<<24)+(b1<<16)+(b2<<8)+b3; 128 else 129 return (b3<<24)+(b2<<16)+(b1<<8)+b0; 130 } else { if (fEncoding == UCS2BE) 132 return (b0<<8)+b1; 133 else 134 return (b1<<8)+b0; 135 } 136 } 138 152 public int read(char ch[], int offset, int length) throws IOException { 153 int byteLength = length << ((fEncoding >= 4)?2:1); 154 if (byteLength > fBuffer.length) { 155 byteLength = fBuffer.length; 156 } 157 int count = fInputStream.read(fBuffer, 0, byteLength); 158 if(count == -1) return -1; 159 if(fEncoding >= 4) { int numToRead = (4 - (count & 3) & 3); 163 for(int i=0; i<numToRead; i++) { 164 int charRead = fInputStream.read(); 165 if(charRead == -1) { for (int j = i;j<numToRead; j++) 167 fBuffer[count+j] = 0; 168 break; 169 } else { 170 fBuffer[count+i] = (byte)charRead; 171 } 172 } 173 count += numToRead; 174 } else { 175 int numToRead = count & 1; 176 if(numToRead != 0) { 177 count++; 178 int charRead = fInputStream.read(); 179 if(charRead == -1) { fBuffer[count] = 0; 181 } else { 182 fBuffer[count] = (byte)charRead; 183 } 184 } 185 } 186 187 int numChars = count >> ((fEncoding >= 4)?2:1); 189 int curPos = 0; 190 for (int i = 0; i < numChars; i++) { 191 int b0 = fBuffer[curPos++] & 0xff; 192 int b1 = fBuffer[curPos++] & 0xff; 193 if(fEncoding >=4) { 194 int b2 = fBuffer[curPos++] & 0xff; 195 int b3 = fBuffer[curPos++] & 0xff; 196 if (fEncoding == UCS4BE) 197 ch[offset+i] = (char)((b0<<24)+(b1<<16)+(b2<<8)+b3); 198 else 199 ch[offset+i] = (char)((b3<<24)+(b2<<16)+(b1<<8)+b0); 200 } else { if (fEncoding == UCS2BE) 202 ch[offset+i] = (char)((b0<<8)+b1); 203 else 204 ch[offset+i] = (char)((b1<<8)+b0); 205 } 206 } 207 return numChars; 208 } 210 220 public long skip(long n) throws IOException { 221 int charWidth = (fEncoding >=4)?2:1; 228 long bytesSkipped = fInputStream.skip(n<<charWidth); 229 if((bytesSkipped & (charWidth | 1)) == 0) return bytesSkipped >> charWidth; 230 return (bytesSkipped >> charWidth) + 1; 231 } 233 242 public boolean ready() throws IOException { 243 return false; 244 } 246 249 public boolean markSupported() { 250 return fInputStream.markSupported(); 251 } 253 266 public void mark(int readAheadLimit) throws IOException { 267 fInputStream.mark(readAheadLimit); 268 } 270 283 public void reset() throws IOException { 284 fInputStream.reset(); 285 } 287 294 public void close() throws IOException { 295 fInputStream.close(); 296 } 298 } | Popular Tags |