1 15 16 package installer; 17 18 import java.io.*; 19 20 21 37 38 public class 39 TarBuffer extends Object 40 { 41 public static final int DEFAULT_RCDSIZE = ( 512 ); 42 public static final int DEFAULT_BLKSIZE = ( DEFAULT_RCDSIZE * 20 ); 43 44 private InputStream inStream; 45 private OutputStream outStream; 46 47 private byte[] blockBuffer; 48 private int currBlkIdx; 49 private int currRecIdx; 50 private int blockSize; 51 private int recordSize; 52 private int recsPerBlock; 53 54 private boolean debug; 55 56 57 public 58 TarBuffer( InputStream inStream ) 59 { 60 this( inStream, TarBuffer.DEFAULT_BLKSIZE ); 61 } 62 63 public 64 TarBuffer( InputStream inStream, int blockSize ) 65 { 66 this( inStream, blockSize, TarBuffer.DEFAULT_RCDSIZE ); 67 } 68 69 public 70 TarBuffer( InputStream inStream, int blockSize, int recordSize ) 71 { 72 this.inStream = inStream; 73 this.outStream = null; 74 this.initialize( blockSize, recordSize ); 75 } 76 77 public 78 TarBuffer( OutputStream outStream ) 79 { 80 this( outStream, TarBuffer.DEFAULT_BLKSIZE ); 81 } 82 83 public 84 TarBuffer( OutputStream outStream, int blockSize ) 85 { 86 this( outStream, blockSize, TarBuffer.DEFAULT_RCDSIZE ); 87 } 88 89 public 90 TarBuffer( OutputStream outStream, int blockSize, int recordSize ) 91 { 92 this.inStream = null; 93 this.outStream = outStream; 94 this.initialize( blockSize, recordSize ); 95 } 96 97 100 private void 101 initialize( int blockSize, int recordSize ) 102 { 103 this.debug = false; 104 this.blockSize = blockSize; 105 this.recordSize = recordSize; 106 this.recsPerBlock = ( this.blockSize / this.recordSize ); 107 this.blockBuffer = new byte[ this.blockSize ]; 108 109 if ( this.inStream != null ) 110 { 111 this.currBlkIdx = -1; 112 this.currRecIdx = this.recsPerBlock; 113 } 114 else 115 { 116 this.currBlkIdx = 0; 117 this.currRecIdx = 0; 118 } 119 } 120 121 124 public int 125 getBlockSize() 126 { 127 return this.blockSize; 128 } 129 130 133 public int 134 getRecordSize() 135 { 136 return this.recordSize; 137 } 138 139 144 public void 145 setDebug( boolean debug ) 146 { 147 this.debug = debug; 148 } 149 150 156 public boolean 157 isEOFRecord( byte[] record ) 158 { 159 for ( int i = 0, sz = this.getRecordSize() ; i < sz ; ++i ) 160 if ( record[i] != 0 ) 161 return false; 162 163 return true; 164 } 165 166 169 170 public void 171 skipRecord() 172 throws IOException 173 { 174 if ( this.debug ) 175 { 176 System.err.println 177 ( "SkipRecord: recIdx = " + this.currRecIdx 178 + " blkIdx = " + this.currBlkIdx ); 179 } 180 181 if ( this.inStream == null ) 182 throw new IOException 183 ( "reading (via skip) from an output buffer" ); 184 185 if ( this.currRecIdx >= this.recsPerBlock ) 186 { 187 if ( ! this.readBlock() ) 188 return; } 190 191 this.currRecIdx++; 192 } 193 194 199 200 public byte[] 201 readRecord() 202 throws IOException 203 { 204 if ( this.debug ) 205 { 206 System.err.println 207 ( "ReadRecord: recIdx = " + this.currRecIdx 208 + " blkIdx = " + this.currBlkIdx ); 209 } 210 211 if ( this.inStream == null ) 212 throw new IOException 213 ( "reading from an output buffer" ); 214 215 if ( this.currRecIdx >= this.recsPerBlock ) 216 { 217 if ( ! this.readBlock() ) 218 return null; 219 } 220 221 byte[] result = new byte[ this.recordSize ]; 222 223 System.arraycopy( 224 this.blockBuffer, (this.currRecIdx * this.recordSize), 225 result, 0, this.recordSize ); 226 227 this.currRecIdx++; 228 229 return result; 230 } 231 232 235 236 private boolean 237 readBlock() 238 throws IOException 239 { 240 if ( this.debug ) 241 { 242 System.err.println 243 ( "ReadBlock: blkIdx = " + this.currBlkIdx ); 244 } 245 246 if ( this.inStream == null ) 247 throw new IOException 248 ( "reading from an output buffer" ); 249 250 this.currRecIdx = 0; 251 252 int offset = 0; 253 int bytesNeeded = this.blockSize; 254 for ( ; bytesNeeded > 0 ; ) 255 { 256 long numBytes = 257 this.inStream.read 258 ( this.blockBuffer, offset, bytesNeeded ); 259 260 274 if ( numBytes == -1 ) 275 break; 276 277 offset += numBytes; 278 bytesNeeded -= numBytes; 279 if ( numBytes != this.blockSize ) 280 { 281 if ( this.debug ) 282 { 283 System.err.println 284 ( "ReadBlock: INCOMPLETE READ " + numBytes 285 + " of " + this.blockSize + " bytes read." ); 286 } 287 } 288 } 289 290 this.currBlkIdx++; 291 292 return true; 293 } 294 295 300 public int 301 getCurrentBlockNum() 302 { 303 return this.currBlkIdx; 304 } 305 306 312 public int 313 getCurrentRecordNum() 314 { 315 return this.currRecIdx - 1; 316 } 317 318 323 324 public void 325 writeRecord( byte[] record ) 326 throws IOException 327 { 328 if ( this.debug ) 329 { 330 System.err.println 331 ( "WriteRecord: recIdx = " + this.currRecIdx 332 + " blkIdx = " + this.currBlkIdx ); 333 } 334 335 if ( this.outStream == null ) 336 throw new IOException 337 ( "writing to an input buffer" ); 338 339 if ( record.length != this.recordSize ) 340 throw new IOException 341 ( "record to write has length '" + record.length 342 + "' which is not the record size of '" 343 + this.recordSize + "'" ); 344 345 if ( this.currRecIdx >= this.recsPerBlock ) 346 { 347 this.writeBlock(); 348 } 349 350 System.arraycopy( 351 record, 0, 352 this.blockBuffer, (this.currRecIdx * this.recordSize), 353 this.recordSize ); 354 355 this.currRecIdx++; 356 } 357 358 366 367 public void 368 writeRecord( byte[] buf, int offset ) 369 throws IOException 370 { 371 if ( this.debug ) 372 { 373 System.err.println 374 ( "WriteRecord: recIdx = " + this.currRecIdx 375 + " blkIdx = " + this.currBlkIdx ); 376 } 377 378 if ( this.outStream == null ) 379 throw new IOException 380 ( "writing to an input buffer" ); 381 382 if ( (offset + this.recordSize) > buf.length ) 383 throw new IOException 384 ( "record has length '" + buf.length 385 + "' with offset '" + offset 386 + "' which is less than the record size of '" 387 + this.recordSize + "'" ); 388 389 if ( this.currRecIdx >= this.recsPerBlock ) 390 { 391 this.writeBlock(); 392 } 393 394 System.arraycopy( 395 buf, offset, 396 this.blockBuffer, (this.currRecIdx * this.recordSize), 397 this.recordSize ); 398 399 this.currRecIdx++; 400 } 401 402 405 private void 406 writeBlock() 407 throws IOException 408 { 409 if ( this.debug ) 410 { 411 System.err.println 412 ( "WriteBlock: blkIdx = " + this.currBlkIdx ); 413 } 414 415 if ( this.outStream == null ) 416 throw new IOException 417 ( "writing to an input buffer" ); 418 419 this.outStream.write( this.blockBuffer, 0, this.blockSize ); 420 this.outStream.flush(); 421 422 this.currRecIdx = 0; 423 this.currBlkIdx++; 424 } 425 426 429 430 private void 431 flushBlock() 432 throws IOException 433 { 434 if ( this.debug ) 435 { 436 System.err.println( "TarBuffer.flushBlock() called." ); 437 } 438 439 if ( this.outStream == null ) 440 throw new IOException 441 ( "writing to an input buffer" ); 442 443 if ( this.currRecIdx > 0 ) 444 { 445 this.writeBlock(); 446 } 447 } 448 449 453 public void 454 close() 455 throws IOException 456 { 457 if ( this.debug ) 458 { 459 System.err.println( "TarBuffer.closeBuffer()." ); 460 } 461 462 if ( this.outStream != null ) 463 { 464 this.flushBlock(); 465 466 if ( this.outStream != System.out 467 && this.outStream != System.err ) 468 { 469 this.outStream.close(); 470 this.outStream = null; 471 } 472 } 473 else if ( this.inStream != null ) 474 { 475 if ( this.inStream != System.in ) 476 { 477 this.inStream.close(); 478 this.inStream = null; 479 } 480 } 481 } 482 483 } 484 485 | Popular Tags |