Avoid UnsupportedEncodingException & use const from StandardCharsets (#11948)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp
2022-01-03 16:05:08 +01:00
committed by GitHub
parent 3f54327d5a
commit 167f8ebc49
52 changed files with 180 additions and 414 deletions

View File

@@ -34,9 +34,6 @@ public class BigAssFanBindingConstants {
// Fans communicate on this port using both UDP (discovery) and TCP (commands)
public static final int BAF_PORT = 31415;
// Commands sent to/from fan are ASCII
public static final String CHARSET = "US-ASCII";
// BigAssFan Thing Type UIDs
public static final ThingTypeUID THING_TYPE_FAN = new ThingTypeUID(BINDING_ID, "fan");
public static final ThingTypeUID THING_TYPE_LIGHT = new ThingTypeUID(BINDING_ID, "light");

View File

@@ -12,16 +12,16 @@
*/
package org.openhab.binding.bigassfan.internal.discovery;
import static org.openhab.binding.bigassfan.internal.BigAssFanBindingConstants.*;
import static org.openhab.binding.bigassfan.internal.BigAssFanBindingConstants.BAF_PORT;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -60,12 +60,10 @@ public class DiscoveryListener {
rcvBuffer = new byte[256];
rcvPacket = new DatagramPacket(rcvBuffer, rcvBuffer.length);
bcastAddress = InetAddress.getByName(BCAST_ADDRESS);
bcastBuffer = POLL_MESSAGE.getBytes(CHARSET);
bcastBuffer = POLL_MESSAGE.getBytes(StandardCharsets.US_ASCII);
bcastPacket = new DatagramPacket(bcastBuffer, bcastBuffer.length, bcastAddress, BAF_PORT);
} catch (UnknownHostException uhe) {
logger.warn("UnknownHostException sending poll request for fans: {}", uhe.getMessage(), uhe);
} catch (UnsupportedEncodingException e) {
logger.warn("Unable to convert buffer to string using {} charset", CHARSET, e);
}
}

View File

@@ -16,7 +16,6 @@ import static org.openhab.binding.bigassfan.internal.BigAssFanBindingConstants.*
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
@@ -24,6 +23,7 @@ import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.BufferOverflowException;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
@@ -611,13 +611,7 @@ public class BigAssFanHandler extends BaseThingHandler {
}
logger.debug("Sending message to {} at {}: {}", thing.getUID(), ipAddress, command);
byte[] buffer;
try {
buffer = command.getBytes(CHARSET);
} catch (UnsupportedEncodingException e) {
logger.warn("Unable to convert to string using {} charset: {}", CHARSET, e.getMessage(), e);
return;
}
byte[] buffer = command.getBytes(StandardCharsets.US_ASCII);
try {
conn.write(buffer);
} catch (IOException e) {