Coverage report

| %line | %branch
--- | --- | ---
org.apache.commons.net.TimeUDPClient |
0% |
0% |

1 |  |
     /*
2 |  |
      * Copyright 2001-2005 The Apache Software Foundation
3 |  |
      *
4 |  |
      * Licensed under the Apache License, Version 2.0 (the "License");
5 |  |
      * you may not use this file except in compliance with the License.
6 |  |
      * You may obtain a copy of the License at
7 |  |
      *
8 |  |
      *     http://www.apache.org/licenses/LICENSE-2.0
9 |  |
      *
10 |  |
      * Unless required by applicable law or agreed to in writing, software
11 |  |
      * distributed under the License is distributed on an "AS IS" BASIS,
12 |  |
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 |  |
      * See the License for the specific language governing permissions and
14 |  |
      * limitations under the License.
15 |  |
      */
16 |  |
     package org.apache.commons.net;
17 |  |
18 |  |
     import java.io.IOException;
19 |  |
     import java.net.DatagramPacket;
20 |  |
     import java.net.InetAddress;
21 |  |
     import java.util.Date;
22 |  |
23 |  |
     /***
24 |  |
      * The TimeUDPClient class is a UDP implementation of a client for the
25 |  |
      * Time protocol described in RFC 868.  To use the class, merely
26 |  |
      * open a local datagram socket with
27 |  |
      * {@link org.apache.commons.net.DatagramSocketClient#open  open }
28 |  |
      * and call {@link #getTime  getTime } or
29 |  |
      * {@link #getTime  getDate } to retrieve the time. Then call
30 |  |
      * {@link org.apache.commons.net.DatagramSocketClient#close  close }
31 |  |
      * to close the connection properly.  Unlike
32 |  |
      * {@link org.apache.commons.net.TimeTCPClient},
33 |  |
      * successive calls to {@link #getTime  getTime } or
34 |  |
      * {@link #getDate  getDate } are permitted
35 |  |
      * without re-establishing a connection.  That is because UDP is a
36 |  |
      * connectionless protocol and the Time protocol is stateless.
37 |  |
      * <p>
38 |  |
      * <p>
39 |  |
      * @author Daniel F. Savarese
40 |  |
      * @see TimeTCPClient
41 |  |
      ***/
42 |  |
43 | 0 |
     public final class TimeUDPClient extends DatagramSocketClient
44 |  |
     {
45 |  |
         /*** The default time port.  It is set to 37 according to RFC 868. ***/
46 |  |
         public static final int DEFAULT_PORT = 37;
47 |  |
48 |  |
         /***
49 |  |
          * The number of seconds between 00:00 1 January 1900 and
50 |  |
          * 00:00 1 January 1970.  This value can be useful for converting
51 |  |
          * time values to other formats.
52 |  |
          ***/
53 |  |
         public static final long SECONDS_1900_TO_1970 = 2208988800L;
54 |  |
55 | 0 |
         private byte[] __dummyData = new byte[1];
56 | 0 |
         private byte[] __timeData = new byte[4];
57 |  |
58 |  |
         /***
59 |  |
          * Retrieves the time from the specified server and port and
60 |  |
          * returns it. The time is the number of seconds since
61 |  |
          * 00:00 (midnight) 1 January 1900 GMT, as specified by RFC 868.
62 |  |
          * This method reads the raw 32-bit big-endian
63 |  |
          * unsigned integer from the server, converts it to a Java long, and
64 |  |
          * returns the value.
65 |  |
          * <p>
66 |  |
          * @param host The address of the server.
67 |  |
          * @param port The port of the service.
68 |  |
          * @return The time value retrieved from the server.
69 |  |
          * @exception IOException If an error occurs while retrieving the time.
70 |  |
          ***/
71 |  |
         public long getTime(InetAddress host, int port) throws IOException
72 |  |
         {
73 |  |
             long time;
74 |  |
             DatagramPacket sendPacket, receivePacket;
75 |  |
76 | 0 |
             sendPacket =
77 |  |
                 new DatagramPacket(__dummyData, __dummyData.length, host, port);
78 | 0 |
             receivePacket = new DatagramPacket(__timeData, __timeData.length);
79 |  |
80 | 0 |
             _socket_.send(sendPacket);
81 | 0 |
             _socket_.receive(receivePacket);
82 |  |
83 | 0 |
             time = 0L;
84 | 0 |
             time |= (((__timeData[0] & 0xff) << 24) & 0xffffffffL);
85 | 0 |
             time |= (((__timeData[1] & 0xff) << 16) & 0xffffffffL);
86 | 0 |
             time |= (((__timeData[2] & 0xff) << 8) & 0xffffffffL);
87 | 0 |
             time |= ((__timeData[3] & 0xff) & 0xffffffffL);
88 |  |
89 | 0 |
             return time;
90 |  |
         }
91 |  |
92 |  |
         /*** Same as <code> getTime(host, DEFAULT_PORT); </code> ***/
93 |  |
         public long getTime(InetAddress host) throws IOException
94 |  |
         {
95 | 0 |
             return getTime(host, DEFAULT_PORT);
96 |  |
         }
97 |  |
98 |  |
99 |  |
         /***
100 |  |
          * Retrieves the time from the server and returns a Java Date
101 |  |
          * containing the time converted to the local timezone.
102 |  |
          * <p>
103 |  |
          * @param host The address of the server.
104 |  |
          * @param port The port of the service.
105 |  |
          * @return A Date value containing the time retrieved from the server
106 |  |
          *     converted to the local timezone.
107 |  |
          * @exception IOException  If an error occurs while fetching the time.
108 |  |
          ***/
109 |  |
         public Date getDate(InetAddress host, int port) throws IOException
110 |  |
         {
111 | 0 |
             return new Date((getTime(host, port) - SECONDS_1900_TO_1970)*1000L);
112 |  |
         }
113 |  |
114 |  |
115 |  |
         /*** Same as <code> getTime(host, DEFAULT_PORT); </code> ***/
116 |  |
         public Date getDate(InetAddress host) throws IOException
117 |  |
         {
118 | 0 |
             return new Date((getTime(host, DEFAULT_PORT) -
119 |  |
                              SECONDS_1900_TO_1970)*1000L);
120 |  |
         }
121 |  |
122 |  |
     }
123 |  |

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.