001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.vfs.i18n;
030:
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.io.Reader;
034:
035: /**
036: * Implements an encoding reader for UTF-16.
037: */
038: public class UTF16Reader extends EncodingReader {
039: private InputStream is;
040:
041: /**
042: * Null-arg constructor for instantiation by com.caucho.vfs.Encoding only.
043: */
044: public UTF16Reader() {
045: }
046:
047: /**
048: * Create a UTF-16 reader based on the readStream.
049: */
050: private UTF16Reader(InputStream is) {
051: this .is = is;
052: }
053:
054: /**
055: * Create a UTF-16 reader based on the readStream.
056: *
057: * @param is the read stream providing the bytes.
058: * @param javaEncoding the JDK name for the encoding.
059: *
060: * @return the UTF-16 reader.
061: */
062: public Reader create(InputStream is, String javaEncoding) {
063: return new UTF16Reader(is);
064: }
065:
066: /**
067: * Reads into a character buffer using the correct encoding.
068: *
069: * @return the next character or -1 on end of file.
070: */
071: public int read() throws IOException {
072: int ch1 = is.read();
073: int ch2 = is.read();
074:
075: if (ch2 < 0)
076: return -1;
077:
078: return (ch1 << 8) + ch2;
079: }
080:
081: /**
082: * Reads into a character buffer using the correct encoding.
083: *
084: * @param cbuf character buffer receiving the data.
085: * @param off starting offset into the buffer.
086: * @param len number of characters to read.
087: *
088: * @return the number of characters read or -1 on end of file.
089: */
090: public int read(char[] cbuf, int off, int len) throws IOException {
091: int i = 0;
092:
093: for (i = 0; i < len; i++) {
094: int ch1 = is.read();
095: int ch2 = is.read();
096:
097: if (ch2 < 0)
098: return i == 0 ? -1 : i;
099:
100: cbuf[off + i] = (char) ((ch1 << 8) + ch2);
101: }
102:
103: return i;
104: }
105: }
|