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 * <p>
036 * <p>
037 * @author Daniel F. Savarese
038 * @see DaytimeUDPClient
039 ***/
040
041public final class DaytimeTCPClient extends SocketClient
042{
043    /*** The default daytime port.  It is set to 13 according to RFC 867. ***/
044    public static final int DEFAULT_PORT = 13;
045
046    // Received dates will likely be less than 64 characters.
047    // This is a temporary buffer used while receiving data.
048    private char[] __buffer = new char[64];
049
050    /***
051     * The default DaytimeTCPClient constructor.  It merely sets the default
052     * port to <code> DEFAULT_PORT </code>.
053     ***/
054    public DaytimeTCPClient ()
055    {
056        setDefaultPort(DEFAULT_PORT);
057    }
058
059    /***
060     * Retrieves the time string from the server and returns it.  The
061     * server will have closed the connection at this point, so you should
062     * call
063     * {@link org.apache.commons.net.SocketClient#disconnect  disconnect }
064     * after calling this method.  To retrieve another time, you must
065     * initiate another connection with
066     * {@link org.apache.commons.net.SocketClient#connect  connect }
067     * before calling <code> getTime() </code> again.
068     * <p>
069     * @return The time string retrieved from the server.
070     * @exception IOException  If an error occurs while fetching the time string.
071     ***/
072    public String getTime() throws IOException
073    {
074        int read;
075        StringBuffer result = new StringBuffer(__buffer.length);
076        BufferedReader reader;
077
078        reader = new BufferedReader(new InputStreamReader(_input_));
079
080        while (true)
081        {
082            read = reader.read(__buffer, 0, __buffer.length);
083            if (read <= 0)
084                break;
085            result.append(__buffer, 0, read);
086        }
087
088        return result.toString();
089    }
090
091}
092