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 2023/06/24 15:02:14 UTC

[commons-net] 03/09: Add and use DatagramSocketClient.checkOpen()

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 a6816444010d0dcad14e497de7a068ce6dd095eb
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 24 10:49:16 2023 -0400

    Add and use DatagramSocketClient.checkOpen()
---
 src/changes/changes.xml                            |  3 +++
 .../apache/commons/net/DatagramSocketClient.java   | 23 ++++++++++++++++------
 .../commons/net/chargen/CharGenUDPClient.java      |  4 ++--
 .../commons/net/daytime/DaytimeUDPClient.java      |  4 ++--
 .../commons/net/discard/DiscardUDPClient.java      |  2 +-
 .../org/apache/commons/net/echo/EchoUDPClient.java |  2 +-
 .../org/apache/commons/net/ntp/NTPUDPClient.java   |  4 ++--
 .../java/org/apache/commons/net/tftp/TFTP.java     | 10 +++++-----
 .../org/apache/commons/net/time/TimeUDPClient.java |  4 ++--
 9 files changed, 35 insertions(+), 21 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 6a5fbe00..3af73342 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -78,6 +78,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="add" dev="ggregory" due-to="Gary Gregory">
         Add and use DatagramSocketClient#setSoTimeout(Duration).
       </action>
+      <action type="add" dev="ggregory" due-to="Gary Gregory">
+        Add and use DatagramSocketClient.checkOpen().
+      </action>
       <!-- FIX -->
       <action type="fix" issue="NET-650" dev="ggregory" due-to="Matthew McGillis, exceptionfactory, sebbASF">
         Delegate host resolution to Socket.connect() #138.
diff --git a/src/main/java/org/apache/commons/net/DatagramSocketClient.java b/src/main/java/org/apache/commons/net/DatagramSocketClient.java
index f3bf647d..a9f75fe8 100644
--- a/src/main/java/org/apache/commons/net/DatagramSocketClient.java
+++ b/src/main/java/org/apache/commons/net/DatagramSocketClient.java
@@ -22,6 +22,7 @@ import java.net.InetAddress;
 import java.net.SocketException;
 import java.nio.charset.Charset;
 import java.time.Duration;
+import java.util.Objects;
 
 /**
  * The DatagramSocketClient provides the basic operations that are required of client objects accessing datagram sockets. It is meant to be subclassed to avoid
@@ -68,6 +69,16 @@ public abstract class DatagramSocketClient {
     public DatagramSocketClient() {
     }
 
+    /**
+     * Returns the non-null DatagramSocket or throwns {@link NullPointerException}.
+     *
+     * @return the non-null DatagramSocket.
+     * @since 3.10.0
+     */
+    protected DatagramSocket checkOpen() {
+        return Objects.requireNonNull(_socket_, "DatagramSocket");
+    }
+
     /**
      * Closes the DatagramSocket used for the connection. You should call this method after you've finished using the class instance and also before you call
      * {@link #open open() } again. _isOpen_ is set to false and _socket_ is set to null.
@@ -118,7 +129,7 @@ public abstract class DatagramSocketClient {
      * @return The local address to which the client's socket is bound.
      */
     public InetAddress getLocalAddress() {
-        return _socket_.getLocalAddress();
+        return checkOpen().getLocalAddress();
     }
 
     /**
@@ -128,7 +139,7 @@ public abstract class DatagramSocketClient {
      * @return The port number of the open socket on the local host used for the connection.
      */
     public int getLocalPort() {
-        return _socket_.getLocalPort();
+        return checkOpen().getLocalPort();
     }
 
     /**
@@ -141,7 +152,7 @@ public abstract class DatagramSocketClient {
      */
     @Deprecated
     public int getSoTimeout() throws SocketException {
-        return _socket_.getSoTimeout();
+        return checkOpen().getSoTimeout();
     }
 
     /**
@@ -152,7 +163,7 @@ public abstract class DatagramSocketClient {
      * @throws SocketException if an error getting the timeout.
      */
     public Duration getSoTimeoutDuration() throws SocketException {
-        return Duration.ofMillis(_socket_.getSoTimeout());
+        return Duration.ofMillis(checkOpen().getSoTimeout());
     }
 
     /**
@@ -265,7 +276,7 @@ public abstract class DatagramSocketClient {
      * @since 3.10.0
      */
     public void setSoTimeout(final Duration timeout) throws SocketException {
-        _socket_.setSoTimeout(Math.toIntExact(timeout.toMillis()));
+        checkOpen().setSoTimeout(Math.toIntExact(timeout.toMillis()));
     }
 
     /**
@@ -277,6 +288,6 @@ public abstract class DatagramSocketClient {
      */
     @Deprecated
     public void setSoTimeout(final int timeout) throws SocketException {
-        _socket_.setSoTimeout(timeout);
+        checkOpen().setSoTimeout(timeout);
     }
 }
