You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/05/28 12:52:03 UTC

(commons-net) branch master updated (4fd0ae1b -> 5403a868)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-net.git


    from 4fd0ae1b Add ASL header
     new 5aa5b0f9 Move variable declarations to method where used.
     new 5403a868 Add teste

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../org/apache/commons/net/time/TimeUDPClient.java | 22 +++++++++++++---------
 .../commons/net/time/TimeUDPClientTest.java}       | 16 ++++++++++++++--
 2 files changed, 27 insertions(+), 11 deletions(-)
 copy src/{main/java/org/apache/commons/net/bsd/package-info.java => test/java/org/apache/commons/net/time/TimeUDPClientTest.java} (70%)


(commons-net) 01/02: Move variable declarations to method where used.

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-net.git

commit 5aa5b0f9688b8823482884f01fe24b2c254ff0ce
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue May 28 08:48:04 2024 -0400

    Move variable declarations to method where used.
---
 src/main/java/org/apache/commons/net/time/TimeUDPClient.java | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/main/java/org/apache/commons/net/time/TimeUDPClient.java b/src/main/java/org/apache/commons/net/time/TimeUDPClient.java
index 7ba95caf..d3888170 100644
--- a/src/main/java/org/apache/commons/net/time/TimeUDPClient.java
+++ b/src/main/java/org/apache/commons/net/time/TimeUDPClient.java
@@ -36,6 +36,7 @@ import org.apache.commons.net.DatagramSocketClient;
  */
 
 public final class TimeUDPClient extends DatagramSocketClient {
+
     /** The default time port. It is set to 37 according to RFC 868. */
     public static final int DEFAULT_PORT = 37;
 
@@ -44,9 +45,6 @@ public final class TimeUDPClient extends DatagramSocketClient {
      */
     public static final long SECONDS_1900_TO_1970 = 2208988800L;
 
-    private final byte[] dummyData = new byte[1];
-    private final byte[] timeData = new byte[4];
-
     /**
      * Same as <code>getTime(host, DEFAULT_PORT);</code>
      *
@@ -91,6 +89,9 @@ public final class TimeUDPClient extends DatagramSocketClient {
      * @throws IOException If an error occurs while retrieving the time.
      */
     public long getTime(final InetAddress host, final int port) throws IOException {
+        final byte[] dummyData = new byte[1];
+        final byte[] timeData = new byte[4];
+
         final DatagramPacket sendPacket = new DatagramPacket(dummyData, dummyData.length, host, port);
         final DatagramPacket receivePacket = new DatagramPacket(timeData, timeData.length);
 


(commons-net) 02/02: Add teste

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-net.git

commit 5403a868cab27c6844e33ff58daa8859f7f53ad4
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue May 28 08:51:58 2024 -0400

    Add teste
---
 .../org/apache/commons/net/time/TimeUDPClient.java | 17 ++++++-----
 .../apache/commons/net/time/TimeUDPClientTest.java | 33 ++++++++++++++++++++++
 2 files changed, 43 insertions(+), 7 deletions(-)

diff --git a/src/main/java/org/apache/commons/net/time/TimeUDPClient.java b/src/main/java/org/apache/commons/net/time/TimeUDPClient.java
index d3888170..28c4ed2d 100644
--- a/src/main/java/org/apache/commons/net/time/TimeUDPClient.java
+++ b/src/main/java/org/apache/commons/net/time/TimeUDPClient.java
@@ -45,6 +45,15 @@ public final class TimeUDPClient extends DatagramSocketClient {
      */
     public static final long SECONDS_1900_TO_1970 = 2208988800L;
 
+    static long toTime(final byte[] timeData) {
+        long time = 0L;
+        time |= (timeData[0] & 0xff) << 24 & 0xffffffffL;
+        time |= (timeData[1] & 0xff) << 16 & 0xffffffffL;
+        time |= (timeData[2] & 0xff) << 8 & 0xffffffffL;
+        time |= timeData[3] & 0xff & 0xffffffffL;
+        return time;
+    }
+
     /**
      * Same as <code>getTime(host, DEFAULT_PORT);</code>
      *
@@ -98,13 +107,7 @@ public final class TimeUDPClient extends DatagramSocketClient {
         checkOpen().send(sendPacket);
         checkOpen().receive(receivePacket);
 
-        long time = 0L;
-        time |= (timeData[0] & 0xff) << 24 & 0xffffffffL;
-        time |= (timeData[1] & 0xff) << 16 & 0xffffffffL;
-        time |= (timeData[2] & 0xff) << 8 & 0xffffffffL;
-        time |= timeData[3] & 0xff & 0xffffffffL;
-
-        return time;
+        return toTime(timeData);
     }
 
 }
diff --git a/src/test/java/org/apache/commons/net/time/TimeUDPClientTest.java b/src/test/java/org/apache/commons/net/time/TimeUDPClientTest.java
new file mode 100644
index 00000000..681f54a8
--- /dev/null
+++ b/src/test/java/org/apache/commons/net/time/TimeUDPClientTest.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.net.time;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link TimeUDPClient}.
+ */
+public class TimeUDPClientTest {
+
+    @Test
+    public void test() {
+        final byte[] timeData = new byte[4];
+        assertEquals(0, TimeUDPClient.toTime(timeData));
+    }
+}