1 15 16 package installer; 17 18 22 23 public class 24 TarHeader extends Object 25 { 26 29 public static final int NAMELEN = 100; 30 33 public static final int MODELEN = 8; 34 37 public static final int UIDLEN = 8; 38 41 public static final int GIDLEN = 8; 42 45 public static final int CHKSUMLEN = 8; 46 49 public static final int SIZELEN = 12; 50 53 public static final int MAGICLEN = 8; 54 57 public static final int MODTIMELEN = 12; 58 61 public static final int UNAMELEN = 32; 62 65 public static final int GNAMELEN = 32; 66 69 public static final int DEVLEN = 8; 70 71 75 public static final byte LF_OLDNORM = 0; 76 79 public static final byte LF_NORMAL = (byte) '0'; 80 83 public static final byte LF_LINK = (byte) '1'; 84 87 public static final byte LF_SYMLINK = (byte) '2'; 88 91 public static final byte LF_CHR = (byte) '3'; 92 95 public static final byte LF_BLK = (byte) '4'; 96 99 public static final byte LF_DIR = (byte) '5'; 100 103 public static final byte LF_FIFO = (byte) '6'; 104 107 public static final byte LF_CONTIG = (byte) '7'; 108 109 112 public static final String TMAGIC = "ustar"; 113 114 117 public static final String GNU_TMAGIC = "ustar "; 118 119 122 public StringBuffer name; 123 126 public int mode; 127 130 public int userId; 131 134 public int groupId; 135 138 public long size; 139 142 public long modTime; 143 146 public int checkSum; 147 150 public byte linkFlag; 151 154 public StringBuffer linkName; 155 158 public StringBuffer magic; 159 162 public StringBuffer userName; 163 166 public StringBuffer groupName; 167 170 public int devMajor; 171 174 public int devMinor; 175 176 177 public 178 TarHeader() 179 { 180 this.magic = new StringBuffer ( TarHeader.TMAGIC ); 181 182 this.name = new StringBuffer (); 183 this.linkName = new StringBuffer (); 184 185 String user = 186 System.getProperty( "user.name", "" ); 187 188 if ( user.length() > 31 ) 189 user = user.substring( 0, 31 ); 190 191 this.userId = 0; 192 this.groupId = 0; 193 this.userName = new StringBuffer ( user ); 194 this.groupName = new StringBuffer ( "" ); 195 } 196 197 200 public Object 201 clone() 202 { 203 TarHeader hdr = null; 204 205 try { 206 hdr = (TarHeader) super.clone(); 207 208 hdr.name = 209 (this.name == null ) ? null 210 : new StringBuffer ( this.name.toString() ); 211 hdr.mode = this.mode; 212 hdr.userId = this.userId; 213 hdr.groupId = this.groupId; 214 hdr.size = this.size; 215 hdr.modTime = this.modTime; 216 hdr.checkSum = this.checkSum; 217 hdr.linkFlag = this.linkFlag; 218 hdr.linkName = 219 (this.linkName == null ) ? null 220 : new StringBuffer ( this.linkName.toString() ); 221 hdr.magic = 222 (this.magic == null ) ? null 223 : new StringBuffer ( this.magic.toString() ); 224 hdr.userName = 225 (this.userName == null ) ? null 226 : new StringBuffer ( this.userName.toString() ); 227 hdr.groupName = 228 (this.groupName == null ) ? null 229 : new StringBuffer ( this.groupName.toString() ); 230 hdr.devMajor = this.devMajor; 231 hdr.devMinor = this.devMinor; 232 } 233 catch ( CloneNotSupportedException ex ) 234 { 235 ex.printStackTrace(); 236 } 237 238 return hdr; 239 } 240 241 246 public String 247 getName() 248 { 249 return this.name.toString(); 250 } 251 252 261 public static long 262 parseOctal( byte[] header, int offset, int length ) 263 throws InvalidHeaderException 264 { 265 long result = 0; 266 boolean stillPadding = true; 267 268 int end = offset + length; 269 for ( int i = offset ; i < end ; ++i ) 270 { 271 if ( header[i] == 0 ) 272 break; 273 274 if ( header[i] == (byte) ' ' || header[i] == '0' ) 275 { 276 if ( stillPadding ) 277 continue; 278 279 if ( header[i] == (byte) ' ' ) 280 break; 281 } 282 283 stillPadding = false; 284 285 result = 286 (result << 3) 287 + (header[i] - '0'); 288 } 289 290 return result; 291 } 292 293 301 public static StringBuffer 302 parseName( byte[] header, int offset, int length ) 303 throws InvalidHeaderException 304 { 305 StringBuffer result = new StringBuffer ( length ); 306 307 int end = offset + length; 308 for ( int i = offset ; i < end ; ++i ) 309 { 310 if ( header[i] == 0 ) 311 break; 312 result.append( (char)header[i] ); 313 } 314 315 return result; 316 } 317 318 326 public static int 327 getNameBytes( StringBuffer name, byte[] buf, int offset, int length ) 328 { 329 int i; 330 331 for ( i = 0 ; i < length && i < name.length() ; ++i ) 332 { 333 buf[ offset + i ] = (byte) name.charAt( i ); 334 } 335 336 for ( ; i < length ; ++i ) 337 { 338 buf[ offset + i ] = 0; 339 } 340 341 return offset + length; 342 } 343 344 352 public static int 353 getOctalBytes( long value, byte[] buf, int offset, int length ) 354 { 355 byte[] result = new byte[ length ]; 356 357 int idx = length - 1; 358 359 buf[ offset + idx ] = 0; 360 --idx; 361 buf[ offset + idx ] = (byte) ' '; 362 --idx; 363 364 if ( value == 0 ) 365 { 366 buf[ offset + idx ] = (byte) '0'; 367 --idx; 368 } 369 else 370 { 371 for ( long val = value ; idx >= 0 && val > 0 ; --idx ) 372 { 373 buf[ offset + idx ] = (byte) 374 ( (byte) '0' + (byte) (val & 7) ); 375 val = val >> 3; 376 } 377 } 378 379 for ( ; idx >= 0 ; --idx ) 380 { 381 buf[ offset + idx ] = (byte) ' '; 382 } 383 384 return offset + length; 385 } 386 387 395 public static int 396 getLongOctalBytes( long value, byte[] buf, int offset, int length ) 397 { 398 byte[] temp = new byte[ length + 1 ]; 399 TarHeader.getOctalBytes( value, temp, 0, length + 1 ); 400 System.arraycopy( temp, 0, buf, offset, length ); 401 return offset + length; 402 } 403 404 412 public static int 413 getCheckSumOctalBytes( long value, byte[] buf, int offset, int length ) 414 { 415 TarHeader.getOctalBytes( value, buf, offset, length ); 416 buf[ offset + length - 1 ] = (byte) ' '; 417 buf[ offset + length - 2 ] = 0; 418 return offset + length; 419 } 420 421 } 422 423 | Popular Tags |