Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/media/mn8/util/tar/TarHeader.java


1   /* 
2    * $COPYRIGHT$
3    * $Id: TarHeader.java,v 1.1 2002/02/27 19:07:34 crow Exp $
4    *
5    * Date        Author            Changes 
6    * FEB 27 2002 Szabo Csaba       Created
7    */
8   
9   package org.media.mn8.util.tar;
10  
11  /**
12   * @author <a href="mailto:crow@nolimits.ro">Szabo Csaba</a>
13   * @version $Revision: 1.1 $ $Date: 2002/02/27 19:07:34 $
14   */
15  
16  public class TarHeader extends Object {
17  
18      //The length of the name field in a header buffer.
19      public static final int         NAMELEN = 100;
20  
21      //The length of the mode field in a header buffer.
22      public static final int         MODELEN = 8;
23  
24      //The length of the user id field in a header buffer.
25      public static final int         UIDLEN = 8;
26  
27      //The length of the group id field in a header buffer.
28      public static final int         GIDLEN = 8;
29  
30      //The length of the checksum field in a header buffer.
31      public static final int         CHKSUMLEN = 8;
32  
33      //The length of the size field in a header buffer.
34      public static final int         SIZELEN = 12;
35  
36      //The length of the magic field in a header buffer.
37      public static final int         MAGICLEN = 8;
38  
39      //The length of the modification time field in a header buffer.
40      public static final int         MODTIMELEN = 12;
41  
42      //The length of the user name field in a header buffer.
43      public static final int         UNAMELEN = 32;
44  
45      //The length of the group name field in a header buffer.
46      public static final int         GNAMELEN = 32; 
47  
48      //The length of the devices field in a header buffer. 
49      public static final int         DEVLEN = 8; 
50  
51      /**
52       * LF_ constants represent the "link flag" of an entry, or more commonly,
53       * the "entry type". This is the "old way" of indicating a normal file.
54       */
55      public static final byte        LF_OLDNORM      = 0;
56      public static final byte        LF_NORMAL       = (byte) '0';
57      public static final byte        LF_LINK         = (byte) '1';
58      public static final byte        LF_SYMLINK      = (byte) '2';
59      public static final byte        LF_CHR          = (byte) '3';
60      public static final byte        LF_BLK          = (byte) '4';
61      public static final byte        LF_DIR          = (byte) '5';
62      public static final byte        LF_FIFO         = (byte) '6';
63      public static final byte        LF_CONTIG       = (byte) '7';
64      
65      //The magic tag representing a POSIX tar archive.
66      public static final String      TMAGIC          = "ustar"; 
67      
68      //The magic tag representing a GNU tar archive.
69      public static final String      GNU_TMAGIC      = "ustar  ";  
70  
71      //The entry's name.
72      public StringBuffer             name;
73  
74      //The entry's permission mode.
75      public int                      mode;
76      
77      //The entry's user id.
78      public int                      userId;
79  
80      //The entry's group id.
81      public int                      groupId;
82  
83      //The entry's size.
84      public long                     size;
85  
86      //The entry's modification time.
87      public long                     modTime;
88      
89      //The entry's checksum.
90      public int                      checkSum;
91  
92      //The entry's link flag.
93      public byte                     linkFlag;
94      
95      //The entry's link name.
96      public StringBuffer             linkName;
97  
98      //The entry's magic tag.
99      public StringBuffer             magic;
100 
101     //The entry's user name.
102     public StringBuffer             userName;
103 
104     //The entry's group name.
105     public StringBuffer             groupName;
106 
107     //The entry's major device number.
108     public int                      devMajor;
109     
110     //The entry's minor device number.
111     public int                      devMinor;
112     
113     
114     public static StringBuffer parseName( byte[] header, int offset, int length ) throws Exception {
115         StringBuffer result = new StringBuffer( length );
116  
117         int end = offset + length;
118         for ( int i = offset ; i < end ; ++i ) {
119             if ( header[i] == 0 ) break;
120             result.append( (char)header[i] );
121         }
122         
123         return result;
124     }
125 
126 
127     public static long parseOctal( byte[] header, int offset, int length ) throws Exception {
128         long     result = 0;
129         boolean padding = true;
130         int         end = offset + length;
131         
132         for ( int i = offset; i < end; ++i ) {
133             if ( header[i] == 0 ) break;
134             if ( header[i] == (byte)' ' || header[i] == '0' ) {
135                 if ( padding ) continue;
136                 if ( header[i] == (byte)' ' ) break;
137             }
138             padding = false;
139             result = (result << 3) + (header[i] - '0');
140         }
141  
142         return result;
143     }
144 
145     
146     public static int getNameBytes( StringBuffer _name, byte[] buf, int offset, int length ) {
147         int       i;
148         byte[] _buf = _name.toString().getBytes();
149 
150         for ( i = 0; i < length && i < _buf.length; i++ ) buf[ offset + i ] = _buf[i];
151         for ( ; i < length; i++ ) buf[ offset + i ] = 0;
152 
153         return offset + length;
154     } 
155 
156 
157     public static int getOctalBytes( long value, byte[] buf, int offset, int length ) {
158         byte[] result = new byte[ length ];
159         int       idx = length - 1;
160  
161         buf[ offset + idx ] = 0; --idx;
162         buf[ offset + idx ] = (byte)' '; --idx;
163         if ( value == 0 ) {
164             buf[ offset + idx ] = (byte)'0'; --idx;
165         }
166         else {
167             for ( long val = value ; idx >= 0 && val > 0 ; --idx ) {
168                 buf[ offset + idx ] = (byte)( (byte) '0' + (byte) (val & 7) );
169                 val = val >> 3;
170             }
171         }
172         for ( ; idx >= 0 ; --idx ) buf[ offset + idx ] = (byte) ' ';
173  
174         return offset + length;
175     }
176 
177     
178     public static int getLongOctalBytes( long value, byte[] buf, int offset, int length ) {
179         byte[] temp = new byte[ length + 1 ];
180         TarHeader.getOctalBytes( value, temp, 0, length + 1 );
181         System.arraycopy( temp, 0, buf, offset, length );
182         return offset + length;
183     }        
184 
185 
186     public static int getCheckSumOctalBytes( long value, byte[] buf, int offset, int length ) {
187         TarHeader.getOctalBytes( value, buf, offset, length );
188         buf[ offset + length - 1 ] = (byte) ' ';
189         buf[ offset + length - 2 ] = 0;
190         return offset + length;
191     }
192 }