KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > trove > io > FastCharToByteBuffer


1 /* ====================================================================
2  * Trove - Copyright (c) 1999-2001 Walt Disney Internet Group
3  * ====================================================================
4  * The Tea Software License, Version 1.1
5  *
6  * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Walt Disney Internet Group (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@dig.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34  * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35  * written permission of the Walt Disney Internet Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.trove.io;
54
55 import java.io.*;
56 import java.util.Set JavaDoc;
57 import java.util.HashSet JavaDoc;
58
59 /******************************************************************************
60  * A CharToByteBuffer implementation that converts ISO-8859-1 encoded
61  * characters faster. To force fast conversion, construct FastCharToByteBuffer
62  * with ISO-8859-1 as the default encoding.
63  *
64  * @author Brian S O'Neill
65  * @version
66  * <!--$$Revision:--> 1 <!-- $-->, <!--$$JustDate:--> 01/06/14 <!-- $-->
67  */

68 public class FastCharToByteBuffer implements CharToByteBuffer, Serializable {
69     private static final int TEMP_BUF_LEN = 512;
70     private static final Set JavaDoc SUPPORTED_ENCODINGS;
71
72     static {
73         SUPPORTED_ENCODINGS = new HashSet JavaDoc();
74         SUPPORTED_ENCODINGS.add("8859-1");
75         SUPPORTED_ENCODINGS.add("8859_1");
76         SUPPORTED_ENCODINGS.add("iso-8859-1");
77         SUPPORTED_ENCODINGS.add("ISO-8859-1");
78         SUPPORTED_ENCODINGS.add("iso8859_1");
79         SUPPORTED_ENCODINGS.add("ISO8859_1");
80     }
81
82     static boolean isSupportedEncoding(String JavaDoc encoding) {
83         return SUPPORTED_ENCODINGS.contains(encoding);
84     }
85
86     private ByteBuffer mBuffer;
87
88     private transient byte[] mTempBytes;
89     private transient char[] mTempChars;
90
91     private CharToByteBuffer mSlowConvertor;
92
93     /**
94      * @param buffer Buffer that receives the characters converted to bytes.
95      */

96     public FastCharToByteBuffer(ByteBuffer buffer) {
97         this(buffer, null);
98     }
99
100     /**
101      * @param buffer Buffer that receives the characters converted to bytes.
102      * @param defaultEncoding Default character encoding to use if setEncoding
103      * is not called.
104      */

105     public FastCharToByteBuffer(ByteBuffer buffer, String JavaDoc defaultEncoding) {
106         mBuffer = buffer;
107         try {
108             setEncoding(defaultEncoding);
109         }
110         catch (IOException e) {
111         }
112     }
113
114     public void setEncoding(String JavaDoc enc) throws IOException {
115         drain();
116         if (isSupportedEncoding(enc)) {
117             mSlowConvertor = null;
118         }
119         else {
120             mSlowConvertor = new DefaultCharToByteBuffer(mBuffer, enc);
121         }
122     }
123
124     public String JavaDoc getEncoding() throws IOException {
125         return mSlowConvertor != null ?
126             mSlowConvertor.getEncoding() : "ISO-8859-1";
127     }
128
129     public long getBaseByteCount() throws IOException {
130         drain();
131         return mBuffer.getBaseByteCount();
132     }
133
134     public long getByteCount() throws IOException {
135         drain();
136         return mBuffer.getByteCount();
137     }
138
139     public void writeTo(OutputStream out) throws IOException {
140         drain();
141         mBuffer.writeTo(out);
142     }
143
144     public void append(byte b) throws IOException {
145         drain();
146         mBuffer.append(b);
147     }
148
149     public void append(byte[] bytes) throws IOException {
150         append(bytes, 0, bytes.length);
151     }
152
153     public void append(byte[] bytes, int offset, int length)
154         throws IOException
155     {
156         if (length != 0) {
157             drain();
158             mBuffer.append(bytes, offset, length);
159         }
160     }
161
162     public void appendSurrogate(ByteData s) throws IOException {
163         if (s != null) {
164             drain();
165             mBuffer.appendSurrogate(s);
166         }
167     }
168
169     public void addCaptureBuffer(ByteBuffer buffer) throws IOException {
170         drain();
171         mBuffer.addCaptureBuffer(buffer);
172     }
173
174     public void removeCaptureBuffer(ByteBuffer buffer) throws IOException {
175         drain();
176         mBuffer.removeCaptureBuffer(buffer);
177     }
178
179     public void append(char c) throws IOException {
180         char[] chars = getTempChars();
181         chars[0] = c;
182         append(chars, 0, 1);
183     }
184
185     public void append(char[] chars) throws IOException {
186         append(chars, 0, chars.length);
187     }
188
189     public void append(char[] chars, int offset, int length)
190         throws IOException
191     {
192         if (mSlowConvertor != null) {
193             mSlowConvertor.append(chars, offset, length);
194             return;
195         }
196
197         appendFast(chars, offset, length);
198     }
199
200     public void append(String JavaDoc str) throws IOException {
201         append(str, 0, str.length());
202     }
203
204     public void append(String JavaDoc str, int offset, int length) throws IOException {
205         if (mSlowConvertor != null) {
206             mSlowConvertor.append(str, offset, length);
207             return;
208         }
209
210         if (length == 0) {
211             return;
212         }
213
214         char[] tempChars = getTempChars();
215         int bufLen = tempChars.length;
216
217         while (length >= bufLen) {
218             str.getChars(offset, offset + bufLen, tempChars, 0);
219             offset += bufLen;
220             length -= bufLen;
221             appendFast(tempChars, 0, bufLen);
222         }
223
224         if (length > 0) {
225             str.getChars(offset, offset + length, tempChars, 0);
226             appendFast(tempChars, 0, length);
227         }
228     }
229
230     public void reset() throws IOException {
231         mBuffer.reset();
232     }
233
234     public void drain() throws IOException {
235         if (mSlowConvertor != null) {
236             mSlowConvertor.drain();
237         }
238     }
239
240     private void appendFast(char[] chars, int offset, int length)
241         throws IOException
242     {
243         byte[] tempBytes = getTempBytes();
244         int bufLen = tempBytes.length;
245         int bi = 0;
246         int climit = offset + length;
247
248         for (int ci = offset; ci < climit; ci++) {
249             tempBytes[bi++] = (byte)chars[ci];
250             if (bi >= bufLen) {
251                 mBuffer.append(tempBytes, 0, bufLen);
252                 bi = 0;
253             }
254         }
255
256         if (bi > 0) {
257             mBuffer.append(tempBytes, 0, bi);
258         }
259     }
260
261     private byte[] getTempBytes() {
262         if (mTempBytes == null) {
263             mTempBytes = new byte[TEMP_BUF_LEN];
264         }
265         return mTempBytes;
266     }
267
268     private char[] getTempChars() {
269         if (mTempChars == null) {
270             mTempChars = new char[TEMP_BUF_LEN];
271         }
272         return mTempChars;
273     }
274 }
275
Popular Tags