1 
2 /*************************************************************************
3  *  Compilation:  javac In.java
4  *  Execution:    java In
5  *
6  *  Reads in data of various types from: stdin, file, URL.
7  *
8  *  Reads in a string, rest of line, or rest of file and returns
9  *  it as a String or null if there is no more data.
10  *
11  *  The client can use Integer.parseInt(in.readString()) to read
12  *  in an int.
13  *
14  *  Typically this class is used with another client, but main()
15  *  provides a testing routine.
16  *
17  *  % java In
18  *  Test if In.java works.
19  *  Second line.
20  *
21  *  Test
22  *  if
23  *  In.java
24  *  works.
25  *  Second
26  *  line.
27  *
28  *  Test if In.java works.
29  *  Second line.
30  *
31  *************************************************************************/
32 
33 
34 import java.net.URLConnection;
35 import java.net.URL;
36 import java.net.Socket;
37 import java.io.File;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.io.InputStreamReader;
41 import java.io.BufferedReader;
42 import java.io.FileReader;
43 
44 
45 class In {
46    private BufferedReader br;
47 
48    // system independent
49    private final static String NEWLINE = System.getProperty("line.separator");
50 
51    // for stdin
In()52    public In() {
53       InputStreamReader isr = new InputStreamReader(System.in);
54       br = new BufferedReader(isr);
55    }
56 
57    // for stdin
In(Socket socket)58    public In(Socket socket) {
59       try {
60          InputStream is        = socket.getInputStream();
61          InputStreamReader isr = new InputStreamReader(is);
62          br                    = new BufferedReader(isr);
63       } catch (IOException ioe) { ioe.printStackTrace(); }
64    }
65 
66    // for URLs
In(URL url)67    public In(URL url) {
68       try {
69          URLConnection site    = url.openConnection();
70          InputStream is        = site.getInputStream();
71          InputStreamReader isr = new InputStreamReader(is);
72          br                    = new BufferedReader(isr);
73       } catch (IOException ioe) { ioe.printStackTrace(); }
74    }
75 
76    // for files and web pages
In(String s)77    public In(String s) {
78 
79       try {
80 
81          // first try to read file from local file system
82          File file = new File(s);
83          if (file.exists()) {
84              FileReader fr = new FileReader(s);
85              br = new BufferedReader(fr);
86          }
87 
88          // next try for files included in jar
89          URL url = getClass().getResource(s);
90 
91          // or URL from web
92          if (url == null) url = new URL(s);
93 
94          URLConnection site    = url.openConnection();
95          InputStream is        = site.getInputStream();
96          InputStreamReader isr = new InputStreamReader(is);
97          br = new BufferedReader(isr);
98       } catch(IOException ioe) {  }
99    }
100 
101 
102    // note read() returns -1 if EOF
readChar()103    private int readChar() {
104       int c = -1;
105       try { c = br.read(); }
106       catch(IOException ioe) { ioe.printStackTrace(); }
107       return c;
108    }
109 
110    // read a token - delete preceding whitespace and one trailing whitespace character
readString()111    public String readString() {
112        int c;
113        while ((c = readChar()) != -1)
114           if (!Character.isWhitespace((char) c)) break;
115 
116        if (c == -1) return null;
117 
118        String s = "" + (char) c;
119        while ((c = readChar()) != -1)
120           if (Character.isWhitespace((char) c)) break;
121           else s += (char) c;
122 
123        return s;
124    }
125 
126    // return rest of line as string and return it, not including newline
readLine()127    public String readLine() {
128        if (br == null) return null;
129        String s = null;
130        try { s = br.readLine(); }
131        catch(IOException ioe) { ioe.printStackTrace(); }
132        return s;
133    }
134 
135 
136    // return rest of input as string, use StringBuffer to avoid quadratic run time
137    // don't include NEWLINE at very end
readAll()138    public String readAll() {
139        StringBuffer sb = new StringBuffer();
140        String s = readLine();
141        if (s == null) return null;
142        sb.append(s);
143        while ((s = readLine()) != null) {
144           sb.append(NEWLINE).append(s);
145        }
146        return sb.toString();
147    }
148 
149 
close()150    public void close() {
151        try { br.close(); }
152        catch (IOException e) { e.printStackTrace(); }
153    }
154 
155 
156    // This method is just here to test the class
main(String args[])157    public static void main (String args[]) {
158       In in;
159       String s;
160 
161       // read from a URL
162       in = new In("http://www.cs.princeton.edu/IntroCS/24inout/InTest.txt");
163       System.out.println(in.readAll());
164       System.out.println();
165 
166       // read one line at a time from URL
167       in = new In("http://www.cs.princeton.edu/IntroCS/24inout/InTest.txt");
168       while ((s = in.readLine()) != null)
169          System.out.println(s);
170       System.out.println();
171 
172       // read one string at a time from URL
173       in = new In("http://www.cs.princeton.edu/IntroCS/24inout/InTest.txt");
174       while ((s = in.readString()) != null)
175          System.out.println(s);
176       System.out.println();
177 
178 
179       // read one line at a time from file in current directory
180       in = new In("InTest.txt");
181       while ((s = in.readLine()) != null)
182          System.out.println(s);
183       System.out.println();
184 
185       // read one line at a time from file using relative path
186       in = new In("../home/index.php");
187       while ((s = in.readLine()) != null)
188          System.out.println(s);
189       System.out.println();
190    }
191 
192 }
193 
194