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: import java.io.UnsupportedEncodingException;
035:
036: /**
037: * Abstract class for a byte-to-character encoding reader and its
038: * associated factory.
039: *
040: * <p/>Implementations need to implement <code>create</code>
041: * and <code>read()</code> at minimum. Efficient implementations will
042: * also implement the <code>read</code> into a buffer.
043: *
044: * <p/>Implementations should not buffer the bytes.
045: */
046: abstract public class EncodingReader extends Reader {
047: private String _javaEncoding;
048:
049: public String getJavaEncoding() {
050: return _javaEncoding;
051: }
052:
053: public void setJavaEncoding(String encoding) {
054: _javaEncoding = encoding;
055: }
056:
057: /**
058: * Returns a new encoding reader for the given stream and javaEncoding.
059: *
060: * @param is the input stream providing the bytes.
061: * @param javaEncoding the JDK name for the encoding.
062: *
063: * @return an encoding reader or null for ISO-8859-1.
064: */
065: public abstract Reader create(InputStream is, String javaEncoding)
066: throws UnsupportedEncodingException;
067:
068: /**
069: * Returns a new encoding reader for the given stream and javaEncoding.
070: *
071: * @param is the input stream providing the bytes.
072: * @param javaEncoding the JDK name for the encoding.
073: *
074: * @return an encoding reader or null for ISO-8859-1.
075: */
076: public Reader create(InputStream is)
077: throws UnsupportedEncodingException {
078: Reader reader = create(is, getJavaEncoding());
079:
080: if (reader != null)
081: return reader;
082: else
083: return new ISO8859_1Reader(is);
084: }
085:
086: /**
087: * Returns the next character using the correct encoding.
088: *
089: * @return the next character or -1 on end of file.
090: */
091: public abstract int read() throws IOException;
092:
093: /**
094: * Reads into a character buffer using the correct encoding.
095: *
096: * @param cbuf character buffer receiving the data.
097: * @param off starting offset into the buffer.
098: * @param len number of characters to read.
099: *
100: * @return the number of characters read or -1 on end of file.
101: */
102: public int read(char[] cbuf, int off, int len) throws IOException {
103: for (int i = 0; i < len; i++) {
104: int ch = read();
105:
106: if (ch < 0)
107: return len;
108:
109: cbuf[off + i] = (char) ch;
110: }
111:
112: return len;
113: }
114:
115: /**
116: * Closes the reader, possibly returning it to a pool.
117: */
118: public void close() {
119: }
120: }
|