diff --git a/src/main/java/org/apache/commons/net/chargen/CharGenUDPClient.java b/src/main/java/org/apache/commons/net/chargen/CharGenUDPClient.java
index 815b32f5..8253bcbb 100644
--- a/src/main/java/org/apache/commons/net/chargen/CharGenUDPClient.java
+++ b/src/main/java/org/apache/commons/net/chargen/CharGenUDPClient.java
@@ -76,7 +76,7 @@ public final class CharGenUDPClient extends DatagramSocketClient {
         final int length;
         final byte[] result;
 
-        _socket_.receive(receivePacket);
+        checkOpen().receive(receivePacket);
 
         result = new byte[length = receivePacket.getLength()];
         System.arraycopy(receiveData, 0, result, 0, length);
@@ -104,7 +104,7 @@ public final class CharGenUDPClient extends DatagramSocketClient {
     public void send(final InetAddress host, final int port) throws IOException {
         sendPacket.setAddress(host);
         sendPacket.setPort(port);
-        _socket_.send(sendPacket);
+        checkOpen().send(sendPacket);
     }
 
 }
diff --git a/src/main/java/org/apache/commons/net/daytime/DaytimeUDPClient.java b/src/main/java/org/apache/commons/net/daytime/DaytimeUDPClient.java
index deb439ac..84495441 100644
--- a/src/main/java/org/apache/commons/net/daytime/DaytimeUDPClient.java
+++ b/src/main/java/org/apache/commons/net/daytime/DaytimeUDPClient.java
@@ -66,8 +66,8 @@ public final class DaytimeUDPClient extends DatagramSocketClient {
         sendPacket = new DatagramPacket(dummyData, dummyData.length, host, port);
         receivePacket = new DatagramPacket(timeData, timeData.length);
 
-        _socket_.send(sendPacket);
-        _socket_.receive(receivePacket);
+        checkOpen().send(sendPacket);
+        checkOpen().receive(receivePacket);
 
         return new String(receivePacket.getData(), 0, receivePacket.getLength(), getCharset()); // Java 1.6 can use getCharset()
     }
diff --git a/src/main/java/org/apache/commons/net/discard/DiscardUDPClient.java b/src/main/java/org/apache/commons/net/discard/DiscardUDPClient.java
index 5fb9f409..4353dd53 100644
--- a/src/main/java/org/apache/commons/net/discard/DiscardUDPClient.java
+++ b/src/main/java/org/apache/commons/net/discard/DiscardUDPClient.java
@@ -81,7 +81,7 @@ public class DiscardUDPClient extends DatagramSocketClient {
         sendPacket.setLength(length);
         sendPacket.setAddress(host);
         sendPacket.setPort(port);
-        _socket_.send(sendPacket);
+        checkOpen().send(sendPacket);
     }
 
 }
