KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > i18n > UTF8Writer


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.i18n;
30
31 import com.caucho.vfs.OutputStreamWithBuffer;
32
33 import java.io.IOException JavaDoc;
34
35 /**
36  * Implements an encoding char-to-byte writer for UTF8 and the associated
37  * factory.
38  */

39 public class UTF8Writer extends EncodingWriter {
40   private final static UTF8Writer _writer = new UTF8Writer();
41
42   /**
43    * Null-arg constructor for instantiation by com.caucho.vfs.Encoding only.
44    */

45   public UTF8Writer()
46   {
47   }
48   
49   /**
50    * Returns the Java encoding for the writer.
51    */

52   public String JavaDoc getJavaEncoding()
53   {
54     return "UTF8";
55   }
56
57   /**
58    * Returns the UTF8_Writer
59    *
60    * @return the UTF8_Writer
61    */

62   public EncodingWriter create(String JavaDoc javaEncoding)
63   {
64     return _writer;
65   }
66
67   /**
68    * Returns the UTF8_Writer
69    *
70    * @return the UTF8_Writer
71    */

72   public EncodingWriter create()
73   {
74     return _writer;
75   }
76
77   /**
78    * Writes a character to the output stream with the correct encoding.
79    *
80    * @param ch the character to write.
81    */

82   public void write(OutputStreamWithBuffer os, char ch)
83     throws IOException JavaDoc
84   {
85     byte []buffer = os.getBuffer();
86     int length = os.getBufferOffset();
87     
88     if (buffer.length <= length + 2) {
89       buffer = os.nextBuffer(length);
90       length = os.getBufferOffset();
91     }
92     
93     if (ch < 0x80) {
94       buffer[length++] = (byte) ch;
95     }
96     else if (ch < 0x800) {
97       buffer[length++] = (byte) (0xc0 + (ch >> 6));
98       buffer[length++] = (byte) (0x80 + (ch & 0x3f));
99     }
100     else {
101       buffer[length++] = (byte) (0xe0 + (ch >> 12));
102       buffer[length++] = (byte) (0x80 + ((ch >> 6) & 0x3f));
103       buffer[length++] = (byte) (0x80 + (ch & 0x3f));
104     }
105
106     os.setBufferOffset(length);
107   }
108
109   /**
110    * Writes into a character buffer using the correct encoding.
111    *
112    * @param cbuf character array with the data to write.
113    * @param off starting offset into the character array.
114    * @param len the number of characters to write.
115    */

116   public void write(OutputStreamWithBuffer os, char []cbuf, int off, int len)
117     throws IOException JavaDoc
118   {
119     byte []buffer = os.getBuffer();
120     int length = os.getBufferOffset();
121     int capacity = buffer.length;
122     
123     for (int i = 0; i < len; i++) {
124       if (capacity <= length + 2) {
125     buffer = os.nextBuffer(length);
126     length = os.getBufferOffset();
127       }
128       
129       char ch = cbuf[off + i];
130
131       if (ch < 0x80)
132     buffer[length++] = (byte) ch;
133       else if (ch < 0x800) {
134     buffer[length++] = (byte) (0xc0 + (ch >> 6));
135     buffer[length++] = (byte) (0x80 + (ch & 0x3f));
136       }
137       else if (ch < 0xd800 || 0xdfff < ch) {
138     buffer[length++] = (byte) (0xe0 + (ch >> 12));
139     buffer[length++] = (byte) (0x80 + ((ch >> 6) & 0x3f));
140     buffer[length++] = (byte) (0x80 + (ch & 0x3f));
141       }
142       else {
143     char ch2 = cbuf[off + i + 1];
144     int v = 0x10000 + (ch & 0x3ff) * 0x400 + (ch2 & 0x3ff);
145     
146     i += 1;
147     
148     buffer[length++] = (byte) (0xf0 + (v >> 18));
149     buffer[length++] = (byte) (0x80 + ((v >> 12) & 0x3f));
150     buffer[length++] = (byte) (0x80 + ((v >> 6) & 0x3f));
151     buffer[length++] = (byte) (0x80 + (v & 0x3f));
152       }
153     }
154
155     os.setBufferOffset(length);
156   }
157 }
158
159
160
Popular Tags