1 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 68 public class DefaultByteBuffer implements ByteBuffer, Serializable { 69 private static final int BUFFER_SIZE = 512; 70 71 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 } 243 | Popular Tags |