diff --git a/src/main/java/org/apache/commons/net/echo/EchoUDPClient.java b/src/main/java/org/apache/commons/net/echo/EchoUDPClient.java
index fb4caa55..da642a4e 100644
--- a/src/main/java/org/apache/commons/net/echo/EchoUDPClient.java
+++ b/src/main/java/org/apache/commons/net/echo/EchoUDPClient.java
@@ -63,7 +63,7 @@ public final class EchoUDPClient extends DiscardUDPClient {
     public int receive(final byte[] data, final int length) throws IOException {
         receivePacket.setData(data);
         receivePacket.setLength(length);
-        _socket_.receive(receivePacket);
+        checkOpen().receive(receivePacket);
         return receivePacket.getLength();
     }
 
diff --git a/src/main/java/org/apache/commons/net/ntp/NTPUDPClient.java b/src/main/java/org/apache/commons/net/ntp/NTPUDPClient.java
index 9fe4a711..c606252f 100644
--- a/src/main/java/org/apache/commons/net/ntp/NTPUDPClient.java
+++ b/src/main/java/org/apache/commons/net/ntp/NTPUDPClient.java
@@ -88,8 +88,8 @@ public final class NTPUDPClient extends DatagramSocketClient {
         // in server response is all 0's which is "Thu Feb 07 01:28:16 EST 2036".
         message.setTransmitTime(now);
 
-        _socket_.send(sendPacket);
-        _socket_.receive(receivePacket);
+        checkOpen().send(sendPacket);
+        checkOpen().receive(receivePacket);
 
         final long returnTimeMillis = System.currentTimeMillis();
 
diff --git a/src/main/java/org/apache/commons/net/tftp/TFTP.java b/src/main/java/org/apache/commons/net/tftp/TFTP.java
index afee0a0d..84ee0d84 100644
--- a/src/main/java/org/apache/commons/net/tftp/TFTP.java
+++ b/src/main/java/org/apache/commons/net/tftp/TFTP.java
@@ -157,7 +157,7 @@ public class TFTP extends DatagramSocketClient {
     public final TFTPPacket bufferedReceive() throws IOException, InterruptedIOException, SocketException, TFTPPacketException {
         receiveDatagram.setData(receiveBuffer);
         receiveDatagram.setLength(receiveBuffer.length);
-        _socket_.receive(receiveDatagram);
+        checkOpen().receive(receiveDatagram);
 
         final TFTPPacket newTFTPPacket = TFTPPacket.newTFTPPacket(receiveDatagram);
         trace("<", newTFTPPacket);
@@ -177,7 +177,7 @@ public class TFTP extends DatagramSocketClient {
      */
     public final void bufferedSend(final TFTPPacket packet) throws IOException {
         trace(">", packet);
-        _socket_.send(packet.newDatagram(sendDatagram, sendBuffer));
+        checkOpen().send(packet.newDatagram(sendDatagram, sendBuffer));
     }
 
     /**
@@ -197,7 +197,7 @@ public class TFTP extends DatagramSocketClient {
 
         try {
             while (true) {
-                _socket_.receive(datagram);
+                checkOpen().receive(datagram);
             }
         } catch (final SocketException | InterruptedIOException e) {
             // Do nothing. We timed out, so we hope we're caught up.
@@ -232,7 +232,7 @@ public class TFTP extends DatagramSocketClient {
 
         packet = new DatagramPacket(new byte[PACKET_SIZE], PACKET_SIZE);
 
-        _socket_.receive(packet);
+        checkOpen().receive(packet);
 
         final TFTPPacket newTFTPPacket = TFTPPacket.newTFTPPacket(packet);
         trace("<", newTFTPPacket);
@@ -247,7 +247,7 @@ public class TFTP extends DatagramSocketClient {
      */
     public final void send(final TFTPPacket packet) throws IOException {
         trace(">", packet);
-        _socket_.send(packet.newDatagram());
+        checkOpen().send(packet.newDatagram());
     }
 
     /**
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 4b9d4d7c..37b10a9d 100644
--- a/src/main/java/org/apache/commons/net/time/TimeUDPClient.java
+++ b/src/main/java/org/apache/commons/net/time/TimeUDPClient.java
@@ -98,8 +98,8 @@ public final class TimeUDPClient extends DatagramSocketClient {
         sendPacket = new DatagramPacket(dummyData, dummyData.length, host, port);
         receivePacket = new DatagramPacket(timeData, timeData.length);
 
-        _socket_.send(sendPacket);
-        _socket_.receive(receivePacket);
+        checkOpen().send(sendPacket);
+        checkOpen().receive(receivePacket);
 
         time = 0L;
         time |= (((timeData[0] & 0xff) << 24) & 0xffffffffL);