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