001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.net.daytime;
019
020import java.io.BufferedReader;
021import java.io.IOException;
022import java.io.InputStreamReader;
023
024import org.apache.commons.net.SocketClient;
025
026/**
027 * The DaytimeTCPClient class is a TCP implementation of a client for the
028 * Daytime protocol described in RFC 867.  To use the class, merely
029 * establish a connection with
030 * {@link org.apache.commons.net.SocketClient#connect  connect }
031 * and call {@link #getTime  getTime() } to retrieve the daytime
032 * string, then
033 * call {@link org.apache.commons.net.SocketClient#disconnect  disconnect }
034 * to close the connection properly.
035 * @see DaytimeUDPClient
036 */
037public final class DaytimeTCPClient extends SocketClient
038{
039    /** The default daytime port.  It is set to 13 according to RFC 867. */
040    public static final int DEFAULT_PORT = 13;
041
042    // Received dates will likely be less than 64 characters.
043    // This is a temporary buffer used while receiving data.
044    private final char[] __buffer = new char[64];
045
046    /**
047     * The default DaytimeTCPClient constructor.  It merely sets the default
048     * port to <code> DEFAULT_PORT </code>.
049     */
050    public DaytimeTCPClient ()
051    {
052        setDefaultPort(DEFAULT_PORT);
053    }
054
055    /**
056     * Retrieves the time string from the server and returns it.  The
057     * server will have closed the connection at this point, so you should
058     * call
059     * {@link org.apache.commons.net.SocketClient#disconnect  disconnect }
060     * after calling this method.  To retrieve another time, you must
061     * initiate another connection with
062     * {@link org.apache.commons.net.SocketClient#connect  connect }
063     * before calling <code> getTime() </code> again.
064     *
065     * @return The time string retrieved from the server.
066     * @throws IOException  If an error occurs while fetching the time string.
067     */
068    public String getTime() throws IOException
069    {
070        int read;
071        StringBuilder result = new StringBuilder(__buffer.length);
072        BufferedReader reader;
073
074        reader = new BufferedReader(new InputStreamReader(_input_, getCharset()));
075
076        while (true)
077        {
078            read = reader.read(__buffer, 0, __buffer.length);
079            if (read <= 0) {
080                break;
081            }
082            result.append(__buffer, 0, read);
083        }
084
085        return result.toString();
086    }
087
088}
089