1 11 package org.eclipse.core.runtime.internal.stats; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.net.URL ; 16 import java.util.*; 17 18 22 23 public class ResourceBundleStats { 24 private String pluginId; private String fileName; private int keyCount = 0; private int keySize = 0; private int valueSize = 0; private long hashSize = 0; private long fileSize = 0; 31 32 private static int sizeOf(String value) { 33 return 44 + (2 * value.length()); 34 } 35 36 private static int sizeOf(Properties value) { 37 return (int) Math.round(44 + (16 + (value.size() * 1.25 * 4)) + (24 * value.size())); 38 } 39 40 public ResourceBundleStats(String pluginId, String fileName, URL input) { 41 this.pluginId = pluginId; 42 this.fileName = fileName; 43 initialize(input); 44 } 45 46 public ResourceBundleStats(String pluginId, String fileName, ResourceBundle bundle) { 47 this.pluginId = pluginId; 48 this.fileName = fileName; 49 initialize(bundle); 50 } 51 52 55 private void initialize(ResourceBundle bundle) { 56 for (Enumeration keys = bundle.getKeys(); keys.hasMoreElements();) { 57 String key = (String ) keys.nextElement(); 58 keySize += sizeOf(key); 59 valueSize += sizeOf(bundle.getString(key)); 60 keyCount++; 61 } 62 } 63 64 67 private void initialize(URL url) { 68 InputStream stream = null; 69 Properties props = new Properties(); 70 try { 71 try { 72 stream = url.openStream(); 73 fileSize = stream.available(); 74 props.load(stream); 75 for (Iterator iter = props.keySet().iterator(); iter.hasNext();) { 76 String key = (String ) iter.next(); 77 keySize += sizeOf(key); 78 valueSize += sizeOf(props.getProperty(key)); 79 keyCount++; 80 } 81 hashSize = sizeOf(props); 82 } finally { 83 if (stream != null) 84 stream.close(); 85 } 86 } catch (IOException e) { 87 } 90 } 91 92 public long getHashSize() { 93 return hashSize; 94 } 95 96 public int getKeyCount() { 97 return keyCount; 98 } 99 100 public String getPluginId() { 101 return pluginId; 102 } 103 104 public int getKeySize() { 105 return keySize; 106 } 107 108 public int getValueSize() { 109 return valueSize; 110 } 111 112 public long getTotalSize() { 113 return keySize + valueSize + hashSize; 114 } 115 116 public String getFileName() { 117 return fileName; 118 } 119 120 public long getFileSize() { 121 return fileSize; 122 } 123 } 124 | Popular Tags |