1 23 24 28 50 package com.sun.jts.CosTransactions; 51 52 54 import java.io.*; 55 import java.util.*; 56 57 65 72 class LogFileHandle { 73 75 final static int OPEN_RDONLY = 0x00000001; 76 77 79 final static int OPEN_RDWR = 0x00000002; 80 81 83 final static int OPEN_CREAT = 0x00000004; 84 85 87 final static int OPEN_SYNC = 0x00000008; 88 89 91 final static int SEEK_RELATIVE = 0; 92 93 95 final static int SEEK_ABSOLUTE = 1; 96 97 99 final static String MODE_READONLY = "r"; 101 103 static String MODE_READWRITEOLD = "rw"; 105 107 static String MODE_READWRITENEW = "rw"; 109 static String dsyncProp = null; 110 111 final static String DSYNC_PROPERTY = "com.sun.appserv.transaction.nofdsync"; 112 113 115 final static int LOG_FNAME_MAX = 252; 117 118 120 final static int FILESYSTEM_BLOCKSIZE = 4096; 121 122 124 private RandomAccessFile fhandle = null; 125 private FileDescriptor fd = null; 126 private byte[] bufferData = null; 127 boolean buffered = false; 128 int bufferUpdateStart = -1; 129 int bufferUpdateEnd = -1; 130 int buffPos = 0; 131 132 static { 133 dsyncProp = System.getProperty(DSYNC_PROPERTY); 134 if (dsyncProp != null) { 135 MODE_READWRITEOLD = "rwd"; 136 MODE_READWRITENEW = "rwd"; 137 } 138 } 139 140 148 LogFileHandle() { 149 fhandle = null; 150 fd = null; 152 } 153 154 165 LogFileHandle( File file, 166 int openOptions ) 167 throws LogException { 168 169 171 if (dsyncProp == null) { 172 if( (openOptions & OPEN_SYNC) == 0 ) 173 buffered = true; 174 } 175 176 178 if( (openOptions & OPEN_RDONLY) != 0 ) 179 fileOpen(file,MODE_READONLY); 180 else 181 try { 182 fileOpen(file,MODE_READWRITEOLD); 183 } catch( LogException e ) { 184 if( (openOptions & OPEN_CREAT) != 0 ) 185 fileOpen(file,MODE_READWRITENEW); 186 } 187 } 188 189 199 public void finalize() 200 throws LogException { 201 202 206 if( fhandle != null ) 207 fileClose(); 208 209 211 bufferData = null; 212 213 } 214 215 225 int fileRead( byte[] buffer ) 226 throws LogException{ 227 228 int bytesRead = 0; 229 230 if( buffer.length > 0 ) 231 try { 232 233 235 if( buffered ) { 236 237 239 if( buffPos >= bufferData.length ) 240 bytesRead = -1; 241 242 245 else { 246 if( buffPos + buffer.length >= bufferData.length ) 247 bytesRead = bufferData.length - buffPos; 248 else 249 bytesRead = buffer.length; 250 251 System.arraycopy(bufferData,buffPos,buffer,0,bytesRead); 252 buffPos += bytesRead; 253 } 254 } 255 256 258 else { 259 bytesRead = fhandle.read(buffer); 260 if( bytesRead == -1 ) bytesRead = 0; 261 } 262 } catch( Throwable exc ) { 263 throw new LogException(null,LogException.LOG_READ_FAILURE,1); 264 } 265 266 return bytesRead; 267 } 268 269 279 int fileWrite( byte[] buffer ) 280 throws LogException { 281 282 if( buffer.length > 0 ) 283 try { 284 285 287 if( buffered ) { 288 289 292 if( buffPos + buffer.length >= bufferData.length ) { 293 byte[] newBufferData = new byte[buffPos+buffer.length]; 294 if( bufferData.length > 0 ) 295 System.arraycopy(bufferData,0,newBufferData,0,bufferData.length); 296 bufferData = newBufferData; 297 } 298 299 301 System.arraycopy(buffer,0,bufferData,buffPos,buffer.length); 302 303 306 if( bufferUpdateStart == -1 || 307 buffPos < bufferUpdateStart ) 308 bufferUpdateStart = buffPos; 309 310 buffPos += buffer.length; 311 312 if( buffPos > bufferUpdateEnd ) 313 bufferUpdateEnd = buffPos; 314 } 315 316 319 else { 320 fhandle.write(buffer); 321 if (dsyncProp == null) 322 fd.sync(); 323 } 324 } catch( Throwable e ) { 325 int errCode = LogException.LOG_WRITE_FAILURE; 326 throw new LogException(null,errCode,1); 329 } 330 331 return buffer.length; 332 } 333 334 345 void fileOpen( File file, 346 String fileMode ) 347 throws LogException { 348 fhandle = null; 349 try { 350 fhandle = new RandomAccessFile(file,fileMode); 351 fd = fhandle.getFD(); 352 353 356 if( buffered ) 357 if( fhandle.length() > 0 ) { 358 bufferData = new byte[(int)fhandle.length()]; 359 fhandle.readFully(bufferData); 360 } 361 else 362 bufferData = new byte[0]; 363 } catch( Throwable e ) { 364 throw new LogException(null,LogException.LOG_OPEN_FAILURE,1); 365 } 366 367 } 368 369 379 void fileClose() 380 throws LogException { 381 382 try { 383 384 387 if( bufferUpdateStart != -1 ) 388 fileSync(); 389 390 392 fhandle.close(); 393 } catch( Throwable e ) { 394 throw new LogException(null,LogException.LOG_CLOSE_FAILURE,1); 395 } 396 397 399 fhandle = null; 400 fd = null; 402 } 403 404 415 416 void fileSeek( long position, 417 int seekMode ) 418 throws LogException { 419 420 422 long absPos = position; 423 try { 424 425 429 if( buffered ) { 430 if( seekMode == SEEK_RELATIVE ) 431 absPos = buffPos + position; 432 buffPos = (int)absPos; 433 } 434 435 437 else { 438 if( seekMode == SEEK_RELATIVE ) 439 absPos = fhandle.getFilePointer() + position; 440 fhandle.seek(absPos); 441 } 442 } catch( Throwable e ) { 443 throw new LogException(null,LogException.LOG_READ_FAILURE,1); 444 } 445 } 446 447 457 void fileSync() throws LogException { 458 459 462 if( bufferUpdateStart != -1 ) 463 try { 464 fhandle.seek(bufferUpdateStart); 465 fhandle.write(bufferData,bufferUpdateStart,bufferUpdateEnd-bufferUpdateStart); 466 if (dsyncProp == null) 467 fd.sync(); 468 469 bufferUpdateStart = -1; 470 bufferUpdateEnd = -1; 471 } catch (Throwable e) { 472 throw new LogException(null,LogException.LOG_READ_FAILURE,1); 473 } 474 475 } 476 477 487 int readVector( byte[][] vector ) 488 throws LogException { 489 int bytesRead = 0; 490 for( int i = 0; i < vector.length; i++ ) 491 bytesRead += fileRead(vector[i]); 492 493 return bytesRead; 494 } 495 496 506 507 void allocFileStorage( int bytesToClear ) 508 throws LogException { 509 int numberOfBlocks; int bytesRemaining; byte[] singleChar1 = new byte[1]; 512 byte[] singleChar2 = new byte[1]; 513 long bytesWritten; 514 515 516 if( bytesToClear == 0 ) { 517 return; 518 } 519 575 576 fileSeek(bytesToClear-1,SEEK_RELATIVE); 577 fileWrite(singleChar2); 578 579 582 fileSeek(-bytesToClear,SEEK_RELATIVE); 583 584 586 if( buffered ) 587 fileSync(); 588 589 } 590 } 591 | Popular Tags |