import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; public class DiskWaste extends Panel implements ActionListener, ItemListener { // GUI elements protected TextArea reportScreen; protected Choice blockSizeChoice; protected Button resetButton; // statistics protected int numFiles = 0; protected long effectivelyUsed = 0; protected long allocatedUsed = 0; protected int blockSizes[] = {512, 1024, 2048, 4096, 8192, 16384, 32768, 65536}; protected int blockSize, blockMask; protected boolean newRun; //------------------------------------------------------------------- // DiskWaste constructor //------------------------------------------------------------------- public DiskWaste() { Label lbl = new Label("Disk Block Size:"); blockSizeChoice = buildChoiceFromIntList(blockSizes); blockSizeChoice.addItemListener(this); resetButton = new Button("Reset Stats"); resetButton.addActionListener(this); Panel controlPanel = new Panel(); controlPanel.add(lbl); controlPanel.add(blockSizeChoice); controlPanel.add(resetButton); reportScreen = new TextArea("", 10, 80, TextArea.SCROLLBARS_NONE); reportScreen.setSize(300,200); reportScreen.setFont(new Font("Courier", Font.PLAIN, 12)); setLayout(new BorderLayout()); add("Center", reportScreen); add("South", controlPanel); itemStateChanged(null); // force setting of block size & mask newRun = true; } //------------------------------------------------------------------- // Utility function to create a Choice from a list of integers //------------------------------------------------------------------- public static Choice buildChoiceFromIntList(int[] list) { Choice choice = new Choice(); for (int i=0; i < list.length; i++) { choice.add( String.valueOf(list[i]) ); } return choice; } //------------------------------------------------------------------- // Reset statistics //------------------------------------------------------------------- protected void resetStats() { numFiles = 0; effectivelyUsed = 0; allocatedUsed = 0; // wipe report screen reportScreen.setText(""); } //------------------------------------------------------------------- // ActionListener interface method //------------------------------------------------------------------- public void actionPerformed(ActionEvent actionEvent) { if ( actionEvent instanceof FilenameActionEvent ) { String filename = ((FilenameActionEvent) actionEvent).getFilename(); accumulateStats(filename); } else { Object source = actionEvent.getSource(); if ( source instanceof Button ) { if ( source == resetButton ) { resetStats(); } } } } //------------------------------------------------------------------- // Given a filename, locate file and calc real and imaginary resource usage //------------------------------------------------------------------- protected void accumulateStats(String filename) { File file; long bytes; if ( filename == null ) { produceDiskUsageReport(); newRun = true; return; } if ( newRun ) { resetStats(); reportScreen.setText("Scanning.. please wait."); newRun = false; } numFiles++; file = new File(filename); bytes = file.length(); // what is this file's length ? effectivelyUsed += bytes; // calculate how many bytes were actually allocated to store them bytes = (bytes + blockSize) & blockMask; allocatedUsed += bytes; } //------------------------------------------------------------------- // Using the accumulated statistics, produce a report of how much disk // space is wasted. //------------------------------------------------------------------- protected void produceDiskUsageReport() { StringBuffer report = new StringBuffer(); float wastage = 100.0F - 100.0F * (effectivelyUsed) / (float)(allocatedUsed); report.append("DiskWaste report:\n"); report.append("-----------------\n"); report.append("\n"); report.append("Total number of files : " + numFiles +"\n"); report.append("File storage requirement : " + effectivelyUsed +"\n"); report.append("Storage actually required : " + allocatedUsed +"\n"); report.append("Wastage : " + wastage + "%\n" ); report.append("\n"); if (wastage < 5.0F) { report.append("That's not bad at all."); } else if (wastage < 15.0F) { report.append("A bit of wastage there.."); } else if (wastage < 33.3F) { report.append("That's a lot of wasted hard disk space, don't you think?"); } else if (wastage < 50.0F) { report.append("Wow! More than a third of space totally wasted."); } else if (wastage < 75.0F) { report.append("Incredible! Over half of your disk is being wasted!"); } else { report.append( "Hey man, you've got a problem here. More than three quarters of your disk is being wasted."); } report.append("\n"); reportScreen.setText(report.toString()); } //------------------------------------------------------------------- // ItemListener interface method // This is where new disk block size selections arrive. Record selection. //------------------------------------------------------------------- public void itemStateChanged(ItemEvent itemEvent) { int selectedSize = blockSizeChoice.getSelectedIndex(); blockSize = blockSizes[ selectedSize ]; // calculate filesize rounding function (using AND) blockMask = ~(blockSize-1); } //------------------------------------------------------------------- // Self-Test code //------------------------------------------------------------------- public static void main (String[] args) { Frame window = new Frame("DiskWaste"); DirLister dirLister = new DirLister(); DiskWaste diskWaste = new DiskWaste(); window.add("Center", diskWaste); window.add("South", dirLister); dirLister.addActionListener(diskWaste); window.pack(); window.setVisible(true); } } // End of Class DiskWaste