KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.caucho.util.ByteBuffer;
32
33 import java.io.IOException 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 public final class OldByteToChar extends ByteToChar {
52   private ByteBuffer bb;
53   private String JavaDoc javaEncoding;
54
55   /**
56    * Creates an uninitialized converter. Use <code>init</code> to initialize.
57    */

58   OldByteToChar()
59   {
60     bb = new ByteBuffer();
61   }
62
63   /**
64    * Sets the encoding for the converter.
65    */

66   public void setEncoding(String JavaDoc encoding)
67     throws UnsupportedEncodingException JavaDoc
68   {
69     javaEncoding = Encoding.getJavaName(encoding);
70   }
71
72   /**
73    * Clears the converted
74    */

75   public void clear()
76   {
77     bb.clear();
78   }
79
80   /**
81    * Gets the converted string.
82    */

83   public String JavaDoc getConvertedString()
84     throws IOException JavaDoc
85   {
86     return bb.toString(javaEncoding);
87   }
88   
89   /**
90    * Adds the next byte.
91    *
92    * @param b the byte to write
93    */

94   public void addByte(int b)
95     throws IOException JavaDoc
96   {
97     bb.add(b);
98   }
99   
100   /**
101    * Adds the next character.
102    *
103    * @param nextCh the character to write
104    */

105   public void addChar(char nextCh)
106     throws IOException JavaDoc
107   {
108     // The oldByteToChar doesn't handle this case
109
bb.add((byte) nextCh);
110   }
111
112   /**
113    * Prints the object.
114    */

115   public String JavaDoc toString()
116   {
117     return "[OldByteToChar " + javaEncoding + "]";
118   }
119 }
120
Popular Tags