Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 350   Methods: 15
NCLOC: 145   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TarOutputStream.java 61.5% 58.3% 66.7% 60.2%
 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.FilterOutputStream;
 63   
 import java.io.OutputStream;
 64   
 import java.io.IOException;
 65   
 
 66   
 /**
 67   
  * The TarOutputStream writes a UNIX tar archive as an OutputStream.
 68   
  * Methods are provided to put entries, and then write their contents
 69   
  * by writing to this stream using write().
 70   
  * 
 71   
  * @author Timothy Gerard Endres <a href="mailto:time@ice.com">time@ice.com</a>
 72   
  */
 73   
 public class TarOutputStream extends FilterOutputStream {
 74   
     /** Fail if a long file name is required in the archive. */
 75   
     public static final int LONGFILE_ERROR = 0;
 76   
     
 77   
     /** Long paths will be truncated in the archive. */
 78   
     public static final int LONGFILE_TRUNCATE = 1;
 79   
     
 80   
     /** GNU tar extensions are used to store long file names in the archive. */
 81   
     public static final int LONGFILE_GNU = 2;
 82   
     
 83   
     protected boolean   debug;
 84   
     protected int       currSize;
 85   
     protected int       currBytes;
 86   
     protected byte[]    oneBuf;
 87   
     protected byte[]    recordBuf;
 88   
     protected int       assemLen;
 89   
     protected byte[]    assemBuf;
 90   
     protected TarBuffer buffer;
 91   
     protected int       longFileMode = LONGFILE_ERROR;
 92   
 
 93  14
     public TarOutputStream(OutputStream os) {
 94  14
         this(os, TarBuffer.DEFAULT_BLKSIZE, TarBuffer.DEFAULT_RCDSIZE);
 95   
     }
 96   
 
 97  0
     public TarOutputStream(OutputStream os, int blockSize) {
 98  0
         this(os, blockSize, TarBuffer.DEFAULT_RCDSIZE);
 99   
     }
 100   
 
 101  14
     public TarOutputStream(OutputStream os, int blockSize, int recordSize) {
 102  14
         super(os);
 103   
 
 104  14
         this.buffer = new TarBuffer(os, blockSize, recordSize);
 105  14
         this.debug = false;
 106  14
         this.assemLen = 0;
 107  14
         this.assemBuf = new byte[recordSize];
 108  14
         this.recordBuf = new byte[recordSize];
 109  14
         this.oneBuf = new byte[1];
 110   
     }
 111   
 
 112  14
     public void setLongFileMode(int longFileMode) {
 113  14
         this.longFileMode = longFileMode;
 114   
     }
 115   
     
 116   
 
 117   
     /**
 118   
      * Sets the debugging flag.
 119   
      * 
 120   
      * @param debugF True to turn on debugging.
 121   
      */
 122  14
     public void setDebug(boolean debugF) {
 123  14
         this.debug = debugF;
 124   
     } 
 125   
 
 126   
     /**
 127   
      * Sets the debugging flag in this stream's TarBuffer.
 128   
      * 
 129   
      * @param debugF True to turn on debugging.
 130   
      */
 131  0
     public void setBufferDebug(boolean debug) {
 132  0
         this.buffer.setDebug(debug);
 133   
     } 
 134   
 
 135   
     /**
 136   
      * Ends the TAR archive without closing the underlying OutputStream.
 137   
      * The result is that the EOF record of nulls is written.
 138   
      */
 139  14
     public void finish() throws IOException {
 140  14
         this.writeEOFRecord();
 141   
     } 
 142   
 
 143   
     /**
 144   
      * Ends the TAR archive and closes the underlying OutputStream.
 145   
      * This means that finish() is called followed by calling the
 146   
      * TarBuffer's close().
 147   
      */
 148  14
     public void close() throws IOException {
 149  14
         this.finish();
 150  14
         this.buffer.close();
 151   
     } 
 152   
 
 153   
     /**
 154   
      * Get the record size being used by this stream's TarBuffer.
 155   
      * 
 156   
      * @return The TarBuffer record size.
 157   
      */
 158  0
     public int getRecordSize() {
 159  0
         return this.buffer.getRecordSize();
 160   
     } 
 161   
 
 162   
     /**
 163   
      * Put an entry on the output stream. This writes the entry's
 164   
      * header record and positions the output stream for writing
 165   
      * the contents of the entry. Once this method is called, the
 166   
      * stream is ready for calls to write() to write the entry's
 167   
      * contents. Once the contents are written, closeEntry()
 168   
      * <B>MUST</B> be called to ensure that all buffered data
 169   
      * is completely written to the output stream.
 170   
      * 
 171   
      * @param entry The TarEntry to be written to the archive.
 172   
      */
 173  15
     public void putNextEntry(TarEntry entry) throws IOException {
 174  15
         if (entry.getName().length() >= TarConstants.NAMELEN) {
 175   
 
 176  0
             if (longFileMode == LONGFILE_GNU) {
 177   
                 // create a TarEntry for the LongLink, the contents
 178   
                 // of which are the entry's name 
 179  0
                 TarEntry longLinkEntry = new TarEntry(TarConstants.GNU_LONGLINK,
 180   
                                                       TarConstants.LF_GNUTYPE_LONGNAME);
 181   
                  
 182  0
                 longLinkEntry.setSize(entry.getName().length() + 1);
 183  0
                 putNextEntry(longLinkEntry);                                                    
 184  0
                 write(entry.getName().getBytes());
 185  0
                 write(0);
 186  0
                 closeEntry();
 187  0
             } else if (longFileMode != LONGFILE_TRUNCATE) {
 188  0
                 throw new RuntimeException("file name '" + entry.getName() 
 189   
                                              + "' is too long ( > " 
 190   
                                              + TarConstants.NAMELEN + " bytes)");
 191   
             }
 192   
         } 
 193   
 
 194  15
         entry.writeEntryHeader(this.recordBuf);
 195  15
         this.buffer.writeRecord(this.recordBuf);
 196   
 
 197  15
         this.currBytes = 0;
 198   
 
 199  15
         if (entry.isDirectory()) {
 200  3
             this.currSize = 0;
 201   
         } else {
 202  12
             this.currSize = (int) entry.getSize();
 203   
         }
 204   
     } 
 205   
 
 206   
     /**
 207   
      * Close an entry. This method MUST be called for all file
 208   
      * entries that contain data. The reason is that we must
 209   
      * buffer data written to the stream in order to satisfy
 210   
      * the buffer's record based writes. Thus, there may be
 211   
      * data fragments still being assembled that must be written
 212   
      * to the output stream before this entry is closed and the
 213   
      * next entry written.
 214   
      */
 215  15
     public void closeEntry() throws IOException {
 216  15
         if (this.assemLen > 0) {
 217  12
             for (int i = this.assemLen; i < this.assemBuf.length; ++i) {
 218  4677
                 this.assemBuf[i] = 0;
 219   
             }
 220   
 
 221  12
             this.buffer.writeRecord(this.assemBuf);
 222   
 
 223  12
             this.currBytes += this.assemLen;
 224  12
             this.assemLen = 0;
 225   
         } 
 226   
 
 227  15
         if (this.currBytes < this.currSize) {
 228  0
             throw new IOException("entry closed at '" + this.currBytes 
 229   
                                   + "' before the '" + this.currSize 
 230   
                                   + "' bytes specified in the header were written");
 231   
         } 
 232   
     } 
 233   
 
 234   
     /**
 235   
      * Writes a byte to the current tar archive entry.
 236   
      * 
 237   
      * This method simply calls read( byte[], int, int ).
 238   
      * 
 239   
      * @param b The byte written.
 240   
      */
 241  0
     public void write(int b) throws IOException {
 242  0
         this.oneBuf[0] = (byte) b;
 243   
 
 244  0
         this.write(this.oneBuf, 0, 1);
 245   
     } 
 246   
 
 247   
     /**
 248   
      * Writes bytes to the current tar archive entry.
 249   
      * 
 250   
      * This method simply calls write( byte[], int, int ).
 251   
      * 
 252   
      * @param wBuf The buffer to write to the archive.
 253   
      * @return The number of bytes read, or -1 at EOF.
 254   
      */
 255  0
     public void write(byte[] wBuf) throws IOException {
 256  0
         this.write(wBuf, 0, wBuf.length);
 257   
     } 
 258   
 
 259   
     /**
 260   
      * Writes bytes to the current tar archive entry. This method
 261   
      * is aware of the current entry and will throw an exception if
 262   
      * you attempt to write bytes past the length specified for the
 263   
      * current entry. The method is also (painfully) aware of the
 264   
      * record buffering required by TarBuffer, and manages buffers
 265   
      * that are not a multiple of recordsize in length, including
 266   
      * assembling records from small buffers.
 267   
      * 
 268   
      * @param wBuf The buffer to write to the archive.
 269   
      * @param wOffset The offset in the buffer from which to get bytes.
 270   
      * @param numToWrite The number of bytes to write.
 271   
      */
 272  24
     public void write(byte[] wBuf, int wOffset, int numToWrite) throws IOException {
 273  24
         if ((this.currBytes + numToWrite) > this.currSize) {
 274  0
             throw new IOException("request to write '" + numToWrite 
 275   
                                   + "' bytes exceeds size in header of '" 
 276   
                                   + this.currSize + "' bytes");
 277   
 
 278   
             // 
 279   
             // We have to deal with assembly!!!
 280   
             // The programmer can be writing little 32 byte chunks for all
 281   
             // we know, and we must assemble complete records for writing.
 282   
             // REVIEW Maybe this should be in TarBuffer? Could that help to
 283   
             // eliminate some of the buffer copying.
 284   
             // 
 285   
         } 
 286   
 
 287  24
         if (this.assemLen > 0) {
 288  0
             if ((this.assemLen + numToWrite) >= this.recordBuf.length) {
 289  0
                 int aLen = this.recordBuf.length - this.assemLen;
 290   
 
 291  0
                 System.arraycopy(this.assemBuf, 0, this.recordBuf, 0, 
 292   
                                  this.assemLen);
 293  0
                 System.arraycopy(wBuf, wOffset, this.recordBuf, 
 294   
                                  this.assemLen, aLen);
 295  0
                 this.buffer.writeRecord(this.recordBuf);
 296   
 
 297  0
                 this.currBytes += this.recordBuf.length;
 298  0
                 wOffset += aLen;
 299  0
                 numToWrite -= aLen;
 300  0
                 this.assemLen = 0;
 301   
             } else {
 302  0
                 System.arraycopy(wBuf, wOffset, this.assemBuf, this.assemLen, 
 303   
                                  numToWrite);
 304   
 
 305  0
                 wOffset += numToWrite;
 306  0
                 this.assemLen += numToWrite;
 307  0
                 numToWrite -= numToWrite;
 308   
             } 
 309   
         } 
 310   
 
 311   
         // 
 312   
         // When we get here we have EITHER:
 313   
         // o An empty "assemble" buffer.
 314   
         // o No bytes to write (numToWrite == 0)
 315   
         // 
 316  24
         while (numToWrite > 0) {
 317  153
             if (numToWrite < this.recordBuf.length) {
 318  12
                 System.arraycopy(wBuf, wOffset, this.assemBuf, this.assemLen, 
 319   
                                  numToWrite);
 320   
 
 321  12
                 this.assemLen += numToWrite;
 322   
 
 323  12
                 break;
 324   
             } 
 325   
 
 326  141
             this.buffer.writeRecord(wBuf, wOffset);
 327   
 
 328  141
             int num = this.recordBuf.length;
 329   
 
 330  141
             this.currBytes += num;
 331  141
             numToWrite -= num;
 332  141
             wOffset += num;
 333   
         } 
 334   
     } 
 335   
 
 336   
     /**
 337   
      * Write an EOF (end of archive) record to the tar archive.
 338   
      * An EOF record consists of a record of all zeros.
 339   
      */
 340  14
     private void writeEOFRecord() throws IOException {
 341  14
         for (int i = 0; i < this.recordBuf.length; ++i) {
 342  7168
             this.recordBuf[i] = 0;
 343   
         }
 344   
 
 345  14
         this.buffer.writeRecord(this.recordBuf);
 346   
     } 
 347   
 }
 348   
 
 349   
 
 350