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