001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.jasper.xmlparser;
019:
020: import java.io.InputStream;
021: import java.io.IOException;
022: import java.io.Reader;
023:
024: /**
025: * Reader for UCS-2 and UCS-4 encodings.
026: * (i.e., encodings from ISO-10646-UCS-(2|4)).
027: *
028: * @author Neil Graham, IBM
029: *
030: * @version $Id: UCSReader.java 467222 2006-10-24 03:17:11Z markt $
031: */
032: public class UCSReader extends Reader {
033:
034: private org.apache.juli.logging.Log log = org.apache.juli.logging.LogFactory
035: .getLog(UCSReader.class);
036:
037: //
038: // Constants
039: //
040:
041: /** Default byte buffer size (8192, larger than that of ASCIIReader
042: * since it's reasonable to surmise that the average UCS-4-encoded
043: * file should be 4 times as large as the average ASCII-encoded file).
044: */
045: public static final int DEFAULT_BUFFER_SIZE = 8192;
046:
047: public static final short UCS2LE = 1;
048: public static final short UCS2BE = 2;
049: public static final short UCS4LE = 4;
050: public static final short UCS4BE = 8;
051:
052: //
053: // Data
054: //
055:
056: /** Input stream. */
057: protected InputStream fInputStream;
058:
059: /** Byte buffer. */
060: protected byte[] fBuffer;
061:
062: // what kind of data we're dealing with
063: protected short fEncoding;
064:
065: //
066: // Constructors
067: //
068:
069: /**
070: * Constructs an ASCII reader from the specified input stream
071: * using the default buffer size. The Endian-ness and whether this is
072: * UCS-2 or UCS-4 needs also to be known in advance.
073: *
074: * @param inputStream The input stream.
075: * @param encoding One of UCS2LE, UCS2BE, UCS4LE or UCS4BE.
076: */
077: public UCSReader(InputStream inputStream, short encoding) {
078: this (inputStream, DEFAULT_BUFFER_SIZE, encoding);
079: } // <init>(InputStream, short)
080:
081: /**
082: * Constructs an ASCII reader from the specified input stream
083: * and buffer size. The Endian-ness and whether this is
084: * UCS-2 or UCS-4 needs also to be known in advance.
085: *
086: * @param inputStream The input stream.
087: * @param size The initial buffer size.
088: * @param encoding One of UCS2LE, UCS2BE, UCS4LE or UCS4BE.
089: */
090: public UCSReader(InputStream inputStream, int size, short encoding) {
091: fInputStream = inputStream;
092: fBuffer = new byte[size];
093: fEncoding = encoding;
094: } // <init>(InputStream,int,short)
095:
096: //
097: // Reader methods
098: //
099:
100: /**
101: * Read a single character. This method will block until a character is
102: * available, an I/O error occurs, or the end of the stream is reached.
103: *
104: * <p> Subclasses that intend to support efficient single-character input
105: * should override this method.
106: *
107: * @return The character read, as an integer in the range 0 to 127
108: * (<tt>0x00-0x7f</tt>), or -1 if the end of the stream has
109: * been reached
110: *
111: * @exception IOException If an I/O error occurs
112: */
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)
129: + " b2 " + (b2 & 0xff) + " b3 " + (b3 & 0xff));
130: if (fEncoding == UCS4BE)
131: return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
132: else
133: return (b3 << 24) + (b2 << 16) + (b1 << 8) + b0;
134: } else { // UCS-2
135: if (fEncoding == UCS2BE)
136: return (b0 << 8) + b1;
137: else
138: return (b1 << 8) + b0;
139: }
140: } // read():int
141:
142: /**
143: * Read characters into a portion of an array. This method will block
144: * until some input is available, an I/O error occurs, or the end of the
145: * stream is reached.
146: *
147: * @param ch Destination buffer
148: * @param offset Offset at which to start storing characters
149: * @param length Maximum number of characters to read
150: *
151: * @return The number of characters read, or -1 if the end of the
152: * stream has been reached
153: *
154: * @exception IOException If an I/O error occurs
155: */
156: public int read(char ch[], int offset, int length)
157: throws IOException {
158: int byteLength = length << ((fEncoding >= 4) ? 2 : 1);
159: if (byteLength > fBuffer.length) {
160: byteLength = fBuffer.length;
161: }
162: int count = fInputStream.read(fBuffer, 0, byteLength);
163: if (count == -1)
164: return -1;
165: // try and make count be a multiple of the number of bytes we're looking for
166: if (fEncoding >= 4) { // BigEndian
167: // this looks ugly, but it avoids an if at any rate...
168: int numToRead = (4 - (count & 3) & 3);
169: for (int i = 0; i < numToRead; i++) {
170: int charRead = fInputStream.read();
171: if (charRead == -1) { // end of input; something likely went wrong!A Pad buffer with nulls.
172: for (int j = i; j < numToRead; j++)
173: fBuffer[count + j] = 0;
174: break;
175: } else {
176: fBuffer[count + i] = (byte) charRead;
177: }
178: }
179: count += numToRead;
180: } else {
181: int numToRead = count & 1;
182: if (numToRead != 0) {
183: count++;
184: int charRead = fInputStream.read();
185: if (charRead == -1) { // end of input; something likely went wrong!A Pad buffer with nulls.
186: fBuffer[count] = 0;
187: } else {
188: fBuffer[count] = (byte) charRead;
189: }
190: }
191: }
192:
193: // now count is a multiple of the right number of bytes
194: int numChars = count >> ((fEncoding >= 4) ? 2 : 1);
195: int curPos = 0;
196: for (int i = 0; i < numChars; i++) {
197: int b0 = fBuffer[curPos++] & 0xff;
198: int b1 = fBuffer[curPos++] & 0xff;
199: if (fEncoding >= 4) {
200: int b2 = fBuffer[curPos++] & 0xff;
201: int b3 = fBuffer[curPos++] & 0xff;
202: if (fEncoding == UCS4BE)
203: ch[offset + i] = (char) ((b0 << 24) + (b1 << 16)
204: + (b2 << 8) + b3);
205: else
206: ch[offset + i] = (char) ((b3 << 24) + (b2 << 16)
207: + (b1 << 8) + b0);
208: } else { // UCS-2
209: if (fEncoding == UCS2BE)
210: ch[offset + i] = (char) ((b0 << 8) + b1);
211: else
212: ch[offset + i] = (char) ((b1 << 8) + b0);
213: }
214: }
215: return numChars;
216: } // read(char[],int,int)
217:
218: /**
219: * Skip characters. This method will block until some characters are
220: * available, an I/O error occurs, or the end of the stream is reached.
221: *
222: * @param n The number of characters to skip
223: *
224: * @return The number of characters actually skipped
225: *
226: * @exception IOException If an I/O error occurs
227: */
228: public long skip(long n) throws IOException {
229: // charWidth will represent the number of bits to move
230: // n leftward to get num of bytes to skip, and then move the result rightward
231: // to get num of chars effectively skipped.
232: // The trick with &'ing, as with elsewhere in this dcode, is
233: // intended to avoid an expensive use of / that might not be optimized
234: // away.
235: int charWidth = (fEncoding >= 4) ? 2 : 1;
236: long bytesSkipped = fInputStream.skip(n << charWidth);
237: if ((bytesSkipped & (charWidth | 1)) == 0)
238: return bytesSkipped >> charWidth;
239: return (bytesSkipped >> charWidth) + 1;
240: } // skip(long):long
241:
242: /**
243: * Tell whether this stream is ready to be read.
244: *
245: * @return True if the next read() is guaranteed not to block for input,
246: * false otherwise. Note that returning false does not guarantee that the
247: * next read will block.
248: *
249: * @exception IOException If an I/O error occurs
250: */
251: public boolean ready() throws IOException {
252: return false;
253: } // ready()
254:
255: /**
256: * Tell whether this stream supports the mark() operation.
257: */
258: public boolean markSupported() {
259: return fInputStream.markSupported();
260: } // markSupported()
261:
262: /**
263: * Mark the present position in the stream. Subsequent calls to reset()
264: * will attempt to reposition the stream to this point. Not all
265: * character-input streams support the mark() operation.
266: *
267: * @param readAheadLimit Limit on the number of characters that may be
268: * read while still preserving the mark. After
269: * reading this many characters, attempting to
270: * reset the stream may fail.
271: *
272: * @exception IOException If the stream does not support mark(),
273: * or if some other I/O error occurs
274: */
275: public void mark(int readAheadLimit) throws IOException {
276: fInputStream.mark(readAheadLimit);
277: } // mark(int)
278:
279: /**
280: * Reset the stream. If the stream has been marked, then attempt to
281: * reposition it at the mark. If the stream has not been marked, then
282: * attempt to reset it in some way appropriate to the particular stream,
283: * for example by repositioning it to its starting point. Not all
284: * character-input streams support the reset() operation, and some support
285: * reset() without supporting mark().
286: *
287: * @exception IOException If the stream has not been marked,
288: * or if the mark has been invalidated,
289: * or if the stream does not support reset(),
290: * or if some other I/O error occurs
291: */
292: public void reset() throws IOException {
293: fInputStream.reset();
294: } // reset()
295:
296: /**
297: * Close the stream. Once a stream has been closed, further read(),
298: * ready(), mark(), or reset() invocations will throw an IOException.
299: * Closing a previously-closed stream, however, has no effect.
300: *
301: * @exception IOException If an I/O error occurs
302: */
303: public void close() throws IOException {
304: fInputStream.close();
305: } // close()
306:
307: } // class UCSReader
|