Mercurial > hg > blitz_condensed
comparison src/com/go/trove/io/DefaultCharToByteBuffer.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dc0c5604566 |
---|---|
1 /* ==================================================================== | |
2 * Trove - Copyright (c) 1999-2000 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 | |
57 /****************************************************************************** | |
58 * A CharToByteBuffer implementation that wraps a ByteBuffer for storage. | |
59 * | |
60 * @author Brian S O'Neill | |
61 * @version | |
62 * <!--$$Revision: 1.1 $-->, <!--$$JustDate:--> 01/05/30 <!-- $--> | |
63 */ | |
64 public class DefaultCharToByteBuffer | |
65 implements CharToByteBuffer, Serializable | |
66 { | |
67 private ByteBuffer mBuffer; | |
68 private transient OutputStreamWriter mConvertor; | |
69 | |
70 private char[] mChars; | |
71 private int mCapacity; | |
72 private int mCursor; | |
73 | |
74 private String mDefaultEncoding; | |
75 | |
76 /** | |
77 * @param buffer Buffer that receives the characters converted to bytes. | |
78 */ | |
79 public DefaultCharToByteBuffer(ByteBuffer buffer) { | |
80 this(buffer, null); | |
81 } | |
82 | |
83 /** | |
84 * @param buffer Buffer that receives the characters converted to bytes. | |
85 * @param defaultEncoding Default character encoding to use if setEncoding | |
86 * is not called. | |
87 */ | |
88 public DefaultCharToByteBuffer(ByteBuffer buffer, String defaultEncoding) { | |
89 mBuffer = buffer; | |
90 mChars = new char[4000]; | |
91 mCapacity = mChars.length; | |
92 mDefaultEncoding = defaultEncoding; | |
93 } | |
94 | |
95 public void setEncoding(String enc) throws IOException { | |
96 drain(true); | |
97 mConvertor = new OutputStreamWriter | |
98 (new ByteBufferOutputStream(mBuffer), enc); | |
99 } | |
100 | |
101 public String getEncoding() { | |
102 return (mConvertor == null) ? mDefaultEncoding : | |
103 mConvertor.getEncoding(); | |
104 } | |
105 | |
106 public long getBaseByteCount() throws IOException { | |
107 return mBuffer.getBaseByteCount(); | |
108 } | |
109 | |
110 public long getByteCount() throws IOException { | |
111 drain(true); | |
112 return mBuffer.getByteCount(); | |
113 } | |
114 | |
115 public void writeTo(OutputStream out) throws IOException { | |
116 drain(true); | |
117 mBuffer.writeTo(out); | |
118 } | |
119 | |
120 public void append(byte b) throws IOException { | |
121 drain(true); | |
122 mBuffer.append(b); | |
123 } | |
124 | |
125 public void append(byte[] bytes) throws IOException { | |
126 append(bytes, 0, bytes.length); | |
127 } | |
128 | |
129 public void append(byte[] bytes, int offset, int length) | |
130 throws IOException { | |
131 | |
132 if (length != 0) { | |
133 drain(true); | |
134 mBuffer.append(bytes, offset, length); | |
135 } | |
136 } | |
137 | |
138 public void appendSurrogate(ByteData s) throws IOException { | |
139 if (s != null) { | |
140 drain(true); | |
141 mBuffer.appendSurrogate(s); | |
142 } | |
143 } | |
144 | |
145 public void addCaptureBuffer(ByteBuffer buffer) throws IOException { | |
146 drain(true); | |
147 mBuffer.addCaptureBuffer(buffer); | |
148 } | |
149 | |
150 public void removeCaptureBuffer(ByteBuffer buffer) throws IOException { | |
151 drain(true); | |
152 mBuffer.removeCaptureBuffer(buffer); | |
153 } | |
154 | |
155 public void append(char c) throws IOException { | |
156 if (mCursor >= mCapacity) { | |
157 drain(false); | |
158 } | |
159 mChars[mCursor++] = c; | |
160 } | |
161 | |
162 public void append(char[] chars) throws IOException { | |
163 append(chars, 0, chars.length); | |
164 } | |
165 | |
166 public void append(char[] chars, int offset, int length) | |
167 throws IOException | |
168 { | |
169 if (length == 0) { | |
170 return; | |
171 } | |
172 | |
173 int capacity = mCapacity; | |
174 | |
175 if (length < (capacity - mCursor)) { | |
176 System.arraycopy(chars, offset, mChars, mCursor, length); | |
177 mCursor += length; | |
178 return; | |
179 } | |
180 | |
181 // Make room and try again. | |
182 drain(false); | |
183 | |
184 if (length < capacity) { | |
185 System.arraycopy(chars, offset, mChars, mCursor, length); | |
186 mCursor += length; | |
187 return; | |
188 } | |
189 | |
190 // Write the whole chunk out at once. | |
191 getConvertor().write(chars, offset, length); | |
192 } | |
193 | |
194 public void append(String str) throws IOException { | |
195 append(str, 0, str.length()); | |
196 } | |
197 | |
198 public void append(String str, int offset, int length) throws IOException { | |
199 if (length == 0) { | |
200 return; | |
201 } | |
202 | |
203 int capacity = mCapacity; | |
204 int avail = capacity - mCursor; | |
205 | |
206 if (length <= avail) { | |
207 str.getChars(offset, offset + length, mChars, mCursor); | |
208 mCursor += length; | |
209 return; | |
210 } | |
211 | |
212 // Fill up the rest of the character buffer and drain it. | |
213 str.getChars(offset, offset + avail, mChars, mCursor); | |
214 offset += avail; | |
215 length -= avail; | |
216 mCursor = capacity; | |
217 drain(false); | |
218 | |
219 // Drain chunks that completely fill the character buffer. | |
220 while (length >= capacity) { | |
221 str.getChars(offset, offset + capacity, mChars, 0); | |
222 offset += capacity; | |
223 length -= capacity; | |
224 mCursor = capacity; | |
225 drain(false); | |
226 } | |
227 | |
228 // Copy the remainder into the character buffer, but don't drain. | |
229 if (length > 0) { | |
230 str.getChars(offset, offset + length, mChars, 0); | |
231 mCursor = length; | |
232 } | |
233 } | |
234 | |
235 public void reset() throws IOException { | |
236 mBuffer.reset(); | |
237 } | |
238 | |
239 public void drain() throws IOException { | |
240 drain(true); | |
241 } | |
242 | |
243 private OutputStreamWriter getConvertor() | |
244 throws UnsupportedEncodingException | |
245 { | |
246 if (mConvertor == null) { | |
247 if (mDefaultEncoding == null) { | |
248 mConvertor = new OutputStreamWriter | |
249 (new ByteBufferOutputStream(mBuffer)); | |
250 } | |
251 else { | |
252 mConvertor = new OutputStreamWriter | |
253 (new ByteBufferOutputStream(mBuffer), mDefaultEncoding); | |
254 } | |
255 } | |
256 return mConvertor; | |
257 } | |
258 | |
259 private void drain(boolean flush) throws IOException { | |
260 if (mCursor != 0) { | |
261 try { | |
262 getConvertor().write(mChars, 0, mCursor); | |
263 } | |
264 finally { | |
265 mCursor = 0; | |
266 } | |
267 } | |
268 | |
269 if (flush && mConvertor != null) { | |
270 mConvertor.flush(); | |
271 } | |
272 } | |
273 | |
274 private void writeObject(ObjectOutputStream out) throws IOException { | |
275 out.defaultWriteObject(); | |
276 if (mConvertor == null) { | |
277 out.writeObject(null); | |
278 } | |
279 else { | |
280 out.writeObject(mConvertor.getEncoding()); | |
281 } | |
282 } | |
283 | |
284 private void readObject(ObjectInputStream in) | |
285 throws IOException, ClassNotFoundException | |
286 { | |
287 in.defaultReadObject(); | |
288 String enc = (String)in.readObject(); | |
289 if (enc != null) { | |
290 mConvertor = new OutputStreamWriter | |
291 (new ByteBufferOutputStream(mBuffer), enc); | |
292 } | |
293 } | |
294 } |