Avoid UnsupportedEncodingException & use const from StandardCharsets (#11948)
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
committed by
GitHub
parent
3f54327d5a
commit
167f8ebc49
@@ -12,6 +12,9 @@
|
||||
*/
|
||||
package org.openhab.binding.homematic.internal.common;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import org.openhab.binding.homematic.internal.model.HmChannel;
|
||||
import org.openhab.binding.homematic.internal.model.HmGatewayInfo;
|
||||
import org.openhab.binding.homematic.internal.model.HmInterface;
|
||||
@@ -22,9 +25,6 @@ import org.openhab.binding.homematic.internal.model.HmInterface;
|
||||
* @author Gerhard Riegler - Initial contribution
|
||||
*/
|
||||
public class HomematicConfig {
|
||||
private static final String ISO_ENCODING = "ISO-8859-1";
|
||||
private static final String UTF_ENCODING = "UTF-8";
|
||||
|
||||
private static final String GATEWAY_TYPE_AUTO = "AUTO";
|
||||
private static final String GATEWAY_TYPE_CCU = "CCU";
|
||||
private static final String GATEWAY_TYPE_NOCCU = "NOCCU";
|
||||
@@ -363,11 +363,11 @@ public class HomematicConfig {
|
||||
/**
|
||||
* Returns the encoding that is suitable on requests to & responds from the Homematic gateway.
|
||||
*/
|
||||
public String getEncoding() {
|
||||
public Charset getEncoding() {
|
||||
if (gatewayInfo != null && gatewayInfo.isHomegear()) {
|
||||
return UTF_ENCODING;
|
||||
return StandardCharsets.UTF_8;
|
||||
} else {
|
||||
return ISO_ENCODING;
|
||||
return StandardCharsets.ISO_8859_1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ import java.io.UnsupportedEncodingException;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.math.RoundingMode;
|
||||
import java.nio.charset.Charset;
|
||||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -52,16 +53,16 @@ public class BinRpcMessage implements RpcRequest<byte[]>, RpcResponse {
|
||||
private String methodName;
|
||||
private TYPE type;
|
||||
private int args;
|
||||
private String encoding;
|
||||
private Charset encoding;
|
||||
|
||||
public BinRpcMessage(String methodName, String encoding) {
|
||||
public BinRpcMessage(String methodName, Charset encoding) {
|
||||
this(methodName, TYPE.REQUEST, encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new request with the specified methodName.
|
||||
*/
|
||||
public BinRpcMessage(String methodName, TYPE type, String encoding) {
|
||||
public BinRpcMessage(String methodName, TYPE type, Charset encoding) {
|
||||
this.methodName = methodName;
|
||||
this.type = type;
|
||||
this.encoding = encoding;
|
||||
@@ -71,7 +72,7 @@ public class BinRpcMessage implements RpcRequest<byte[]>, RpcResponse {
|
||||
/**
|
||||
* Decodes a BIN-RPC message from the given InputStream.
|
||||
*/
|
||||
public BinRpcMessage(InputStream is, boolean methodHeader, String encoding) throws IOException {
|
||||
public BinRpcMessage(InputStream is, boolean methodHeader, Charset encoding) throws IOException {
|
||||
this.encoding = encoding;
|
||||
byte sig[] = new byte[8];
|
||||
int length = is.read(sig, 0, 4);
|
||||
@@ -111,7 +112,7 @@ public class BinRpcMessage implements RpcRequest<byte[]>, RpcResponse {
|
||||
/**
|
||||
* Decodes a BIN-RPC message from the given byte array.
|
||||
*/
|
||||
public BinRpcMessage(byte[] message, boolean methodHeader, String encoding) throws IOException, ParseException {
|
||||
public BinRpcMessage(byte[] message, boolean methodHeader, Charset encoding) throws IOException, ParseException {
|
||||
this.encoding = encoding;
|
||||
if (message.length < 8) {
|
||||
throw new EOFException("Only " + message.length + " bytes received");
|
||||
@@ -213,7 +214,7 @@ public class BinRpcMessage implements RpcRequest<byte[]>, RpcResponse {
|
||||
return (new BigInteger(bi)).longValue();
|
||||
}
|
||||
|
||||
private String readString() throws UnsupportedEncodingException {
|
||||
private String readString() {
|
||||
int len = readInt();
|
||||
offset += len;
|
||||
return new String(binRpcData, offset - len, len, encoding);
|
||||
@@ -310,12 +311,7 @@ public class BinRpcMessage implements RpcRequest<byte[]>, RpcResponse {
|
||||
}
|
||||
|
||||
private void addString(String string) {
|
||||
byte sd[];
|
||||
try {
|
||||
sd = string.getBytes(encoding);
|
||||
} catch (UnsupportedEncodingException use) {
|
||||
sd = string.getBytes();
|
||||
}
|
||||
byte sd[] = string.getBytes(encoding);
|
||||
for (byte ch : sd) {
|
||||
addByte(ch);
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ package org.openhab.binding.homematic.internal.communicator.message;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.Charset;
|
||||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
@@ -43,7 +44,7 @@ public class XmlRpcResponse implements RpcResponse {
|
||||
/**
|
||||
* Decodes a XML-RPC message from the given InputStream.
|
||||
*/
|
||||
public XmlRpcResponse(InputStream is, String encoding)
|
||||
public XmlRpcResponse(InputStream is, Charset encoding)
|
||||
throws SAXException, ParserConfigurationException, IOException {
|
||||
SAXParserFactory factory = SAXParserFactory.newInstance();
|
||||
SAXParser saxParser = factory.newSAXParser();
|
||||
@@ -51,7 +52,7 @@ public class XmlRpcResponse implements RpcResponse {
|
||||
saxParser.getXMLReader().setFeature("http://xml.org/sax/features/external-general-entities", false);
|
||||
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
||||
InputSource inputSource = new InputSource(is);
|
||||
inputSource.setEncoding(encoding);
|
||||
inputSource.setEncoding(encoding.name());
|
||||
saxParser.parse(inputSource, new XmlRpcHandler());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user