01: /*
02: * Copyright 2001-2005 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.apache.commons.net;
17:
18: import java.io.BufferedReader;
19: import java.io.IOException;
20: import java.io.InputStreamReader;
21:
22: /***
23: * The DaytimeTCPClient class is a TCP implementation of a client for the
24: * Daytime protocol described in RFC 867. To use the class, merely
25: * establish a connection with
26: * {@link org.apache.commons.net.SocketClient#connect connect }
27: * and call {@link #getTime getTime() } to retrieve the daytime
28: * string, then
29: * call {@link org.apache.commons.net.SocketClient#disconnect disconnect }
30: * to close the connection properly.
31: * <p>
32: * <p>
33: * @author Daniel F. Savarese
34: * @see DaytimeUDPClient
35: ***/
36:
37: public final class DaytimeTCPClient extends SocketClient {
38: /*** The default daytime port. It is set to 13 according to RFC 867. ***/
39: public static final int DEFAULT_PORT = 13;
40:
41: // Received dates will likely be less than 64 characters.
42: // This is a temporary buffer used while receiving data.
43: private char[] __buffer = new char[64];
44:
45: /***
46: * The default DaytimeTCPClient constructor. It merely sets the default
47: * port to <code> DEFAULT_PORT </code>.
48: ***/
49: public DaytimeTCPClient() {
50: setDefaultPort(DEFAULT_PORT);
51: }
52:
53: /***
54: * Retrieves the time string from the server and returns it. The
55: * server will have closed the connection at this point, so you should
56: * call
57: * {@link org.apache.commons.net.SocketClient#disconnect disconnect }
58: * after calling this method. To retrieve another time, you must
59: * initiate another connection with
60: * {@link org.apache.commons.net.SocketClient#connect connect }
61: * before calling <code> getTime() </code> again.
62: * <p>
63: * @return The time string retrieved from the server.
64: * @exception IOException If an error occurs while fetching the time string.
65: ***/
66: public String getTime() throws IOException {
67: int read;
68: StringBuffer result = new StringBuffer(__buffer.length);
69: BufferedReader reader;
70:
71: reader = new BufferedReader(new InputStreamReader(_input_));
72:
73: while (true) {
74: read = reader.read(__buffer, 0, __buffer.length);
75: if (read <= 0)
76: break;
77: result.append(__buffer, 0, read);
78: }
79:
80: return result.toString();
81: }
82:
83: }
|