Mercurial > hg > blitz_condensed
comparison src/com/go/trove/io/DefaultByteBuffer.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.OutputStream; | |
56 import java.io.IOException; | |
57 import java.io.Serializable; | |
58 import java.util.List; | |
59 import java.util.ArrayList; | |
60 | |
61 /****************************************************************************** | |
62 * A ByteBuffer implementation that keeps byte data in memory. | |
63 * | |
64 * @author Brian S O'Neill | |
65 * @version | |
66 * <!--$$Revision: 1.1 $-->, <!--$$JustDate:--> 01/05/30 <!-- $--> | |
67 */ | |
68 public class DefaultByteBuffer implements ByteBuffer, Serializable { | |
69 private static final int BUFFER_SIZE = 512; | |
70 | |
71 // A List of ByteData instances. | |
72 private List mChunks; | |
73 | |
74 private byte[] mBuffer; | |
75 private int mCursor; | |
76 | |
77 private int mBaseCount; | |
78 | |
79 private List mCaptureBuffers; | |
80 | |
81 public DefaultByteBuffer() { | |
82 mChunks = new ArrayList(100); | |
83 } | |
84 | |
85 public long getBaseByteCount() { | |
86 if (mBuffer != null) { | |
87 return mBaseCount + mCursor; | |
88 } | |
89 else { | |
90 return mBaseCount; | |
91 } | |
92 } | |
93 | |
94 public long getByteCount() throws IOException { | |
95 long count; | |
96 if (mBuffer != null) { | |
97 count = mCursor; | |
98 } | |
99 else { | |
100 count = 0; | |
101 } | |
102 | |
103 int size = mChunks.size(); | |
104 for (int i=0; i<size; i++) { | |
105 count += ((ByteData)mChunks.get(i)).getByteCount(); | |
106 } | |
107 | |
108 return count; | |
109 } | |
110 | |
111 public void writeTo(OutputStream out) throws IOException { | |
112 int size = mChunks.size(); | |
113 for (int i=0; i<size; i++) { | |
114 ((ByteData)mChunks.get(i)).writeTo(out); | |
115 } | |
116 | |
117 if (mBuffer != null && mCursor != 0) { | |
118 out.write(mBuffer, 0, mCursor); | |
119 } | |
120 } | |
121 | |
122 public void append(byte b) throws IOException { | |
123 List captureBuffers; | |
124 if ((captureBuffers = mCaptureBuffers) != null) { | |
125 int size = captureBuffers.size(); | |
126 for (int i=0; i<size; i++) { | |
127 ((ByteBuffer)captureBuffers.get(i)).append(b); | |
128 } | |
129 } | |
130 | |
131 if (mBuffer == null) { | |
132 mBuffer = new byte[BUFFER_SIZE]; | |
133 mCursor = 0; | |
134 } | |
135 else if (mCursor >= mBuffer.length) { | |
136 mChunks.add(new ArrayByteData(mBuffer)); | |
137 mBaseCount += BUFFER_SIZE; | |
138 mBuffer = new byte[BUFFER_SIZE]; | |
139 mCursor = 0; | |
140 } | |
141 | |
142 mBuffer[mCursor++] = b; | |
143 } | |
144 | |
145 public void append(byte[] bytes) throws IOException { | |
146 append(bytes, 0, bytes.length); | |
147 } | |
148 | |
149 public void append(byte[] bytes, int offset, int length) | |
150 throws IOException | |
151 { | |
152 List captureBuffers; | |
153 if ((captureBuffers = mCaptureBuffers) != null) { | |
154 int size = captureBuffers.size(); | |
155 for (int i=0; i<size; i++) { | |
156 ((ByteBuffer)captureBuffers.get(i)).append | |
157 (bytes, offset, length); | |
158 } | |
159 } | |
160 | |
161 while (length > 0) { | |
162 if (mBuffer == null) { | |
163 if (length >= BUFFER_SIZE) { | |
164 byte[] copy = new byte[length]; | |
165 System.arraycopy(bytes, offset, copy, 0, length); | |
166 mChunks.add(new ArrayByteData(copy)); | |
167 mBaseCount += length; | |
168 return; | |
169 } | |
170 | |
171 mBuffer = new byte[BUFFER_SIZE]; | |
172 mCursor = 0; | |
173 } | |
174 | |
175 int available = BUFFER_SIZE - mCursor; | |
176 | |
177 if (length <= available) { | |
178 System.arraycopy(bytes, offset, mBuffer, mCursor, length); | |
179 mCursor += length; | |
180 return; | |
181 } | |
182 | |
183 System.arraycopy(bytes, offset, mBuffer, mCursor, available); | |
184 mChunks.add(new ArrayByteData(mBuffer)); | |
185 mBaseCount += BUFFER_SIZE; | |
186 mBuffer = null; | |
187 offset += available; | |
188 length -= available; | |
189 } | |
190 } | |
191 | |
192 public void appendSurrogate(ByteData s) throws IOException { | |
193 if (s == null) { | |
194 return; | |
195 } | |
196 | |
197 List captureBuffers; | |
198 if ((captureBuffers = mCaptureBuffers) != null) { | |
199 int size = captureBuffers.size(); | |
200 for (int i=0; i<size; i++) { | |
201 ((ByteBuffer)captureBuffers.get(i)).appendSurrogate(s); | |
202 } | |
203 } | |
204 | |
205 if (mBuffer != null && mCursor > 0) { | |
206 mChunks.add(new ArrayByteData(mBuffer, 0, mCursor)); | |
207 mBaseCount += mCursor; | |
208 mBuffer = null; | |
209 } | |
210 mChunks.add(s); | |
211 } | |
212 | |
213 public void addCaptureBuffer(ByteBuffer buffer) { | |
214 List captureBuffers; | |
215 if ((captureBuffers = mCaptureBuffers) == null) { | |
216 captureBuffers = mCaptureBuffers = new ArrayList(); | |
217 } | |
218 captureBuffers.add(buffer); | |
219 } | |
220 | |
221 public void removeCaptureBuffer(ByteBuffer buffer) { | |
222 List captureBuffers; | |
223 if ((captureBuffers = mCaptureBuffers) != null) { | |
224 captureBuffers.remove(buffer); | |
225 } | |
226 } | |
227 | |
228 public void reset() throws IOException { | |
229 int size = mChunks.size(); | |
230 for (int i=0; i<size; i++) { | |
231 ((ByteData)mChunks.get(i)).reset(); | |
232 } | |
233 | |
234 List captureBuffers; | |
235 if ((captureBuffers = mCaptureBuffers) != null) { | |
236 size = captureBuffers.size(); | |
237 for (int i=0; i<size; i++) { | |
238 ((ByteData)captureBuffers.get(i)).reset(); | |
239 } | |
240 } | |
241 } | |
242 } |