001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.ukit.jaxp;
028:
029: import java.io.Reader;
030: import java.io.InputStream;
031: import java.io.IOException;
032:
033: /**
034: * UTF-16 encoded stream reader.
035: */
036: public class ReaderUTF16 extends Reader {
037: private InputStream is;
038: private char bo;
039:
040: /**
041: * Constructor.
042: *
043: * Byte order argument can be: 'l' for little-endian or 'b' for big-endian.
044: *
045: * @param is A byte input stream.
046: * @param bo A byte order in the input stream.
047: */
048: public ReaderUTF16(InputStream is, char bo) {
049: switch (bo) {
050: case 'l':
051: break;
052:
053: case 'b':
054: break;
055:
056: default:
057: throw new IllegalArgumentException("");
058: }
059: this .bo = bo;
060: this .is = is;
061: }
062:
063: /**
064: * Reads characters into a portion of an array.
065: *
066: * @param cbuf Destination buffer.
067: * @param off Offset at which to start storing characters.
068: * @param len Maximum number of characters to read.
069: * @exception IOException If any IO errors occur.
070: */
071: public int read(char[] cbuf, int off, int len) throws IOException {
072: int num = 0;
073: int val;
074: if (bo == 'b') {
075: while (num < len) {
076: if ((val = is.read()) < 0)
077: return (num != 0) ? num : -1;
078: cbuf[off++] = (char) ((val << 8) | (is.read() & 0xff));
079: num++;
080: }
081: } else {
082: while (num < len) {
083: if ((val = is.read()) < 0)
084: return (num != 0) ? num : -1;
085: cbuf[off++] = (char) ((is.read() << 8) | (val & 0xff));
086: num++;
087: }
088: }
089: return num;
090: }
091:
092: /**
093: * Reads a single character.
094: *
095: * @return The character read, as an integer in the range 0 to 65535
096: * (0x0000-0xffff), or -1 if the end of the stream has been reached.
097: * @exception IOException If any IO errors occur.
098: */
099: public int read() throws IOException {
100: int val;
101: if ((val = is.read()) < 0)
102: return -1;
103: if (bo == 'b') {
104: val = (char) ((val << 8) | (is.read() & 0xff));
105: } else {
106: val = (char) ((is.read() << 8) | (val & 0xff));
107: }
108: return val;
109: }
110:
111: /**
112: * Closes the stream.
113: *
114: * @exception IOException If any IO errors occur.
115: */
116: public void close() throws IOException {
117: is.close();
118: }
119: }
|