Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/gnu/readline/ReadlineReader.java


1   package org.gnu.readline;
2   
3   import java.io.File;
4   import java.io.Reader;
5   import java.io.IOException;
6   import java.io.EOFException;
7   import java.io.UnsupportedEncodingException;
8   
9   /**
10   * A <code>Reader</code> wrapper for the Readline classes.  This seems
11   * to work fine in conjunction with such classes as BufferedReader,
12   * but it hasn't been tested well enough to see if this will work well
13   * in all cases.
14   *
15   * This was implemented to make it easier to supplant Readline's
16   * functionality [shrug] anywhere and everywhere, but specifically in
17   * <a href="http://www.beanshell.org">BeanShell</a>.
18   *
19   * @version $Revision: 1.2 $
20   * @author Shane Celis <shane@terrapsring.com>
21   **/
22  
23  public class ReadlineReader extends Reader {
24  
25    public static final String DEFAULT_PROMPT = "";
26    private StringBuffer iBuff;  
27    private String       iLineSeparator;
28    private String iPrompt;
29    private File iHistoryFile;
30    
31    /**
32     * Constructs a ReadlineReader object with the given prompt.
33     **/
34    
35    public ReadlineReader(String prompt,ReadlineLibrary lib) { 
36      iBuff = new StringBuffer();
37      setPrompt(prompt);
38      Readline.load(lib);
39      Readline.initReadline("ReadlineReader");
40      iLineSeparator = System.getProperty("line.separator", "\n");
41    }
42  
43    /**
44     * Constructs a ReadlineReader object with the default prompt.
45     **/
46  
47    public ReadlineReader(ReadlineLibrary lib) {
48      this(DEFAULT_PROMPT,lib);
49    }
50  
51    /**
52     * Constructs a ReadlineReader object with an associated history
53     * file.
54     **/
55  
56    public ReadlineReader(File history,ReadlineLibrary lib) throws IOException {
57      this(DEFAULT_PROMPT,lib);
58      Readline.readHistoryFile(history.getAbsolutePath());
59      iHistoryFile = history; // only set this if we can read the file
60    }
61  
62    /**
63     * Constructs a ReadlineReader object with an associated history
64     * file and prompt.
65     **/
66  
67    public ReadlineReader(String prompt, File history,ReadlineLibrary lib)
68                                                           throws IOException {
69      this(history,lib);
70      setPrompt(prompt);
71    }
72  
73    /**
74     * Returns the current prompt.
75     **/
76  
77    public String getPrompt() {
78      return iPrompt;
79    }
80      
81    /**
82     * Sets the prompt to the given value.
83     **/
84  
85    public void setPrompt(String prompt) {
86      iPrompt = prompt;
87    }
88  
89    /**
90     * Reads what's given from <code>readline()</code> into a buffer.
91     * When that buffer is emptied, <code>readline()</code> is called
92     * again to replenish that buffer.  This seems to work fine in
93     * conjunction with such classes as BufferedReader, but it hasn't
94     * been tested well enough to see if this will work well in all
95     * cases.
96     **/
97  
98    public int read(char[] cbuf, int off, int len) 
99      throws IOException {
100     try {
101       if (iBuff.length() == 0) {
102   String line = Readline.readline(iPrompt);
103   iBuff.append((line == null ? "" : line) + iLineSeparator);
104       }
105       if (len > iBuff.length())
106   len = iBuff.length();
107       if (len == 0) 
108   return 0;
109       char[] sbuf = iBuff.substring(0, len).toCharArray();
110       System.arraycopy(sbuf, 0, cbuf, off, len);
111       iBuff.delete(0, len);
112       return len;
113     } catch (EOFException eof) {
114       throw eof;
115     } catch (UnsupportedEncodingException uee) {
116       throw uee;
117     }
118   }
119 
120   /**
121    * Nullifies all buffers and writes history file if one was given
122    * at construction time.
123    **/
124 
125   public void close()
126     throws IOException {
127     iBuff = null;
128     iPrompt = null;
129     if (iHistoryFile != null) {
130       Readline.writeHistoryFile(iHistoryFile.getAbsolutePath());
131       iHistoryFile = null;
132     }
133   }
134 
135   public static void main(String[] args) throws Exception {
136     java.io.BufferedReader rd = 
137       new java.io.BufferedReader(new 
138     ReadlineReader("hmm ", new File("test"),ReadlineLibrary.GnuReadline));
139     String line;
140     try {
141       while ((line = rd.readLine()) != null) {
142   System.out.println("got: " + line);
143       }
144     } finally {
145       rd.close();
146     }
147   }
148 }
149