KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > framework > stats > ResourceBundleStats


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.osgi.framework.stats;
12
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.net.URL;
16 import java.util.*;
17
18 /**
19  * BundleStats is used to represent information about loaded bundle. A
20  * bundlestats instance represents only one bundle.
21  */

22
23 public class ResourceBundleStats {
24     private String pluginId; // the plugin loading this bundle
25
private String fileName; // the filename of the bundle
26
private int keyCount = 0; // number of keys in the bundle
27
private int keySize = 0; // size of the keys in the bundle
28
private int valueSize = 0; // size of the values in the bundle
29
private long hashSize = 0; // size of the hashtable
30
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     /**
53      * Compute the size of bundle
54      */

55     private void initialize(ResourceBundle bundle) {
56         for (Enumeration enum = bundle.getKeys(); enum.hasMoreElements();) {
57             String key = (String) enum.nextElement();
58             keySize += sizeOf(key);
59             valueSize += sizeOf(bundle.getString(key));
60             keyCount++;
61         }
62     }
63
64     /**
65      * Compute the size of stream which represents a property file
66      */

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             // ignore exceptions as they will be handled when the stream
88
// is loaded for real. See callers.
89
}
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 }
Popular Tags