KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > masters_of_disaster > ant > tasks > calculatesize > CalculateSize


1 package de.masters_of_disaster.ant.tasks.calculatesize;
2
3 import java.io.File JavaDoc;
4 import java.util.Enumeration JavaDoc;
5 import java.util.Vector JavaDoc;
6 import org.apache.tools.ant.BuildException;
7 import org.apache.tools.ant.taskdefs.MatchingTask;
8 import org.apache.tools.ant.types.FileSet;
9
10 /**
11  * Calculates the "Installed-Size" of a deb package for the "control"-file.
12  *
13  * @ant.task category="packaging"
14  */

15 public class CalculateSize extends MatchingTask {
16     String JavaDoc realSizeProperty = null;
17     String JavaDoc diskSizeProperty = null;
18     Vector JavaDoc fileSets = new Vector JavaDoc();
19     File JavaDoc baseDir;
20
21     /**
22      * Add a new fileset
23      *
24      * @return the fileset to be used as the nested element.
25      */

26     public FileSet createFileSet() {
27         FileSet fileSet = new FileSet();
28         fileSets.addElement(fileSet);
29         return fileSet;
30     }
31
32     /**
33      * This is the base directory to look in for things to include.
34      *
35      * @param baseDir the base directory.
36      */

37     public void setBaseDir(File JavaDoc baseDir) {
38         this.baseDir = baseDir;
39         fileset.setDir(baseDir);
40     }
41
42     /**
43      * This is the property to set to the real size.
44      *
45      * @param realSizeProperty The property to set to the real size
46      */

47     public void setRealSizeProperty(String JavaDoc realSizeProperty) {
48         this.realSizeProperty = realSizeProperty;
49     }
50
51     /**
52      * This is the property to set to the disk size.
53      *
54      * @param diskSizeProperty The property to set to the disk size
55      */

56     public void setDiskSizeProperty(String JavaDoc diskSizeProperty) {
57         this.diskSizeProperty = diskSizeProperty;
58     }
59
60     /**
61      * do the business
62      *
63      * @throws BuildException on error
64      */

65     public void execute() throws BuildException {
66         if ((null == realSizeProperty) && (null == diskSizeProperty)) {
67             throw new BuildException("realSizeProperty or diskSizeProperty must be set for <CalculateSize>");
68         }
69
70         if (null != baseDir) {
71             // add the main fileset to the list of filesets to process.
72
fileSets.addElement(fileset);
73         }
74
75         long realSize = 0;
76         long diskSize = 0;
77         for (Enumeration JavaDoc e=fileSets.elements() ; e.hasMoreElements() ; ) {
78             FileSet fileSet = (FileSet)e.nextElement();
79             String JavaDoc[] files = fileSet.getDirectoryScanner(getProject()).getIncludedFiles();
80             File JavaDoc fileSetDir = fileSet.getDir(getProject());
81             for (int i=0, c=files.length ; i<c ; i++) {
82                 long fileLength = new File JavaDoc(fileSetDir,files[i]).length();
83                 realSize += fileLength / 1024;
84                 diskSize += (fileLength / 4096 + 1) * 4;
85             }
86         }
87         if (null != realSizeProperty) {
88             getProject().setNewProperty(realSizeProperty,Long.toString(realSize));
89         }
90         if (null != diskSizeProperty) {
91             getProject().setNewProperty(diskSizeProperty,Long.toString(diskSize));
92         }
93     }
94 }
95
Popular Tags