Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 443   Methods: 21
NCLOC: 225   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TarBuffer.java 54.3% 67.6% 57.1% 61.9%
 1   
 /*
 2   
  * The Apache Software License, Version 1.1
 3   
  *
 4   
  * Copyright (c) 2000,2002 The Apache Software Foundation.  All rights
 5   
  * reserved.
 6   
  *
 7   
  * Redistribution and use in source and binary forms, with or without
 8   
  * modification, are permitted provided that the following conditions
 9   
  * are met:
 10   
  *
 11   
  * 1. Redistributions of source code must retain the above copyright
 12   
  *    notice, this list of conditions and the following disclaimer.
 13   
  *
 14   
  * 2. Redistributions in binary form must reproduce the above copyright
 15   
  *    notice, this list of conditions and the following disclaimer in
 16   
  *    the documentation and/or other materials provided with the
 17   
  *    distribution.
 18   
  *
 19   
  * 3. The end-user documentation included with the redistribution, if
 20   
  *    any, must include the following acknowlegement:
 21   
  *       "This product includes software developed by the
 22   
  *        Apache Software Foundation (http://www.apache.org/)."
 23   
  *    Alternately, this acknowlegement may appear in the software itself,
 24   
  *    if and wherever such third-party acknowlegements normally appear.
 25   
  *
 26   
  * 4. The names "Ant" and "Apache Software
 27   
  *    Foundation" must not be used to endorse or promote products derived
 28   
  *    from this software without prior written permission. For written
 29   
  *    permission, please contact apache@apache.org.
 30   
  *
 31   
  * 5. Products derived from this software may not be called "Apache"
 32   
  *    nor may "Apache" appear in their names without prior written
 33   
  *    permission of the Apache Group.
 34   
  *
 35   
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 36   
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 37   
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 38   
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 39   
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 40   
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 41   
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 42   
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 43   
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 44   
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 45   
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 46   
  * SUCH DAMAGE.
 47   
  * ====================================================================
 48   
  *
 49   
  * This software consists of voluntary contributions made by many
 50   
  * individuals on behalf of the Apache Software Foundation.  For more
 51   
  * information on the Apache Software Foundation, please see
 52   
  * <http://www.apache.org/>.
 53   
  */
 54   
 
 55   
 /*
 56   
  * This package is based on the work done by Timothy Gerard Endres 
 57   
  * (time@ice.com) to whom the Ant project is very grateful for his great code.
 58   
  */
 59   
  
 60   
 package org.apache.tools.tar;
 61   
 
 62   
 import java.io.InputStream;
 63   
 import java.io.OutputStream;
 64   
 import java.io.IOException;
 65   
 
 66   
 /**
 67   
  * The TarBuffer class implements the tar archive concept
 68   
  * of a buffered input stream. This concept goes back to the
 69   
  * days of blocked tape drives and special io devices. In the
 70   
  * Java universe, the only real function that this class
 71   
  * performs is to ensure that files have the correct "block"
 72   
  * size, or other tars will complain.
 73   
  * <p>
 74   
  * You should never have a need to access this class directly.
 75   
  * TarBuffers are created by Tar IO Streams.
 76   
  * 
 77   
  * @author Timothy Gerard Endres <a href="mailto:time@ice.com">time@ice.com</a>
 78   
  */
 79   
  
 80   
 public class TarBuffer {
 81   
         
 82   
     public static final int DEFAULT_RCDSIZE = (512);
 83   
     public static final int DEFAULT_BLKSIZE = (DEFAULT_RCDSIZE * 20);
 84   
     
 85   
     private InputStream     inStream;
 86   
     private OutputStream    outStream;
 87   
     private byte[]          blockBuffer;
 88   
     private int             currBlkIdx;
 89   
     private int             currRecIdx;
 90   
     private int             blockSize;
 91   
     private int             recordSize;
 92   
     private int             recsPerBlock;
 93   
     private boolean         debug;
 94   
 
 95  0
     public TarBuffer(InputStream inStream) {
 96  0
         this(inStream, TarBuffer.DEFAULT_BLKSIZE);
 97   
     }
 98   
 
 99  0
     public TarBuffer(InputStream inStream, int blockSize) {
 100  0
         this(inStream, blockSize, TarBuffer.DEFAULT_RCDSIZE);
 101   
     }
 102   
 
 103  10
     public TarBuffer(InputStream inStream, int blockSize, int recordSize) {
 104  10
         this.inStream = inStream;
 105  10
         this.outStream = null;
 106   
 
 107  10
         this.initialize(blockSize, recordSize);
 108   
     }
 109   
 
 110  0
     public TarBuffer(OutputStream outStream) {
 111  0
         this(outStream, TarBuffer.DEFAULT_BLKSIZE);
 112   
     }
 113   
 
 114  0
     public TarBuffer(OutputStream outStream, int blockSize) {
 115  0
         this(outStream, blockSize, TarBuffer.DEFAULT_RCDSIZE);
 116   
     }
 117   
 
 118  14
     public TarBuffer(OutputStream outStream, int blockSize, int recordSize) {
 119  14
         this.inStream = null;
 120  14
         this.outStream = outStream;
 121   
 
 122  14
         this.initialize(blockSize, recordSize);
 123   
     }
 124   
 
 125   
     /**
 126   
      * Initialization common to all constructors.
 127   
      */
 128  24
     private void initialize(int blockSize, int recordSize) {
 129  24
         this.debug = false;
 130  24
         this.blockSize = blockSize;
 131  24
         this.recordSize = recordSize;
 132  24
         this.recsPerBlock = (this.blockSize / this.recordSize);
 133  24
         this.blockBuffer = new byte[this.blockSize];
 134   
 
 135  24
         if (this.inStream != null) {
 136  10
             this.currBlkIdx = -1;
 137  10
             this.currRecIdx = this.recsPerBlock;
 138   
         } else {
 139  14
             this.currBlkIdx = 0;
 140  14
             this.currRecIdx = 0;
 141   
         } 
 142   
     } 
 143   
 
 144   
     /**
 145   
      * Get the TAR Buffer's block size. Blocks consist of multiple records.
 146   
      */
 147  0
     public int getBlockSize() {
 148  0
         return this.blockSize;
 149   
     } 
 150   
 
 151   
     /**
 152   
      * Get the TAR Buffer's record size.
 153   
      */
 154  21
     public int getRecordSize() {
 155  21
         return this.recordSize;
 156   
     } 
 157   
 
 158   
     /**
 159   
      * Set the debugging flag for the buffer.
 160   
      * 
 161   
      * @param debug If true, print debugging output.
 162   
      */
 163  0
     public void setDebug(boolean debug) {
 164  0
         this.debug = debug;
 165   
     } 
 166   
 
 167   
     /**
 168   
      * Determine if an archive record indicate End of Archive. End of
 169   
      * archive is indicated by a record that consists entirely of null bytes.
 170   
      * 
 171   
      * @param record The record data to check.
 172   
      */
 173  21
     public boolean isEOFRecord(byte[] record) {
 174  21
         for (int i = 0, sz = this.getRecordSize(); i < sz; ++i) {
 175  5131
             if (record[i] != 0) {
 176  11
                 return false;
 177   
             } 
 178   
         }
 179   
 
 180  10
         return true;
 181   
     } 
 182   
 
 183   
     /**
 184   
      * Skip over a record on the input stream.
 185   
      */
 186  0
     public void skipRecord() throws IOException {
 187  0
         if (this.debug) {
 188  0
             System.err.println("SkipRecord: recIdx = " + this.currRecIdx 
 189   
                                + " blkIdx = " + this.currBlkIdx);
 190   
         } 
 191   
 
 192  0
         if (this.inStream == null) {
 193  0
             throw new IOException("reading (via skip) from an output buffer");
 194   
         } 
 195   
 
 196  0
         if (this.currRecIdx >= this.recsPerBlock) {
 197  0
             if (!this.readBlock()) {
 198  0
                 return;    // UNDONE
 199   
             } 
 200   
         } 
 201   
 
 202  0
         this.currRecIdx++;
 203   
     } 
 204   
 
 205   
     /**
 206   
      * Read a record from the input stream and return the data.
 207   
      * 
 208   
      * @return The record data.
 209   
      */
 210  129
     public byte[] readRecord() throws IOException {
 211  129
         if (this.debug) {
 212  0
             System.err.println("ReadRecord: recIdx = " + this.currRecIdx 
 213   
                                + " blkIdx = " + this.currBlkIdx);
 214   
         } 
 215   
 
 216  129
         if (this.inStream == null) {
 217  0
             throw new IOException("reading from an output buffer");
 218   
         } 
 219   
 
 220  129
         if (this.currRecIdx >= this.recsPerBlock) {
 221  10
             if (!this.readBlock()) {
 222  0
                 return null;
 223   
             } 
 224   
         } 
 225   
 
 226  129
         byte[] result = new byte[this.recordSize];
 227   
 
 228  129
         System.arraycopy(this.blockBuffer, 
 229   
                          (this.currRecIdx * this.recordSize), result, 0, 
 230   
                          this.recordSize);
 231   
 
 232  129
         this.currRecIdx++;
 233   
 
 234  129
         return result;
 235   
     } 
 236   
 
 237   
     /**
 238   
      * @return false if End-Of-File, else true
 239   
      */
 240  10
     private boolean readBlock() throws IOException {
 241  10
         if (this.debug) {
 242  0
             System.err.println("ReadBlock: blkIdx = " + this.currBlkIdx);
 243   
         } 
 244   
 
 245  10
         if (this.inStream == null) {
 246  0
             throw new IOException("reading from an output buffer");
 247   
         } 
 248   
 
 249  10
         this.currRecIdx = 0;
 250   
 
 251  10
         int offset = 0;
 252  10
         int bytesNeeded = this.blockSize;
 253   
 
 254  10
         while (bytesNeeded > 0) {
 255  37
             long numBytes = this.inStream.read(this.blockBuffer, offset, 
 256   
                                                bytesNeeded);
 257   
 
 258   
             // 
 259   
             // NOTE
 260   
             // We have fit EOF, and the block is not full!
 261   
             // 
 262   
             // This is a broken archive. It does not follow the standard
 263   
             // blocking algorithm. However, because we are generous, and
 264   
             // it requires little effort, we will simply ignore the error
 265   
             // and continue as if the entire block were read. This does
 266   
             // not appear to break anything upstream. We used to return
 267   
             // false in this case.
 268   
             // 
 269   
             // Thanks to 'Yohann.Roussel@alcatel.fr' for this fix.
 270   
             // 
 271  37
             if (numBytes == -1) {
 272  0
                 break;
 273   
             } 
 274   
 
 275  37
             offset += numBytes;
 276  37
             bytesNeeded -= numBytes;
 277   
 
 278  37
             if (numBytes != this.blockSize) {
 279  30
                 if (this.debug) {
 280  0
                     System.err.println("ReadBlock: INCOMPLETE READ " 
 281   
                                        + numBytes + " of " + this.blockSize 
 282   
                                        + " bytes read.");
 283   
                 } 
 284   
             } 
 285   
         } 
 286   
 
 287  10
         this.currBlkIdx++;
 288   
 
 289  10
         return true;
 290   
     } 
 291   
 
 292   
     /**
 293   
      * Get the current block number, zero based.
 294   
      * 
 295   
      * @return The current zero based block number.
 296   
      */
 297  0
     public int getCurrentBlockNum() {
 298  0
         return this.currBlkIdx;
 299   
     } 
 300   
 
 301   
     /**
 302   
      * Get the current record number, within the current block, zero based.
 303   
      * Thus, current offset = (currentBlockNum * recsPerBlk) + currentRecNum.
 304   
      * 
 305   
      * @return The current zero based record number.
 306   
      */
 307  0
     public int getCurrentRecordNum() {
 308  0
         return this.currRecIdx - 1;
 309   
     } 
 310   
 
 311   
     /**
 312   
      * Write an archive record to the archive.
 313   
      * 
 314   
      * @param record The record data to write to the archive.
 315   
      */
 316  41
     public void writeRecord(byte[] record) throws IOException {
 317  41
         if (this.debug) {
 318  0
             System.err.println("WriteRecord: recIdx = " + this.currRecIdx 
 319   
                                + " blkIdx = " + this.currBlkIdx);
 320   
         } 
 321   
 
 322  41
         if (this.outStream == null) {
 323  0
             throw new IOException("writing to an input buffer");
 324   
         } 
 325   
 
 326  41
         if (record.length != this.recordSize) {
 327  0
             throw new IOException("record to write has length '" 
 328   
                                   + record.length 
 329   
                                   + "' which is not the record size of '" 
 330   
                                   + this.recordSize + "'");
 331   
         } 
 332   
 
 333  41
         if (this.currRecIdx >= this.recsPerBlock) {
 334  0
             this.writeBlock();
 335   
         } 
 336   
 
 337  41
         System.arraycopy(record, 0, this.blockBuffer, 
 338   
                          (this.currRecIdx * this.recordSize), 
 339   
                          this.recordSize);
 340   
 
 341  41
         this.currRecIdx++;
 342   
     } 
 343   
 
 344   
     /**
 345   
      * Write an archive record to the archive, where the record may be
 346   
      * inside of a larger array buffer. The buffer must be "offset plus
 347   
      * record size" long.
 348   
      * 
 349   
      * @param buf The buffer containing the record data to write.
 350   
      * @param offset The offset of the record data within buf.
 351   
      */
 352  141
     public void writeRecord(byte[] buf, int offset) throws IOException {
 353  141
         if (this.debug) {
 354  0
             System.err.println("WriteRecord: recIdx = " + this.currRecIdx 
 355   
                                + " blkIdx = " + this.currBlkIdx);
 356   
         } 
 357   
 
 358  141
         if (this.outStream == null) {
 359  0
             throw new IOException("writing to an input buffer");
 360   
         } 
 361   
 
 362  141
         if ((offset + this.recordSize) > buf.length) {
 363  0
             throw new IOException("record has length '" + buf.length 
 364   
                                   + "' with offset '" + offset 
 365   
                                   + "' which is less than the record size of '" 
 366   
                                   + this.recordSize + "'");
 367   
         } 
 368   
 
 369  141
         if (this.currRecIdx >= this.recsPerBlock) {
 370  0
             this.writeBlock();
 371   
         } 
 372   
 
 373  141
         System.arraycopy(buf, offset, this.blockBuffer, 
 374   
                          (this.currRecIdx * this.recordSize), 
 375   
                          this.recordSize);
 376   
 
 377  141
         this.currRecIdx++;
 378   
     } 
 379   
 
 380   
     /**
 381   
      * Write a TarBuffer block to the archive.
 382   
      */
 383  14
     private void writeBlock() throws IOException {
 384  14
         if (this.debug) {
 385  0
             System.err.println("WriteBlock: blkIdx = " + this.currBlkIdx);
 386   
         } 
 387   
 
 388  14
         if (this.outStream == null) {
 389  0
             throw new IOException("writing to an input buffer");
 390   
         } 
 391   
 
 392  14
         this.outStream.write(this.blockBuffer, 0, this.blockSize);
 393  14
         this.outStream.flush();
 394   
 
 395  14
         this.currRecIdx = 0;
 396  14
         this.currBlkIdx++;
 397   
     } 
 398   
 
 399   
     /**
 400   
      * Flush the current data block if it has any data in it.
 401   
      */
 402  14
     private void flushBlock() throws IOException {
 403  14
         if (this.debug) {
 404  0
             System.err.println("TarBuffer.flushBlock() called.");
 405   
         } 
 406   
 
 407  14
         if (this.outStream == null) {
 408  0
             throw new IOException("writing to an input buffer");
 409   
         } 
 410   
 
 411  14
         if (this.currRecIdx > 0) {
 412  14
             this.writeBlock();
 413   
         } 
 414   
     } 
 415   
 
 416   
     /**
 417   
      * Close the TarBuffer. If this is an output buffer, also flush the
 418   
      * current block before closing.
 419   
      */
 420  24
     public void close() throws IOException {
 421  24
         if (this.debug) {
 422  0
             System.err.println("TarBuffer.closeBuffer().");
 423   
         } 
 424   
 
 425  24
         if (this.outStream != null) {
 426  14
             this.flushBlock();
 427   
 
 428  14
             if (this.outStream != System.out 
 429   
                     && this.outStream != System.err) {
 430  14
                 this.outStream.close();
 431   
 
 432  14
                 this.outStream = null;
 433   
             } 
 434  10
         } else if (this.inStream != null) {
 435  10
             if (this.inStream != System.in) {
 436  10
                 this.inStream.close();
 437   
 
 438  10
                 this.inStream = null;
 439   
             } 
 440   
         } 
 441   
     } 
 442   
 }
 443