1
1
/**
2
- * Copyright (C) 2008 Happy Fish / YuQing
3
- *
4
- * FastDFS Java Client may be copied only under the terms of the GNU Lesser
5
- * General Public License (LGPL).
6
- * Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
7
- **/
2
+ * Copyright (C) 2008 Happy Fish / YuQing
3
+ * <p>
4
+ * FastDFS Java Client may be copied only under the terms of the GNU Lesser
5
+ * General Public License (LGPL).
6
+ * Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
7
+ **/
8
8
9
9
package org .csource .common ;
10
10
11
11
import java .io .*;
12
- import java .util .* ;
13
- import org . csource . common .* ;
12
+ import java .util .ArrayList ;
13
+ import java . util . Hashtable ;
14
14
15
15
/**
16
- * ini file reader / parser
17
- * @author Happy Fish / YuQing
18
- * @version Version 1.0
19
- */
20
- public class IniFileReader
21
- {
22
- private Hashtable paramTable ;
23
- private String conf_filename ;
16
+ * ini file reader / parser
17
+ *
18
+ * @author Happy Fish / YuQing
19
+ * @version Version 1.0
20
+ */
21
+ public class IniFileReader {
22
+ private Hashtable paramTable ;
23
+ private String conf_filename ;
24
+
25
+ /**
26
+ * @param conf_filename config filename
27
+ */
28
+ public IniFileReader (String conf_filename ) throws FileNotFoundException , IOException {
29
+ this .conf_filename = conf_filename ;
30
+ loadFromFile (conf_filename );
31
+ }
24
32
25
- /**
26
- * @param conf_filename config filename
27
- */
28
- public IniFileReader (String conf_filename ) throws FileNotFoundException , IOException
29
- {
30
- this .conf_filename = conf_filename ;
31
- loadFromFile (conf_filename );
32
- }
33
+ private static ClassLoader classLoader () {
34
+ ClassLoader loader = Thread .currentThread ().getContextClassLoader ();
35
+ if (loader == null ) {
36
+ loader = ClassLoader .getSystemClassLoader ();
37
+ }
38
+ return loader ;
39
+ }
33
40
34
- /**
35
- * get the config filename
36
- * @return config filename
37
- */
38
- public String getConfFilename ()
39
- {
40
- return this .conf_filename ;
41
- }
41
+ /**
42
+ * get the config filename
43
+ *
44
+ * @return config filename
45
+ */
46
+ public String getConfFilename () {
47
+ return this .conf_filename ;
48
+ }
42
49
43
- /**
44
- * get string value from config file
45
- * @param name item name in config file
46
- * @return string value
47
- */
48
- public String getStrValue (String name )
49
- {
50
- Object obj ;
51
- obj = this .paramTable .get (name );
52
- if (obj == null )
53
- {
54
- return null ;
55
- }
56
-
57
- if (obj instanceof String )
58
- {
59
- return (String )obj ;
60
- }
61
-
62
- return (String )((ArrayList )obj ).get (0 );
63
- }
50
+ /**
51
+ * get string value from config file
52
+ *
53
+ * @param name item name in config file
54
+ * @return string value
55
+ */
56
+ public String getStrValue (String name ) {
57
+ Object obj ;
58
+ obj = this .paramTable .get (name );
59
+ if (obj == null ) {
60
+ return null ;
61
+ }
64
62
65
- /**
66
- * get int value from config file
67
- * @param name item name in config file
68
- * @param default_value the default value
69
- * @return int value
70
- */
71
- public int getIntValue (String name , int default_value )
72
- {
73
- String szValue = this .getStrValue (name );
74
- if (szValue == null )
75
- {
76
- return default_value ;
77
- }
78
-
79
- return Integer .parseInt (szValue );
80
- }
63
+ if (obj instanceof String ) {
64
+ return (String ) obj ;
65
+ }
81
66
82
- /**
83
- * get boolean value from config file
84
- * @param name item name in config file
85
- * @param default_value the default value
86
- * @return boolean value
87
- */
88
- public boolean getBoolValue (String name , boolean default_value )
89
- {
90
- String szValue = this .getStrValue (name );
91
- if (szValue == null )
92
- {
93
- return default_value ;
94
- }
95
-
96
- return szValue .equalsIgnoreCase ("yes" ) || szValue .equalsIgnoreCase ("on" ) ||
97
- szValue .equalsIgnoreCase ("true" ) || szValue .equals ("1" );
98
- }
67
+ return (String ) ((ArrayList ) obj ).get (0 );
68
+ }
99
69
100
- /**
101
- * get all values from config file
102
- * @param name item name in config file
103
- * @return string values (array)
104
- */
105
- public String [] getValues (String name )
106
- {
107
- Object obj ;
108
- String [] values ;
109
-
110
- obj = this .paramTable .get (name );
111
- if (obj == null )
112
- {
113
- return null ;
114
- }
115
-
116
- if (obj instanceof String )
117
- {
118
- values = new String [1 ];
119
- values [0 ] = (String )obj ;
120
- return values ;
121
- }
122
-
123
- Object [] objs = ((ArrayList )obj ).toArray ();
124
- values = new String [objs .length ];
125
- System .arraycopy (objs , 0 , values , 0 , objs .length );
126
- return values ;
127
- }
70
+ /**
71
+ * get int value from config file
72
+ *
73
+ * @param name item name in config file
74
+ * @param default_value the default value
75
+ * @return int value
76
+ */
77
+ public int getIntValue (String name , int default_value ) {
78
+ String szValue = this .getStrValue (name );
79
+ if (szValue == null ) {
80
+ return default_value ;
81
+ }
82
+
83
+ return Integer .parseInt (szValue );
84
+ }
85
+
86
+ /**
87
+ * get boolean value from config file
88
+ *
89
+ * @param name item name in config file
90
+ * @param default_value the default value
91
+ * @return boolean value
92
+ */
93
+ public boolean getBoolValue (String name , boolean default_value ) {
94
+ String szValue = this .getStrValue (name );
95
+ if (szValue == null ) {
96
+ return default_value ;
97
+ }
98
+
99
+ return szValue .equalsIgnoreCase ("yes" ) || szValue .equalsIgnoreCase ("on" ) ||
100
+ szValue .equalsIgnoreCase ("true" ) || szValue .equals ("1" );
101
+ }
102
+
103
+ /**
104
+ * get all values from config file
105
+ *
106
+ * @param name item name in config file
107
+ * @return string values (array)
108
+ */
109
+ public String [] getValues (String name ) {
110
+ Object obj ;
111
+ String [] values ;
112
+
113
+ obj = this .paramTable .get (name );
114
+ if (obj == null ) {
115
+ return null ;
116
+ }
117
+
118
+ if (obj instanceof String ) {
119
+ values = new String [1 ];
120
+ values [0 ] = (String ) obj ;
121
+ return values ;
122
+ }
123
+
124
+ Object [] objs = ((ArrayList ) obj ).toArray ();
125
+ values = new String [objs .length ];
126
+ System .arraycopy (objs , 0 , values , 0 , objs .length );
127
+ return values ;
128
+ }
128
129
129
130
private void loadFromFile (String confFilePath ) throws IOException {
130
131
InputStream in = null ;
@@ -144,7 +145,7 @@ private void loadFromFile(String confFilePath) throws IOException {
144
145
ex .printStackTrace ();
145
146
} finally {
146
147
try {
147
- if (in != null ) in .close ();
148
+ if (in != null ) in .close ();
148
149
//System.out.println("loadFrom...finally...in.close(); done");
149
150
} catch (Exception ex ) {
150
151
ex .printStackTrace ();
@@ -193,21 +194,13 @@ private void readToParamTable(InputStream in) throws IOException {
193
194
ex .printStackTrace ();
194
195
} finally {
195
196
try {
196
- if (bufferedReader != null ) bufferedReader .close ();
197
- if (inReader != null ) inReader .close ();
197
+ if (bufferedReader != null ) bufferedReader .close ();
198
+ if (inReader != null ) inReader .close ();
198
199
//System.out.println("readToParamTable...finally...bufferedReader.close();inReader.close(); done");
199
200
} catch (Exception ex ) {
200
201
ex .printStackTrace ();
201
202
}
202
203
}
203
204
}
204
205
205
- private static ClassLoader classLoader () {
206
- ClassLoader loader = Thread .currentThread ().getContextClassLoader ();
207
- if (loader == null ) {
208
- loader = ClassLoader .getSystemClassLoader ();
209
- }
210
- return loader ;
211
- }
212
-
213
206
}
0 commit comments