1 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 70 public class FileByteBuffer implements ByteBuffer { 71 private RandomAccessFile mFile; 72 private List mSurrogates; 73 private List mCaptureBuffers; 74 75 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 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 } 266 | Popular Tags |