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;
030:
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.io.Reader;
034: import java.io.UnsupportedEncodingException;
035:
036: /**
037: * Utility class for converting a byte stream to a character stream.
038: *
039: * <pre>
040: * ByteToChar converter = new ByteToChar();
041: * converter.setEncoding("utf-8");
042: * converter.clear();
043: *
044: * converter.addChar('H');
045: * converter.addByte(0xc0);
046: * converter.addByte(0xb8);
047: *
048: * String value = converter.getConvertedString();
049: * </pre>
050: */
051: abstract public class AbstractByteToChar extends InputStream {
052: private Reader _readEncoding;
053: private String _readEncodingName;
054: private int _specialEncoding;
055:
056: private final byte[] _byteBuffer = new byte[256];
057: private final char[] _charBuffer = new char[1];
058: private int _byteHead;
059: private int _byteTail;
060:
061: /**
062: * Creates an uninitialized converter. Use <code>init</code> to initialize.
063: */
064: AbstractByteToChar() {
065: }
066:
067: /**
068: * Sets the encoding for the converter.
069: */
070: public void setEncoding(String encoding)
071: throws UnsupportedEncodingException {
072: if (encoding != null) {
073: _readEncoding = Encoding.getReadEncoding(this , encoding);
074: _readEncodingName = Encoding.getMimeName(encoding);
075: } else {
076: _readEncoding = null;
077: _readEncodingName = null;
078: }
079: }
080:
081: /**
082: * Clears the converted
083: */
084: public void clear() {
085: _byteHead = 0;
086: _byteTail = 0;
087: }
088:
089: /**
090: * Adds the next byte.
091: *
092: * @param b the byte to write
093: */
094: public void addByte(int b) throws IOException {
095: int nextHead = (_byteHead + 1) % _byteBuffer.length;
096:
097: while (nextHead == _byteTail) {
098: int ch = readChar();
099:
100: if (ch < 0)
101: break;
102:
103: outputChar(ch);
104: }
105:
106: _byteBuffer[_byteHead] = (byte) b;
107: _byteHead = nextHead;
108: }
109:
110: /**
111: * Adds the next character.
112: *
113: * @param nextCh the character to write
114: */
115: public void addChar(char nextCh) throws IOException {
116: int ch;
117: while ((ch = readChar()) >= 0)
118: outputChar((char) ch);
119:
120: outputChar(nextCh);
121: }
122:
123: /**
124: * Flushes the buffer.
125: */
126: public void flush() throws IOException {
127: int ch;
128: while ((ch = readChar()) >= 0)
129: outputChar((char) ch);
130: }
131:
132: /**
133: * Reads the next converted character.
134: */
135: private int readChar() throws IOException {
136: Reader readEncoding = _readEncoding;
137:
138: if (readEncoding == null)
139: return read();
140: else {
141: if (readEncoding.read(_charBuffer, 0, 1) < 0)
142: return -1;
143: else
144: return _charBuffer[0];
145: }
146: }
147:
148: /**
149: * For internal use only. Reads the next byte from the byte buffer.
150: *
151: * @return the next byte
152: */
153: public int read() throws IOException {
154: if (_byteHead == _byteTail)
155: return -1;
156:
157: int b = _byteBuffer[_byteTail] & 0xff;
158: _byteTail = (_byteTail + 1) % _byteBuffer.length;
159:
160: return b;
161: }
162:
163: abstract protected void outputChar(int ch) throws IOException;
164: }
|