Permalink
Browse files

Simplify KNX Import stream usage

  • Loading branch information...
richturner committed Oct 11, 2017
1 parent 22e371d commit 92c1853acf4f0c8a015651adcd3248961639a83c
Showing with 16 additions and 6 deletions.
  1. +16 −6 agent/src/main/java/org/openremote/agent/protocol/knx/KNXProtocol.java
@@ -7,6 +7,7 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@@ -440,21 +441,22 @@ protected boolean isKNXConnectionStillUsed(KNXConnection knxConnection) {
@Override
public Asset[] discoverLinkedAssetAttributes(AssetAttribute protocolConfiguration, FileInfo fileInfo) throws IllegalStateException {
ZipInputStream zin = null;
try {
String xmlData = null;
boolean fileFound = false;
byte[] data = Util.decodeBase64(fileInfo.getContents());
ZipInputStream zin = new ZipInputStream(new ByteArrayInputStream(data));
zin = new ZipInputStream(new ByteArrayInputStream(data));
ZipEntry zipEntry = zin.getNextEntry();
while (zipEntry != null) {
if (zipEntry.getName().endsWith("/0.xml")) {
xmlData = IOUtils.toString(zin, "UTF-8");
fileFound = true;
break;
}
zipEntry = zin.getNextEntry();
}
if (isNullOrEmpty(xmlData)) {
if (!fileFound) {
String msg = "Failed to find '0.xml' in project file";
LOG.info(msg);
throw new IllegalStateException(msg);
@@ -472,7 +474,7 @@ protected boolean isKNXConnectionStillUsed(KNXConnection knxConnection) {
// Transform the source XML into byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
transformer.transform(new StreamSource(new ByteArrayInputStream(xmlData.getBytes())), new StreamResult(bos));
transformer.transform(new StreamSource(zin), new StreamResult(bos));
byte[] result = bos.toByteArray();
// we use a map of state-based datapoints and read from the transformed xml
@@ -504,6 +506,14 @@ protected boolean isKNXConnectionStillUsed(KNXConnection knxConnection) {
} catch (Exception e) {
throw new IllegalStateException("ETS import error", e);
} finally {
if (zin != null) {
try {
zin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

0 comments on commit 92c1853

Please sign in to comment.