1 17 18 package org.apache.jasper.xmlparser; 19 20 import java.io.InputStream ; 21 import java.io.IOException ; 22 import java.io.Reader ; 23 24 32 public class UCSReader extends Reader { 33 34 private org.apache.commons.logging.Log log= 35 org.apache.commons.logging.LogFactory.getLog( UCSReader.class ); 36 37 41 45 public static final int DEFAULT_BUFFER_SIZE = 8192; 46 47 public static final short UCS2LE = 1; 48 public static final short UCS2BE = 2; 49 public static final short UCS4LE = 4; 50 public static final short UCS4BE = 8; 51 52 56 57 protected InputStream fInputStream; 58 59 60 protected byte[] fBuffer; 61 62 protected short fEncoding; 64 65 69 77 public UCSReader(InputStream inputStream, short encoding) { 78 this(inputStream, DEFAULT_BUFFER_SIZE, encoding); 79 } 81 90 public UCSReader(InputStream inputStream, int size, short encoding) { 91 fInputStream = inputStream; 92 fBuffer = new byte[size]; 93 fEncoding = encoding; 94 } 96 100 113 public int read() throws IOException { 114 int b0 = fInputStream.read() & 0xff; 115 if (b0 == 0xff) 116 return -1; 117 int b1 = fInputStream.read() & 0xff; 118 if (b1 == 0xff) 119 return -1; 120 if(fEncoding >=4) { 121 int b2 = fInputStream.read() & 0xff; 122 if (b2 == 0xff) 123 return -1; 124 int b3 = fInputStream.read() & 0xff; 125 if (b3 == 0xff) 126 return -1; 127 if (log.isDebugEnabled()) 128 log.debug("b0 is " + (b0 & 0xff) + " b1 " + (b1 & 0xff) + " b2 " + (b2 & 0xff) + " b3 " + (b3 & 0xff)); 129 if (fEncoding == UCS4BE) 130 return (b0<<24)+(b1<<16)+(b2<<8)+b3; 131 else 132 return (b3<<24)+(b2<<16)+(b1<<8)+b0; 133 } else { if (fEncoding == UCS2BE) 135 return (b0<<8)+b1; 136 else 137 return (b1<<8)+b0; 138 } 139 } 141 155 public int read(char ch[], int offset, int length) throws IOException { 156 int byteLength = length << ((fEncoding >= 4)?2:1); 157 if (byteLength > fBuffer.length) { 158 byteLength = fBuffer.length; 159 } 160 int count = fInputStream.read(fBuffer, 0, byteLength); 161 if(count == -1) return -1; 162 if(fEncoding >= 4) { int numToRead = (4 - (count & 3) & 3); 166 for(int i=0; i<numToRead; i++) { 167 int charRead = fInputStream.read(); 168 if(charRead == -1) { for (int j = i;j<numToRead; j++) 170 fBuffer[count+j] = 0; 171 break; 172 } else { 173 fBuffer[count+i] = (byte)charRead; 174 } 175 } 176 count += numToRead; 177 } else { 178 int numToRead = count & 1; 179 if(numToRead != 0) { 180 count++; 181 int charRead = fInputStream.read(); 182 if(charRead == -1) { fBuffer[count] = 0; 184 } else { 185 fBuffer[count] = (byte)charRead; 186 } 187 } 188 } 189 190 int numChars = count >> ((fEncoding >= 4)?2:1); 192 int curPos = 0; 193 for (int i = 0; i < numChars; i++) { 194 int b0 = fBuffer[curPos++] & 0xff; 195 int b1 = fBuffer[curPos++] & 0xff; 196 if(fEncoding >=4) { 197 int b2 = fBuffer[curPos++] & 0xff; 198 int b3 = fBuffer[curPos++] & 0xff; 199 if (fEncoding == UCS4BE) 200 ch[offset+i] = (char)((b0<<24)+(b1<<16)+(b2<<8)+b3); 201 else 202 ch[offset+i] = (char)((b3<<24)+(b2<<16)+(b1<<8)+b0); 203 } else { if (fEncoding == UCS2BE) 205 ch[offset+i] = (char)((b0<<8)+b1); 206 else 207 ch[offset+i] = (char)((b1<<8)+b0); 208 } 209 } 210 return numChars; 211 } 213 223 public long skip(long n) throws IOException { 224 int charWidth = (fEncoding >=4)?2:1; 231 long bytesSkipped = fInputStream.skip(n<<charWidth); 232 if((bytesSkipped & (charWidth | 1)) == 0) return bytesSkipped >> charWidth; 233 return (bytesSkipped >> charWidth) + 1; 234 } 236 245 public boolean ready() throws IOException { 246 return false; 247 } 249 252 public boolean markSupported() { 253 return fInputStream.markSupported(); 254 } 256 269 public void mark(int readAheadLimit) throws IOException { 270 fInputStream.mark(readAheadLimit); 271 } 273 286 public void reset() throws IOException { 287 fInputStream.reset(); 288 } 290 297 public void close() throws IOException { 298 fInputStream.close(); 299 } 301 } | Popular Tags |