comparison src/com/go/trove/io/FileByteBuffer.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.RandomAccessFile;
57 import java.io.IOException;
58 import java.util.List;
59 import java.util.ArrayList;
60
61 /******************************************************************************
62 * A ByteBuffer implementation that can read from an open file or can write
63 * to it. This implementation is best suited for temporary byte data that is
64 * too large to hold in memory.
65 *
66 * @author Brian S O'Neill
67 * @version
68 * <!--$$Revision: 1.1 $--> 5 <!-- $$JustDate:--> 00/12/05 <!-- $-->
69 */
70 public class FileByteBuffer implements ByteBuffer {
71 private RandomAccessFile mFile;
72 private List mSurrogates;
73 private List mCaptureBuffers;
74
75 /**
76 * Creates a FileByteBuffer on a RandomAccessFile. If the file is opened
77 * read-only, then the append operations will fail.
78 *
79 * @param file The file to use as a buffer.
80 */
81 public FileByteBuffer(RandomAccessFile file) throws IOException {
82 mFile = file;
83 file.seek(0);
84 }
85
86 public long getBaseByteCount() throws IOException {
87 return mFile.length();
88 }
89
90 public long getByteCount() throws IOException {
91 long count = getBaseByteCount();
92 if (mSurrogates == null) {
93 return count;
94 }
95
96 int size = mSurrogates.size();
97 for (int i=0; i<size; i++) {
98 count += ((Surrogate)mSurrogates.get(i)).mByteData.getByteCount();
99 }
100
101 return count;
102 }
103
104 public void writeTo(OutputStream out) throws IOException {
105 long length = mFile.length();
106 int bufSize;
107 if (length > 4000) {
108 bufSize = 4000;
109 }
110 else {
111 bufSize = (int)length;
112 }
113
114 byte[] inputBuffer = new byte[bufSize];
115
116 mFile.seek(0);
117
118 if (mSurrogates != null) {
119 long currentPos = 0;
120
121 int size = mSurrogates.size();
122 for (int i=0; i<size; i++) {
123 Surrogate s = (Surrogate)mSurrogates.get(i);
124 currentPos = writeTo(inputBuffer, out, currentPos, s.mPos);
125 s.mByteData.writeTo(out);
126 }
127 }
128
129 // Write out the rest of the file.
130 int readAmount;
131 while ((readAmount = mFile.read(inputBuffer, 0, bufSize)) > 0) {
132 out.write(inputBuffer, 0, readAmount);
133 }
134 }
135
136 private long writeTo(byte[] inputBuffer, OutputStream out,
137 long fromPos, long toPos) throws IOException {
138 if (toPos == fromPos) {
139 return fromPos;
140 }
141
142 int bufSize = inputBuffer.length;
143 int readAmount;
144
145 while (toPos > fromPos) {
146 int amount;
147 if (bufSize <= (toPos - fromPos)) {
148 amount = bufSize;
149 }
150 else {
151 amount = (int)(toPos - fromPos);
152 }
153
154 while ((readAmount = mFile.read(inputBuffer, 0, amount)) > 0) {
155 out.write(inputBuffer, 0, readAmount);
156 fromPos += readAmount;
157 amount -= readAmount;
158 if (amount <= 0) {
159 break;
160 }
161 }
162
163 if (readAmount <= 0) {
164 break;
165 }
166 }
167
168 return fromPos;
169 }
170
171 public void append(byte b) throws IOException {
172 List captureBuffers;
173 if ((captureBuffers = mCaptureBuffers) != null) {
174 int size = captureBuffers.size();
175 for (int i=0; i<size; i++) {
176 ((ByteBuffer)captureBuffers.get(i)).append(b);
177 }
178 }
179
180 mFile.write(b);
181 }
182
183 public void append(byte[] bytes) throws IOException {
184 mFile.write(bytes);
185 }
186
187 public void append(byte[] bytes, int offset, int length)
188 throws IOException
189 {
190 List captureBuffers;
191 if ((captureBuffers = mCaptureBuffers) != null) {
192 int size = captureBuffers.size();
193 for (int i=0; i<size; i++) {
194 ((ByteBuffer)captureBuffers.get(i)).append
195 (bytes, offset, length);
196 }
197 }
198
199 mFile.write(bytes, offset, length);
200 }
201
202 public void appendSurrogate(ByteData s) throws IOException {
203 if (s == null) {
204 return;
205 }
206
207 List captureBuffers;
208 if ((captureBuffers = mCaptureBuffers) != null) {
209 int size = captureBuffers.size();
210 for (int i=0; i<size; i++) {
211 ((ByteBuffer)captureBuffers.get(i)).appendSurrogate(s);
212 }
213 }
214
215 if (mSurrogates == null) {
216 mSurrogates = new ArrayList();
217 }
218
219 mSurrogates.add(new Surrogate(s));
220 }
221
222 public void addCaptureBuffer(ByteBuffer buffer) {
223 List captureBuffers;
224 if ((captureBuffers = mCaptureBuffers) == null) {
225 captureBuffers = mCaptureBuffers = new ArrayList();
226 }
227 captureBuffers.add(buffer);
228 }
229
230 public void removeCaptureBuffer(ByteBuffer buffer) {
231 List captureBuffers;
232 if ((captureBuffers = mCaptureBuffers) != null) {
233 captureBuffers.remove(buffer);
234 }
235 }
236
237 public void reset() throws IOException {
238 List byteDatas;
239 int i, size;
240
241 if ((byteDatas = mSurrogates) != null) {
242 size = byteDatas.size();
243 for (i=0; i<size; i++) {
244 ((ByteData)byteDatas.get(i)).reset();
245 }
246 }
247
248 if ((byteDatas = mCaptureBuffers) != null) {
249 size = byteDatas.size();
250 for (i=0; i<size; i++) {
251 ((ByteData)byteDatas.get(i)).reset();
252 }
253 }
254 }
255
256 private class Surrogate {
257 public final ByteData mByteData;
258 public final long mPos;
259
260 public Surrogate(ByteData data) throws IOException {
261 mByteData = data;
262 mPos = mFile.getFilePointer();
263 }
264 }
265 }