DaytimeTCPClient.java

  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. package org.apache.commons.net.daytime;

  18. import java.io.BufferedReader;
  19. import java.io.IOException;
  20. import java.io.InputStreamReader;

  21. import org.apache.commons.net.SocketClient;

  22. /**
  23.  * 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
  24.  * with {@link org.apache.commons.net.SocketClient#connect connect } and call {@link #getTime getTime() } to retrieve the daytime string, then call
  25.  * {@link org.apache.commons.net.SocketClient#disconnect disconnect } to close the connection properly.
  26.  *
  27.  * @see DaytimeUDPClient
  28.  */
  29. public final class DaytimeTCPClient extends SocketClient {
  30.     /** The default daytime port. It is set to 13 according to RFC 867. */
  31.     public static final int DEFAULT_PORT = 13;

  32.     // Received dates will likely be less than 64 characters.
  33.     // This is a temporary buffer used while receiving data.
  34.     private final char[] buffer = new char[64];

  35.     /**
  36.      * The default DaytimeTCPClient constructor. It merely sets the default port to <code>DEFAULT_PORT</code>.
  37.      */
  38.     public DaytimeTCPClient() {
  39.         setDefaultPort(DEFAULT_PORT);
  40.     }

  41.     /**
  42.      * Retrieves the time string from the server and returns it. The server will have closed the connection at this point, so you should call
  43.      * {@link org.apache.commons.net.SocketClient#disconnect disconnect } after calling this method. To retrieve another time, you must initiate another
  44.      * connection with {@link org.apache.commons.net.SocketClient#connect connect } before calling <code>getTime()</code> again.
  45.      *
  46.      * @return The time string retrieved from the server.
  47.      * @throws IOException If an error occurs while fetching the time string.
  48.      */
  49.     public String getTime() throws IOException {
  50.         int read;
  51.         final StringBuilder result = new StringBuilder(buffer.length);
  52.         final BufferedReader reader;

  53.         reader = new BufferedReader(new InputStreamReader(_input_, getCharset()));

  54.         while (true) {
  55.             read = reader.read(buffer, 0, buffer.length);
  56.             if (read <= 0) {
  57.                 break;
  58.             }
  59.             result.append(buffer, 0, read);
  60.         }

  61.         return result.toString();
  62.     }

  63. }