1 package de.masters_of_disaster.ant.tasks.ar; 2 3 import java.io.File ; 4 import java.util.Date ; 5 6 47 48 public class ArEntry implements ArConstants { 49 50 private StringBuffer filename; 51 52 53 private long fileDate; 54 55 56 private int userId; 57 58 59 private int groupId; 60 61 62 private int mode; 63 64 65 private long size; 66 67 68 private StringBuffer magic; 69 70 71 private File file; 72 73 74 public static final int DEFAULT_FILE_MODE = 0100644; 75 76 77 public static final int MILLIS_PER_SECOND = 1000; 78 79 82 private ArEntry () { 83 this.magic = new StringBuffer (HEADERMAGIC); 84 this.filename = new StringBuffer (); 85 this.userId = 0; 86 this.groupId = 0; 87 this.file = null; 88 } 89 90 96 public ArEntry(String name) { 97 this(); 98 if (name.endsWith("/")) { 99 throw new IllegalArgumentException ("ar archives can only contain files"); 100 } 101 this.filename = new StringBuffer (name); 102 this.mode = DEFAULT_FILE_MODE; 103 this.userId = 0; 104 this.groupId = 0; 105 this.size = 0; 106 this.fileDate = (new Date ()).getTime() / MILLIS_PER_SECOND; 107 } 108 109 115 public ArEntry(File file) { 116 this(); 117 if (file.isDirectory()) { 118 throw new IllegalArgumentException ("ar archives can only contain files"); 119 } 120 this.file = file; 121 this.filename = new StringBuffer (file.getName()); 122 this.fileDate = file.lastModified() / MILLIS_PER_SECOND; 123 this.mode = DEFAULT_FILE_MODE; 124 this.size = file.length(); 125 } 126 127 133 public ArEntry(byte[] headerBuf) { 134 this(); 135 this.parseArHeader(headerBuf); 136 } 137 138 145 public boolean equals(ArEntry it) { 146 return this.getFilename().equals(it.getFilename()); 147 } 148 149 156 public boolean equals(Object it) { 157 if (it == null || getClass() != it.getClass()) { 158 return false; 159 } 160 return equals((ArEntry) it); 161 } 162 163 168 public int hashCode() { 169 return getFilename().hashCode(); 170 } 171 172 177 public String getFilename() { 178 return this.filename.toString(); 179 } 180 181 186 public void setFilename(String filename) { 187 this.filename = new StringBuffer (filename); 188 } 189 190 195 public void setMode(int mode) { 196 this.mode = mode; 197 } 198 199 204 public int getUserId() { 205 return this.userId; 206 } 207 208 213 public void setUserId(int userId) { 214 this.userId = userId; 215 } 216 217 222 public int getGroupId() { 223 return this.groupId; 224 } 225 226 231 public void setGroupId(int groupId) { 232 this.groupId = groupId; 233 } 234 235 241 public void setIds(int userId, int groupId) { 242 this.setUserId(userId); 243 this.setGroupId(groupId); 244 } 245 246 252 public void setFileDate(long time) { 253 this.fileDate = time / MILLIS_PER_SECOND; 254 } 255 256 261 public void setFileDate(Date time) { 262 this.fileDate = time.getTime() / MILLIS_PER_SECOND; 263 } 264 265 270 public Date getFileDate() { 271 return new Date (this.fileDate * MILLIS_PER_SECOND); 272 } 273 274 279 public File getFile() { 280 return this.file; 281 } 282 283 288 public int getMode() { 289 return this.mode; 290 } 291 292 297 public long getSize() { 298 return this.size; 299 } 300 301 306 public void setSize(long size) { 307 this.size = size; 308 } 309 310 315 public void writeEntryHeader(byte[] outbuf) { 316 int offset = 0; 317 318 offset = ArUtils.getNameBytes(this.filename, outbuf, offset, NAMELEN); 319 offset = ArUtils.getLongBytes(this.fileDate, outbuf, offset, FILEDATELEN); 320 offset = ArUtils.getIntegerBytes(this.userId, outbuf, offset, UIDLEN); 321 offset = ArUtils.getIntegerBytes(this.groupId, outbuf, offset, GIDLEN); 322 offset = ArUtils.getOctalBytes(this.mode, outbuf, offset, MODELEN); 323 offset = ArUtils.getLongBytes(this.size, outbuf, offset, SIZELEN); 324 offset = ArUtils.getNameBytes(this.magic, outbuf, offset, MAGICLEN); 325 326 while (offset < outbuf.length) { 327 outbuf[offset++] = 0; 328 } 329 } 330 331 336 public void parseArHeader(byte[] header) { 337 throw new UnsupportedOperationException ("parseArHeader(byte[]) not yet implmented"); 338 } 355 } 356 | Popular Tags |