@@ -216,7 +216,7 @@ public static int[] copyOf(int[] original, int newLength) {
216216 * Loads the model from inputReader.
217217 * It uses {@link java.util.Locale#ENGLISH} for number formatting.
218218 *
219- * <p><b> Note: The inputReader is closed after reading or in case of an exception.</b> </p>
219+ * <p>Note: The inputReader is <b>NOT closed</b> after reading or in case of an exception.</p>
220220 */
221221 public static Model loadModel (Reader inputReader ) throws IOException {
222222 Model model = new Model ();
@@ -232,65 +232,60 @@ public static Model loadModel(Reader inputReader) throws IOException {
232232 reader = new BufferedReader (inputReader );
233233 }
234234
235- try {
236- String line = null ;
237- while ((line = reader .readLine ()) != null ) {
238- String [] split = whitespace .split (line );
239- if (split [0 ].equals ("solver_type" )) {
240- SolverType solver = SolverType .valueOf (split [1 ]);
241- if (solver == null ) {
242- throw new RuntimeException ("unknown solver type" );
243- }
244- model .solverType = solver ;
245- } else if (split [0 ].equals ("nr_class" )) {
246- model .nr_class = atoi (split [1 ]);
247- Integer .parseInt (split [1 ]);
248- } else if (split [0 ].equals ("nr_feature" )) {
249- model .nr_feature = atoi (split [1 ]);
250- } else if (split [0 ].equals ("bias" )) {
251- model .bias = atof (split [1 ]);
252- } else if (split [0 ].equals ("w" )) {
253- break ;
254- } else if (split [0 ].equals ("label" )) {
255- model .label = new int [model .nr_class ];
256- for (int i = 0 ; i < model .nr_class ; i ++) {
257- model .label [i ] = atoi (split [i + 1 ]);
258- }
259- } else {
260- throw new RuntimeException ("unknown text in model file: [" + line + "]" );
235+ String line = null ;
236+ while ((line = reader .readLine ()) != null ) {
237+ String [] split = whitespace .split (line );
238+ if (split [0 ].equals ("solver_type" )) {
239+ SolverType solver = SolverType .valueOf (split [1 ]);
240+ if (solver == null ) {
241+ throw new RuntimeException ("unknown solver type" );
261242 }
243+ model .solverType = solver ;
244+ } else if (split [0 ].equals ("nr_class" )) {
245+ model .nr_class = atoi (split [1 ]);
246+ Integer .parseInt (split [1 ]);
247+ } else if (split [0 ].equals ("nr_feature" )) {
248+ model .nr_feature = atoi (split [1 ]);
249+ } else if (split [0 ].equals ("bias" )) {
250+ model .bias = atof (split [1 ]);
251+ } else if (split [0 ].equals ("w" )) {
252+ break ;
253+ } else if (split [0 ].equals ("label" )) {
254+ model .label = new int [model .nr_class ];
255+ for (int i = 0 ; i < model .nr_class ; i ++) {
256+ model .label [i ] = atoi (split [i + 1 ]);
257+ }
258+ } else {
259+ throw new RuntimeException ("unknown text in model file: [" + line + "]" );
262260 }
261+ }
263262
264- int w_size = model .nr_feature ;
265- if (model .bias >= 0 ) w_size ++;
263+ int w_size = model .nr_feature ;
264+ if (model .bias >= 0 ) w_size ++;
266265
267- int nr_w = model .nr_class ;
268- if (model .nr_class == 2 && model .solverType != SolverType .MCSVM_CS ) nr_w = 1 ;
266+ int nr_w = model .nr_class ;
267+ if (model .nr_class == 2 && model .solverType != SolverType .MCSVM_CS ) nr_w = 1 ;
269268
270- model .w = new double [w_size * nr_w ];
271- int [] buffer = new int [128 ];
269+ model .w = new double [w_size * nr_w ];
270+ int [] buffer = new int [128 ];
272271
273- for (int i = 0 ; i < w_size ; i ++) {
274- for (int j = 0 ; j < nr_w ; j ++) {
275- int b = 0 ;
276- while (true ) {
277- int ch = reader .read ();
278- if (ch == -1 ) {
279- throw new EOFException ("unexpected EOF" );
280- }
281- if (ch == ' ' ) {
282- model .w [i * nr_w + j ] = atof (new String (buffer , 0 , b ));
283- break ;
284- } else {
285- buffer [b ++] = ch ;
286- }
272+ for (int i = 0 ; i < w_size ; i ++) {
273+ for (int j = 0 ; j < nr_w ; j ++) {
274+ int b = 0 ;
275+ while (true ) {
276+ int ch = reader .read ();
277+ if (ch == -1 ) {
278+ throw new EOFException ("unexpected EOF" );
279+ }
280+ if (ch == ' ' ) {
281+ model .w [i * nr_w + j ] = atof (new String (buffer , 0 , b ));
282+ break ;
283+ } else {
284+ buffer [b ++] = ch ;
287285 }
288286 }
289287 }
290288 }
291- finally {
292- closeQuietly (reader );
293- }
294289
295290 return model ;
296291 }
@@ -301,7 +296,12 @@ public static Model loadModel(Reader inputReader) throws IOException {
301296 */
302297 public static Model loadModel (File modelFile ) throws IOException {
303298 BufferedReader inputReader = new BufferedReader (new InputStreamReader (new FileInputStream (modelFile ), FILE_CHARSET ));
304- return loadModel (inputReader );
299+ try {
300+ return loadModel (inputReader );
301+ }
302+ finally {
303+ inputReader .close ();
304+ }
305305 }
306306
307307 static void closeQuietly (Closeable c ) {
0 commit comments