Clover coverage report - Ant Coverage
Coverage timestamp: Tue Apr 8 2003 20:43:55 EST
file stats: LOC: 234   Methods: 7
NCLOC: 84   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TarUtils.java 96.2% 100% 100% 98.8%
 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   
 /**
 63   
  * This class provides static utility methods to work with byte streams.
 64   
  * 
 65   
  * @author Timothy Gerard Endres <a href="mailto:time@ice.com">time@ice.com</a>
 66   
  * @author Stefano Mazzocchi <a href="mailto:stefano@apache.org">stefano@apache.org</a>
 67   
  */
 68   
 public class TarUtils {
 69   
 
 70   
     /**
 71   
      * Parse an octal string from a header buffer. This is used for the
 72   
      * file permission mode value.
 73   
      * 
 74   
      * @param header The header buffer from which to parse.
 75   
      * @param offset The offset into the buffer from which to parse.
 76   
      * @param length The number of header bytes to parse.
 77   
      * @return The long value of the octal string.
 78   
      */
 79  88
     public static long parseOctal(byte[] header, int offset, int length) {
 80  88
         long    result = 0;
 81  88
         boolean stillPadding = true;
 82  88
         int     end = offset + length;
 83   
 
 84  88
         for (int i = offset; i < end; ++i) {
 85  731
             if (header[i] == 0) {
 86  64
                 break;
 87   
             } 
 88   
 
 89  667
             if (header[i] == (byte) ' ' || header[i] == '0') {
 90  414
                 if (stillPadding) {
 91  358
                     continue;
 92   
                 } 
 93   
 
 94  56
                 if (header[i] == (byte) ' ') {
 95  22
                     break;
 96   
                 } 
 97   
             } 
 98   
 
 99  287
             stillPadding = false;
 100  287
             result = (result << 3) + (header[i] - '0');
 101   
         } 
 102   
 
 103  88
         return result;
 104   
     } 
 105   
 
 106   
     /**
 107   
      * Parse an entry name from a header buffer.
 108   
      * 
 109   
      * @param header The header buffer from which to parse.
 110   
      * @param offset The offset into the buffer from which to parse.
 111   
      * @param length The number of header bytes to parse.
 112   
      * @return The header's entry name.
 113   
      */
 114  55
     public static StringBuffer parseName(byte[] header, int offset, int length) {
 115  55
         StringBuffer result = new StringBuffer(length);
 116  55
         int          end = offset + length;
 117   
 
 118  290
         for (int i = offset; i < end; ++i) {
 119  290
             if (header[i] == 0) {
 120  55
                 break;
 121   
             } 
 122   
 
 123  235
             result.append((char) header[i]);
 124   
         } 
 125   
 
 126  55
         return result;
 127   
     } 
 128   
 
 129   
     /**
 130   
      * Determine the number of bytes in an entry name.
 131   
      * 
 132   
      * @param header The header buffer from which to parse.
 133   
      * @param offset The offset into the buffer from which to parse.
 134   
      * @param length The number of header bytes to parse.
 135   
      * @return The number of bytes in a header's entry name.
 136   
      */
 137  75
     public static int getNameBytes(StringBuffer name, byte[] buf, int offset, int length) {
 138  75
         int i;
 139   
 
 140  75
         for (i = 0; i < length && i < name.length(); ++i) {
 141  252
             buf[offset + i] = (byte) name.charAt(i);
 142   
         } 
 143   
 
 144  75
         for (; i < length; ++i) {
 145  3828
             buf[offset + i] = 0;
 146   
         } 
 147   
 
 148  75
         return offset + length;
 149   
     } 
 150   
 
 151   
     /**
 152   
      * Parse an octal integer from a header buffer.
 153   
      * 
 154   
      * @param header The header buffer from which to parse.
 155   
      * @param offset The offset into the buffer from which to parse.
 156   
      * @param length The number of header bytes to parse.
 157   
      * @return The integer value of the octal bytes.
 158   
      */
 159  120
     public static int getOctalBytes(long value, byte[] buf, int offset, int length) {
 160  120
         int    idx = length - 1;
 161   
 
 162  120
         buf[offset + idx] = 0;
 163  120
         --idx;
 164  120
         buf[offset + idx] = (byte) ' ';
 165  120
         --idx;
 166   
 
 167  120
         if (value == 0) {
 168  63
             buf[offset + idx] = (byte) '0';
 169  63
             --idx;
 170   
         } else {
 171  57
             for (long val = value; idx >= 0 && val > 0; --idx) {
 172  366
                 buf[offset + idx] = (byte) ((byte) '0' + (byte) (val & 7));
 173  366
                 val = val >> 3;
 174   
             } 
 175   
         } 
 176   
 
 177  120
         for (; idx >= 0; --idx) {
 178  441
             buf[offset + idx] = (byte) ' ';
 179   
         } 
 180   
 
 181  120
         return offset + length;
 182   
     } 
 183   
 
 184   
     /**
 185   
      * Parse an octal long integer from a header buffer.
 186   
      * 
 187   
      * @param header The header buffer from which to parse.
 188   
      * @param offset The offset into the buffer from which to parse.
 189   
      * @param length The number of header bytes to parse.
 190   
      * @return The long value of the octal bytes.
 191   
      */
 192  30
     public static int getLongOctalBytes(long value, byte[] buf, int offset, int length) {
 193  30
         byte[] temp = new byte[length + 1];
 194   
 
 195  30
         getOctalBytes(value, temp, 0, length + 1);
 196  30
         System.arraycopy(temp, 0, buf, offset, length);
 197   
 
 198  30
         return offset + length;
 199   
     } 
 200   
 
 201   
     /**
 202   
      * Parse the checksum octal integer from a header buffer.
 203   
      * 
 204   
      * @param header The header buffer from which to parse.
 205   
      * @param offset The offset into the buffer from which to parse.
 206   
      * @param length The number of header bytes to parse.
 207   
      * @return The integer value of the entry's checksum.
 208   
      */
 209  15
     public static int getCheckSumOctalBytes(long value, byte[] buf, int offset, int length) {
 210  15
         getOctalBytes(value, buf, offset, length);
 211   
 
 212  15
         buf[offset + length - 1] = (byte) ' ';
 213  15
         buf[offset + length - 2] = 0;
 214   
 
 215  15
         return offset + length;
 216   
     }
 217   
     
 218   
     /** 
 219   
      * Compute the checksum of a tar entry header.
 220   
      *  
 221   
      * @param buf The tar entry's header buffer.
 222   
      * @return The computed checksum.
 223   
      */ 
 224  15
     public static long computeCheckSum(byte[] buf) {
 225  15
         long sum = 0;
 226   
         
 227  15
         for (int i = 0; i < buf.length; ++i) {
 228  7680
             sum += 255 & buf[i];
 229   
         } 
 230   
         
 231  15
         return sum;
 232   
     }    
 233   
 }
 234