1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.net;
17
18 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.io.BufferedOutputStream;
23 import java.io.DataOutputStream;
24
25 /***
26 * The FingerClient class implements the client side of the Internet Finger
27 * Protocol defined in RFC 1288. To finger a host you create a
28 * FingerClient instance, connect to the host, query the host, and finally
29 * disconnect from the host. If the finger service you want to query is on
30 * a non-standard port, connect to the host at that port.
31 * Here's a sample use:
32 * <pre>
33 * FingerClient finger;
34 *
35 * finger = new FingerClient();
36 *
37 * try {
38 * finger.connect("foo.bar.com");
39 * System.out.println(finger.query("foobar", false));
40 * finger.disconnect();
41 * } catch(IOException e) {
42 * System.err.println("Error I/O exception: " + e.getMessage());
43 * return;
44 * }
45 * </pre>
46 * <p>
47 * <p>
48 * @author Daniel F. Savarese
49 ***/
50
51 public class FingerClient extends SocketClient
52 {
53 /***
54 * The default FINGER port. Set to 79 according to RFC 1288.
55 ***/
56 public static final int DEFAULT_PORT = 79;
57
58 private static final String __LONG_FLAG = "/W ";
59
60 private transient StringBuffer __query = new StringBuffer(64);
61 private transient char[] __buffer = new char[1024];
62
63 /***
64 * The default FingerClient constructor. Initializes the
65 * default port to <code> DEFAULT_PORT </code>.
66 ***/
67 public FingerClient()
68 {
69 setDefaultPort(DEFAULT_PORT);
70 }
71
72
73 /***
74 * Fingers a user at the connected host and returns the output
75 * as a String. You must first connect to a finger server before
76 * calling this method, and you should disconnect afterward.
77 * <p>
78 * @param longOutput Set to true if long output is requested, false if not.
79 * @param username The name of the user to finger.
80 * @return The result of the finger query.
81 * @exception IOException If an I/O error occurs while reading the socket.
82 ***/
83 public String query(boolean longOutput, String username) throws IOException
84 {
85 int read;
86 StringBuffer result = new StringBuffer(__buffer.length);
87 BufferedReader input;
88
89 input =
90 new BufferedReader(new InputStreamReader(getInputStream(longOutput,
91 username)));
92
93 while (true)
94 {
95 read = input.read(__buffer, 0, __buffer.length);
96 if (read <= 0)
97 break;
98 result.append(__buffer, 0, read);
99 }
100
101 input.close();
102
103 return result.toString();
104 }
105
106
107 /***
108 * Fingers the connected host and returns the output
109 * as a String. You must first connect to a finger server before
110 * calling this method, and you should disconnect afterward.
111 * This is equivalent to calling <code> query(longOutput, "") </code>.
112 * <p>
113 * @param longOutput Set to true if long output is requested, false if not.
114 * @return The result of the finger query.
115 * @exception IOException If an I/O error occurs while reading the socket.
116 ***/
117 public String query(boolean longOutput) throws IOException
118 {
119 return query(longOutput, "");
120 }
121
122
123 /***
124 * Fingers a user and returns the input stream from the network connection
125 * of the finger query. You must first connect to a finger server before
126 * calling this method, and you should disconnect after finishing reading
127 * the stream.
128 * <p>
129 * @param longOutput Set to true if long output is requested, false if not.
130 * @param username The name of the user to finger.
131 * @return The InputStream of the network connection of the finger query.
132 * Can be read to obtain finger results.
133 * @exception IOException If an I/O error during the operation.
134 ***/
135 public InputStream getInputStream(boolean longOutput, String username)
136 throws IOException
137 {
138 DataOutputStream output;
139
140 __query.setLength(0);
141 if (longOutput)
142 __query.append(__LONG_FLAG);
143 __query.append(username);
144 __query.append(SocketClient.NETASCII_EOL);
145
146 output =
147 new DataOutputStream(new BufferedOutputStream(_output_, 1024));
148 output.writeBytes(__query.toString());
149 output.flush();
150
151 return _input_;
152 }
153
154
155 /***
156 * Fingers the connected host and returns the input stream from
157 * the network connection of the finger query. This is equivalent to
158 * calling getInputStream(longOutput, ""). You must first connect to a
159 * finger server before calling this method, and you should disconnect
160 * after finishing reading the stream.
161 * <p>
162 * @param longOutput Set to true if long output is requested, false if not.
163 * @return The InputStream of the network connection of the finger query.
164 * Can be read to obtain finger results.
165 * @exception IOException If an I/O error during the operation.
166 ***/
167 public InputStream getInputStream(boolean longOutput) throws IOException
168 {
169 return getInputStream(longOutput, "");
170 }
171
172 }