KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > AbstractByteToChar


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.vfs;
30
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.Reader JavaDoc;
34 import java.io.UnsupportedEncodingException JavaDoc;
35
36 /**
37  * Utility class for converting a byte stream to a character stream.
38  *
39  * <pre>
40  * ByteToChar converter = new ByteToChar();
41  * converter.setEncoding("utf-8");
42  * converter.clear();
43  *
44  * converter.addChar('H');
45  * converter.addByte(0xc0);
46  * converter.addByte(0xb8);
47  *
48  * String value = converter.getConvertedString();
49  * </pre>
50  */

51 abstract public class AbstractByteToChar extends InputStream JavaDoc {
52   private Reader JavaDoc _readEncoding;
53   private String JavaDoc _readEncodingName;
54   private int _specialEncoding;
55
56   private final byte []_byteBuffer = new byte[256];
57   private final char []_charBuffer = new char[1];
58   private int _byteHead;
59   private int _byteTail;
60
61   /**
62    * Creates an uninitialized converter. Use <code>init</code> to initialize.
63    */

64   AbstractByteToChar()
65   {
66   }
67
68   /**
69    * Sets the encoding for the converter.
70    */

71   public void setEncoding(String JavaDoc encoding)
72     throws UnsupportedEncodingException JavaDoc
73   {
74     if (encoding != null) {
75       _readEncoding = Encoding.getReadEncoding(this, encoding);
76       _readEncodingName = Encoding.getMimeName(encoding);
77     }
78     else {
79       _readEncoding = null;
80       _readEncodingName = null;
81     }
82   }
83
84   /**
85    * Clears the converted
86    */

87   public void clear()
88   {
89     _byteHead = 0;
90     _byteTail = 0;
91   }
92   
93   /**
94    * Adds the next byte.
95    *
96    * @param b the byte to write
97    */

98   public void addByte(int b)
99     throws IOException JavaDoc
100   {
101     int nextHead = (_byteHead + 1) % _byteBuffer.length;
102
103     while (nextHead == _byteTail) {
104       int ch = readChar();
105
106       if (ch < 0)
107         break;
108
109       outputChar(ch);
110     }
111
112     _byteBuffer[_byteHead] = (byte) b;
113     _byteHead = nextHead;
114   }
115   
116   /**
117    * Adds the next character.
118    *
119    * @param nextCh the character to write
120    */

121   public void addChar(char nextCh)
122     throws IOException JavaDoc
123   {
124     int ch;
125     while ((ch = readChar()) >= 0)
126       outputChar((char) ch);
127
128     outputChar(nextCh);
129   }
130
131   /**
132    * Flushes the buffer.
133    */

134   public void flush()
135     throws IOException JavaDoc
136   {
137     int ch;
138     while ((ch = readChar()) >= 0)
139       outputChar((char) ch);
140   }
141
142   /**
143    * Reads the next converted character.
144    */

145   private int readChar()
146     throws IOException JavaDoc
147   {
148     Reader JavaDoc readEncoding = _readEncoding;
149     
150     if (readEncoding == null)
151       return read();
152     else {
153       if (readEncoding.read(_charBuffer, 0, 1) < 0)
154     return -1;
155       else
156     return _charBuffer[0];
157     }
158   }
159
160   /**
161    * For internal use only. Reads the next byte from the byte buffer.
162    *
163    * @return the next byte
164    */

165   public int read()
166     throws IOException JavaDoc
167   {
168     if (_byteHead == _byteTail)
169       return -1;
170
171     int b = _byteBuffer[_byteTail] & 0xff;
172     _byteTail = (_byteTail + 1) % _byteBuffer.length;
173
174     return b;
175   }
176
177   abstract protected void outputChar(int ch)
178     throws IOException JavaDoc;
179 }
180
Popular Tags