1 21 22 package org.apache.derby.impl.store.raw.data; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.util.Vector ; 30 31 34 public class MemByteHolder 35 implements ByteHolder 36 { 37 int bufSize; 38 39 boolean writing = true; 40 41 Vector bufV; 42 int curBufVEleAt; 43 44 byte[] curBuf; 45 int curBufPos; 46 47 int curBufDataBytes; 52 53 int lastBufVEleAt = 0; 59 int lastBufDataBytes = 0; 60 61 65 public MemByteHolder(int bufSize) 66 { 67 this.bufSize = bufSize; 68 69 this.curBuf = new byte[bufSize]; 70 this.curBufPos = 0; 71 72 this.bufV = new Vector (128); 73 bufV.addElement(curBuf); 74 this.curBufVEleAt = 0; 75 } 76 77 81 public void write(int b) throws IOException 82 { 83 if (SanityManager.DEBUG) 84 SanityManager.ASSERT(writing == true, 85 "Writing should be true 1"); 86 87 if(curBufPos>=curBuf.length) 88 getNextBuffer_w(); 89 90 curBuf[curBufPos++] = (byte)b; 91 } 92 93 97 public void write(byte[] data, int offset, int len) throws IOException 98 { 99 if (SanityManager.DEBUG) 100 SanityManager.ASSERT(writing == true, 101 "Writing should be true 2"); 102 103 while(len > 0) 104 { 105 if(curBufPos>=curBuf.length) 106 getNextBuffer_w(); 107 108 int bytesToCopyThisTime = len; 109 int bytesInCurBuf = curBuf.length - curBufPos; 110 111 if (bytesToCopyThisTime > bytesInCurBuf) 112 bytesToCopyThisTime = bytesInCurBuf; 113 System.arraycopy(data,offset,curBuf,curBufPos,bytesToCopyThisTime); 114 offset += bytesToCopyThisTime; 115 curBufPos += bytesToCopyThisTime; 116 len -= bytesToCopyThisTime; 117 } 118 } 119 120 124 public long write(InputStream is, long count) throws IOException 125 { 126 long bytesToTransfer = count; 127 int bytesTransferredThisTime = 0; 128 129 do 130 { 131 if(curBufPos>=curBuf.length) 132 getNextBuffer_w(); 133 134 int bytesToTransferThisTime; 135 int bytesInCurBuf = curBuf.length - curBufPos; 136 137 if (bytesToTransfer >= bytesInCurBuf) 138 bytesToTransferThisTime = bytesInCurBuf; 139 else 140 bytesToTransferThisTime = (int)bytesToTransfer; 141 bytesTransferredThisTime = 147 is.read(curBuf,curBufPos,bytesToTransferThisTime); 148 149 if (bytesTransferredThisTime > 0) 150 { 151 if (SanityManager.DEBUG) 152 SanityManager.ASSERT( 153 writing == true, "Writing should be true 3"); 154 155 bytesToTransfer -= bytesTransferredThisTime; 156 curBufPos += bytesTransferredThisTime; 157 } 158 } while (bytesToTransfer > 0 && 159 bytesTransferredThisTime > 0); 160 161 return count - bytesToTransfer; 162 } 163 164 169 public void clear() throws IOException 170 { 171 writing = true; 172 173 curBuf = (byte[])bufV.elementAt(0); 174 this.curBufVEleAt = 0; 175 this.curBufPos = 0; 176 177 lastBufVEleAt = 0; 178 lastBufDataBytes = 0; 179 } 180 181 184 public void startReading() 185 throws IOException 186 { 187 if (writing == true) 188 { 189 writing = false; 191 lastBufDataBytes = curBufPos; 192 lastBufVEleAt = curBufVEleAt; 193 } 194 curBuf = (byte[])bufV.elementAt(0); 198 this.curBufVEleAt = 0; 199 this.curBufPos = 0; 200 if (curBufVEleAt == lastBufVEleAt) 201 curBufDataBytes = lastBufDataBytes; 202 else 203 curBufDataBytes = bufSize; 204 } 205 206 210 public int read() throws IOException 211 { 212 if (SanityManager.DEBUG) 213 SanityManager.ASSERT(writing == false, 214 "Reading should be true 2"); 215 216 if (curBufPos >= curBufDataBytes) 217 getNextBuffer_r(); 218 219 if (curBufPos >= curBufDataBytes) 220 return -1; 221 else 222 return 0xff & curBuf[curBufPos++]; 223 } 224 225 229 public int read(byte b[], 230 int off, 231 int len) 232 throws IOException 233 { 234 return (read(b, off, (OutputStream ) null, len)); 235 } 236 237 241 public int read(OutputStream out, 242 int len) 243 throws IOException 244 { 245 return(read((byte []) null, 0, out, len)); 246 } 247 248 252 public int read(byte b[], 253 int off, 254 OutputStream out, 255 int len) 256 throws IOException 257 { 258 int bytesIRead = 0; 259 boolean eof = false; 260 261 if (SanityManager.DEBUG) 262 SanityManager.ASSERT(writing == false, 263 "Reading should be true 3"); 264 265 if (curBufPos >= curBufDataBytes) 266 eof = getNextBuffer_r(); 267 268 if (eof) return -1; 269 270 while (len > 0 && !eof) 271 { 272 int bytesInCurBuf = curBufDataBytes - curBufPos; 273 int bytesIReadThisTime; 274 if (len >= bytesInCurBuf) 275 bytesIReadThisTime = bytesInCurBuf; 276 else 277 bytesIReadThisTime = len; 278 279 if (out == null) { 280 System.arraycopy(curBuf,curBufPos,b,off,bytesIReadThisTime); 282 } else { 283 out.write(curBuf, curBufPos, bytesIReadThisTime); 285 } 286 off+=bytesIReadThisTime; 287 curBufPos+=bytesIReadThisTime; 288 len -= bytesIReadThisTime; 289 bytesIRead+=bytesIReadThisTime; 290 if (curBufPos >= curBufDataBytes) 291 eof = getNextBuffer_r(); 292 } 293 294 return bytesIRead; 295 } 296 297 301 public int shiftToFront() throws IOException 302 { 303 int remainingBytes = available(); 304 remainingBytes = remainingBytes > 0 ? remainingBytes : (-1) * remainingBytes; 305 306 byte b[] = new byte[remainingBytes + 1]; 307 int bytesRead = read(b, 0, remainingBytes); 308 309 clear(); 311 312 writing = true; 314 write(b, 0, bytesRead); 315 316 curBufDataBytes = 0; 317 318 return bytesRead; 319 } 320 321 324 public int available() 325 { 326 330 int curBufAvailable = curBufDataBytes - curBufPos; 331 int lastBufAvailable = 0; 332 int middleBuffers = 0; 333 if (curBufVEleAt != lastBufVEleAt) 334 { 335 middleBuffers = lastBufVEleAt - curBufVEleAt - 1; 336 lastBufAvailable = lastBufDataBytes; 337 } 338 int availableBytes = 339 curBufAvailable + 340 lastBufAvailable + 341 middleBuffers * bufSize; 342 343 return availableBytes; 344 } 345 346 353 public int numBytesSaved() 354 { 355 int ret_val; 356 357 if (writing) 358 { 359 if (SanityManager.DEBUG) 361 SanityManager.ASSERT( 362 lastBufVEleAt == 0 && lastBufDataBytes == 0, 363 "counters were somehow bumped during writing"); 364 365 ret_val = (curBufVEleAt * bufSize) + curBufPos; 366 } 367 else 368 { 369 ret_val = (lastBufVEleAt * bufSize) + lastBufDataBytes; 370 } 371 372 return(ret_val); 373 } 374 375 379 public long skip(long count) throws IOException 380 { 381 long bytesISkipped = 0; 382 boolean eof = false; 383 384 if (SanityManager.DEBUG) 385 SanityManager.ASSERT(writing == false, 386 "Reading should be true 4"); 387 388 if (curBufPos >= curBufDataBytes) 389 eof = getNextBuffer_r(); 390 391 while (count > 0 && !eof) 392 { 393 int bytesInCurBuf = curBufDataBytes - curBufPos; 394 int bytesISkippedThisTime; 395 396 if (count >= bytesInCurBuf) 397 bytesISkippedThisTime = bytesInCurBuf; 398 else 399 bytesISkippedThisTime = (int)count; 400 401 curBufPos+=bytesISkippedThisTime; 402 count -= bytesISkippedThisTime; 403 bytesISkipped+=bytesISkippedThisTime; 404 405 if (count > 0) 406 eof = getNextBuffer_r(); 407 } 408 409 return bytesISkipped; 410 } 411 412 415 public boolean writingMode() 416 { 417 return writing; 418 } 419 420 424 protected void getNextBuffer_w() throws IOException 425 { 426 if (SanityManager.DEBUG) 427 { 428 getNextBuffer_w_Sanity(); 429 } 430 431 curBufVEleAt++; 432 433 if (bufV.size() <= curBufVEleAt) 434 { 435 curBuf = new byte[bufSize]; 436 bufV.addElement(curBuf); 437 } 438 else 439 { 440 curBuf = (byte[])bufV.elementAt(curBufVEleAt); 441 } 442 443 initBuffer_w(); 444 } 445 446 447 protected void getNextBuffer_w_Sanity() 448 { 449 if (SanityManager.DEBUG) 450 { 451 SanityManager.ASSERT(curBufPos == curBuf.length, 452 "partial write"); 453 454 SanityManager.ASSERT(writing == true, 455 "Writing should be true 5"); 456 } 457 } 458 459 460 protected void initBuffer_w() 461 { 462 curBufPos = 0; 463 464 if (SanityManager.DEBUG) 465 { 466 SanityManager.ASSERT(curBuf.length == bufSize, 467 "bad Buf Length "+curBuf.length); 468 } 469 } 470 471 479 protected boolean getNextBuffer_r() throws IOException 480 { 481 if (SanityManager.DEBUG) 482 SanityManager.ASSERT(writing == false, 483 "Reading should be true 5"); 484 if (curBufVEleAt >= lastBufVEleAt) return true; 485 curBuf = (byte[])bufV.elementAt(++curBufVEleAt); 486 curBufPos = 0; 487 if (curBufVEleAt == lastBufVEleAt) 488 curBufDataBytes = lastBufDataBytes; 489 else 490 curBufDataBytes = bufSize; 491 return false; 492 } 493 494 498 private String dumpBuf(int bufVEleAt) 499 { 500 StringBuffer sb = new StringBuffer (100); 501 502 byte[] buf = (byte[])bufV.elementAt(bufVEleAt); 503 sb.append("("); 504 for (int ix = 0;ix<buf.length;ix++) 505 sb.append(buf[ix]+"."); 506 sb.append(")"); 507 return sb.toString(); 508 } 509 510 514 public String toString() 515 { 516 return 517 " writing: "+writing+ 518 " curBufVEleAt: "+curBufVEleAt+ 519 " curBufPos: "+curBufPos+ 520 " curBufDataBytes: "+curBufDataBytes+ 521 " lastBufVEleAt: "+lastBufVEleAt+ 522 " lastBufDataBytes: "+lastBufDataBytes+ 523 " curBuf: "+dumpBuf(curBufVEleAt); 524 } 525 } 526 | Popular Tags |