View Javadoc
1   package org.cyclopsgroup.jmxterm.io;
2   
3   import org.apache.commons.lang3.Validate;
4   
5   import java.io.File;
6   import java.io.FileNotFoundException;
7   import java.io.FileReader;
8   import java.io.IOException;
9   import java.io.LineNumberReader;
10  
11  /**
12   * Implementation of CommandInput with given File
13   * 
14   * @author <a href="mailto:jiaqi.guo@gmail.com">Jiaqi Guo</a>
15   */
16  public class FileCommandInput extends CommandInput {
17    private final LineNumberReader in;
18  
19    /**
20     * Read input from a given file
21     * 
22     * @param inputFile Given input file
23     * @throws FileNotFoundException Thrown when file doesn't exist
24     */
25    public FileCommandInput(File inputFile) throws FileNotFoundException {
26      Validate.notNull(inputFile, "Input can't be NULL");
27      this.in = new LineNumberReader(new FileReader(inputFile));
28    }
29  
30    @Override
31    public void close() throws IOException {
32      in.close();
33    }
34  
35    @Override
36    public String readLine() throws IOException {
37      return in.readLine();
38    }
39  
40    @Override
41    public String readMaskedString(String prompt) throws IOException {
42      throw new UnsupportedOperationException("Reading password from a file is not supported");
43    }
44  }