From 1bc55a208be93da284de70d1788c7756c1bd1f72 Mon Sep 17 00:00:00 2001 From: Hilbrand Bouwkamp Date: Fri, 25 Sep 2020 20:46:14 +0200 Subject: [PATCH] Removed usage to IOUtils.toString (#8579) Specifically the toString reading from inputstream. Signed-off-by: Hilbrand Bouwkamp --- .../internal/handler/AirVisualNodeHandler.java | 3 +-- .../org/openhab/binding/deconz/DeconzTest.java | 3 +-- .../impl/HttpTransportImpl.java | 15 ++++++++------- .../handler/FoobotAccountHandlerTest.java | 4 ++-- .../handler/FoobotDeviceHandlerTest.java | 4 ++-- .../test/RadioServiceDummy.java | 6 +++--- .../internal/MillHeatAccountHandlerTest.java | 18 ++++++++++-------- .../binding/sensibo/internal/WireHelper.java | 8 +++++--- .../handler/SensiboAccountHandlerTest.java | 8 +++++--- .../spotify/internal/SpotifyAuthService.java | 4 ++-- .../internal/model/ModelTestUtil.java | 6 ++---- .../internal/ResponseLoader.java | 5 ++--- .../binding/feed/test/FeedHandlerTest.java | 5 +++-- 13 files changed, 46 insertions(+), 43 deletions(-) diff --git a/bundles/org.openhab.binding.airvisualnode/src/main/java/org/openhab/binding/airvisualnode/internal/handler/AirVisualNodeHandler.java b/bundles/org.openhab.binding.airvisualnode/src/main/java/org/openhab/binding/airvisualnode/internal/handler/AirVisualNodeHandler.java index dd8a369d3dc..1b22c18b40b 100644 --- a/bundles/org.openhab.binding.airvisualnode/src/main/java/org/openhab/binding/airvisualnode/internal/handler/AirVisualNodeHandler.java +++ b/bundles/org.openhab.binding.airvisualnode/src/main/java/org/openhab/binding/airvisualnode/internal/handler/AirVisualNodeHandler.java @@ -32,7 +32,6 @@ import java.time.zone.ZoneRules; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; -import org.apache.commons.io.IOUtils; import org.openhab.binding.airvisualnode.internal.config.AirVisualNodeConfig; import org.openhab.binding.airvisualnode.internal.json.NodeData; import org.openhab.core.library.types.DateTimeType; @@ -178,7 +177,7 @@ public class AirVisualNodeHandler extends BaseThingHandler { String url = "smb://" + nodeAddress + "/" + nodeShareName + "/" + NODE_JSON_FILE; NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, nodeUsername, nodePassword); try (SmbFileInputStream in = new SmbFileInputStream(new SmbFile(url, auth))) { - return IOUtils.toString(in, StandardCharsets.UTF_8.name()); + return new String(in.readAllBytes(), StandardCharsets.UTF_8); } } diff --git a/bundles/org.openhab.binding.deconz/src/test/java/org/openhab/binding/deconz/DeconzTest.java b/bundles/org.openhab.binding.deconz/src/test/java/org/openhab/binding/deconz/DeconzTest.java index 4eba739960e..fae1d38be33 100644 --- a/bundles/org.openhab.binding.deconz/src/test/java/org/openhab/binding/deconz/DeconzTest.java +++ b/bundles/org.openhab.binding.deconz/src/test/java/org/openhab/binding/deconz/DeconzTest.java @@ -21,7 +21,6 @@ import java.nio.charset.StandardCharsets; import java.time.ZoneId; import java.time.ZonedDateTime; -import org.apache.commons.io.IOUtils; import org.eclipse.jdt.annotation.NonNullByDefault; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -89,7 +88,7 @@ public class DeconzTest { } public static T getObjectFromJson(String filename, Class clazz, Gson gson) throws IOException { - String json = IOUtils.toString(DeconzTest.class.getResourceAsStream(filename), StandardCharsets.UTF_8.name()); + String json = new String(DeconzTest.class.getResourceAsStream(filename).readAllBytes(), StandardCharsets.UTF_8); return gson.fromJson(json, clazz); } diff --git a/bundles/org.openhab.binding.digitalstrom/src/main/java/org/openhab/binding/digitalstrom/internal/lib/serverconnection/impl/HttpTransportImpl.java b/bundles/org.openhab.binding.digitalstrom/src/main/java/org/openhab/binding/digitalstrom/internal/lib/serverconnection/impl/HttpTransportImpl.java index 77cea9a99a8..4f5df416816 100644 --- a/bundles/org.openhab.binding.digitalstrom/src/main/java/org/openhab/binding/digitalstrom/internal/lib/serverconnection/impl/HttpTransportImpl.java +++ b/bundles/org.openhab.binding.digitalstrom/src/main/java/org/openhab/binding/digitalstrom/internal/lib/serverconnection/impl/HttpTransportImpl.java @@ -22,6 +22,7 @@ import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.SocketTimeoutException; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; @@ -257,9 +258,9 @@ public class HttpTransportImpl implements HttpTransport { final int responseCode = connection.getResponseCode(); if (responseCode != HttpURLConnection.HTTP_FORBIDDEN) { if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) { - response = IOUtils.toString(connection.getErrorStream()); + response = new String(connection.getErrorStream().readAllBytes(), StandardCharsets.UTF_8); } else { - response = IOUtils.toString(connection.getInputStream()); + response = new String(connection.getInputStream().readAllBytes(), StandardCharsets.UTF_8); } if (response != null) { if (!response.contains("Authentication failed")) { @@ -380,7 +381,8 @@ public class HttpTransportImpl implements HttpTransport { if (connection != null) { connection.connect(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { - if (IOUtils.toString(connection.getInputStream()).contains("Authentication failed")) { + if (new String(connection.getInputStream().readAllBytes(), StandardCharsets.UTF_8) + .contains("Authentication failed")) { return ConnectionManager.AUTHENTIFICATION_PROBLEM; } } @@ -419,13 +421,12 @@ public class HttpTransportImpl implements HttpTransport { File dssCert = new File(path); if (dssCert.exists()) { if (path.endsWith(".crt")) { - try { - InputStream certInputStream = new FileInputStream(dssCert); - String cert = IOUtils.toString(certInputStream); + try (InputStream certInputStream = new FileInputStream(dssCert)) { + String cert = new String(certInputStream.readAllBytes(), StandardCharsets.UTF_8); if (cert.startsWith(BEGIN_CERT)) { return cert; } else { - logger.error("File is not a PEM certificate file. PEM-Certificats starts with: {}", + logger.error("File is not a PEM certificate file. PEM-Certificates starts with: {}", BEGIN_CERT); } } catch (FileNotFoundException e) { diff --git a/bundles/org.openhab.binding.foobot/src/test/java/org/openhab/binding/foobot/internal/handler/FoobotAccountHandlerTest.java b/bundles/org.openhab.binding.foobot/src/test/java/org/openhab/binding/foobot/internal/handler/FoobotAccountHandlerTest.java index 7824f180cf9..bd86d6bebd4 100644 --- a/bundles/org.openhab.binding.foobot/src/test/java/org/openhab/binding/foobot/internal/handler/FoobotAccountHandlerTest.java +++ b/bundles/org.openhab.binding.foobot/src/test/java/org/openhab/binding/foobot/internal/handler/FoobotAccountHandlerTest.java @@ -16,9 +16,9 @@ import static org.junit.jupiter.api.Assertions.*; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.util.List; -import org.apache.commons.io.IOUtils; import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.openhab.binding.foobot.internal.FoobotApiConnector; @@ -38,7 +38,7 @@ public class FoobotAccountHandlerTest { @Override protected String request(String url, String apiKey) throws FoobotApiException { try (InputStream stream = getClass().getResourceAsStream("../devices.json")) { - return IOUtils.toString(stream); + return new String(stream.readAllBytes(), StandardCharsets.UTF_8); } catch (IOException e) { throw new AssertionError(e.getMessage()); } diff --git a/bundles/org.openhab.binding.foobot/src/test/java/org/openhab/binding/foobot/internal/handler/FoobotDeviceHandlerTest.java b/bundles/org.openhab.binding.foobot/src/test/java/org/openhab/binding/foobot/internal/handler/FoobotDeviceHandlerTest.java index 8041d8bb662..b0f049d905b 100644 --- a/bundles/org.openhab.binding.foobot/src/test/java/org/openhab/binding/foobot/internal/handler/FoobotDeviceHandlerTest.java +++ b/bundles/org.openhab.binding.foobot/src/test/java/org/openhab/binding/foobot/internal/handler/FoobotDeviceHandlerTest.java @@ -16,8 +16,8 @@ import static org.junit.jupiter.api.Assertions.*; import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; -import org.apache.commons.io.IOUtils; import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.openhab.binding.foobot.internal.FoobotApiConnector; @@ -40,7 +40,7 @@ public class FoobotDeviceHandlerTest { @Override protected String request(String url, String apiKey) throws FoobotApiException { try (InputStream stream = getClass().getResourceAsStream("../sensors.json")) { - return IOUtils.toString(stream); + return new String(stream.readAllBytes(), StandardCharsets.UTF_8); } catch (IOException e) { throw new AssertionError(e.getMessage()); } diff --git a/bundles/org.openhab.binding.fsinternetradio/src/test/java/org/openhab/binding/fsinternetradio/test/RadioServiceDummy.java b/bundles/org.openhab.binding.fsinternetradio/src/test/java/org/openhab/binding/fsinternetradio/test/RadioServiceDummy.java index 4a4ad673059..c8909b7dcad 100644 --- a/bundles/org.openhab.binding.fsinternetradio/src/test/java/org/openhab/binding/fsinternetradio/test/RadioServiceDummy.java +++ b/bundles/org.openhab.binding.fsinternetradio/src/test/java/org/openhab/binding/fsinternetradio/test/RadioServiceDummy.java @@ -14,6 +14,7 @@ package org.openhab.binding.fsinternetradio.test; import java.io.IOException; import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.Collections; import java.util.Map; @@ -24,7 +25,6 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.apache.commons.io.IOUtils; import org.eclipse.jetty.http.HttpStatus; import org.openhab.binding.fsinternetradio.internal.radio.FrontierSiliconRadioConstants; @@ -238,11 +238,11 @@ public class RadioServiceDummy extends HttpServlet { } private String makeValidXMLResponse() throws IOException { - return IOUtils.toString(getClass().getResourceAsStream("/validXml.xml")); + return new String(getClass().getResourceAsStream("/validXml.xml").readAllBytes(), StandardCharsets.UTF_8); } private String makeInvalidXMLResponse() throws IOException { - return IOUtils.toString(getClass().getResourceAsStream("/invalidXml.xml")); + return new String(getClass().getResourceAsStream("/invalidXml.xml").readAllBytes(), StandardCharsets.UTF_8); } public void setInvalidResponse(boolean value) { diff --git a/bundles/org.openhab.binding.millheat/src/test/java/org/openhab/binding/millheat/internal/MillHeatAccountHandlerTest.java b/bundles/org.openhab.binding.millheat/src/test/java/org/openhab/binding/millheat/internal/MillHeatAccountHandlerTest.java index 93c9d38bc78..0868f5f0d33 100644 --- a/bundles/org.openhab.binding.millheat/src/test/java/org/openhab/binding/millheat/internal/MillHeatAccountHandlerTest.java +++ b/bundles/org.openhab.binding.millheat/src/test/java/org/openhab/binding/millheat/internal/MillHeatAccountHandlerTest.java @@ -18,8 +18,8 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; import java.io.IOException; +import java.nio.charset.StandardCharsets; -import org.apache.commons.io.IOUtils; import org.eclipse.jetty.client.HttpClient; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -80,13 +80,15 @@ public class MillHeatAccountHandlerTest { @Test public void testUpdateModel() throws InterruptedException, IOException, MillheatCommunicationException { - final String getHomesResponse = IOUtils.toString(getClass().getResourceAsStream("/select_home_list_ok.json")); - final String getRoomsByHomeResponse = IOUtils - .toString(getClass().getResourceAsStream("/get_rooms_by_home_ok.json")); - final String getDeviceByRoomResponse = IOUtils - .toString(getClass().getResourceAsStream("/get_device_by_room_ok.json")); - final String getIndependentDevicesResponse = IOUtils - .toString(getClass().getResourceAsStream("/get_independent_devices_ok.json")); + final String getHomesResponse = new String( + getClass().getResourceAsStream("/select_home_list_ok.json").readAllBytes(), StandardCharsets.UTF_8); + final String getRoomsByHomeResponse = new String( + getClass().getResourceAsStream("/get_rooms_by_home_ok.json").readAllBytes(), StandardCharsets.UTF_8); + final String getDeviceByRoomResponse = new String( + getClass().getResourceAsStream("/get_device_by_room_ok.json").readAllBytes(), StandardCharsets.UTF_8); + final String getIndependentDevicesResponse = new String( + getClass().getResourceAsStream("/get_independent_devices_ok.json").readAllBytes(), + StandardCharsets.UTF_8); stubFor(post(urlEqualTo("/millService/v1/selectHomeList")) .willReturn(aResponse().withStatus(200).withBody(getHomesResponse))); diff --git a/bundles/org.openhab.binding.sensibo/src/test/java/org/openhab/binding/sensibo/internal/WireHelper.java b/bundles/org.openhab.binding.sensibo/src/test/java/org/openhab/binding/sensibo/internal/WireHelper.java index 271fe644fd2..427aaacddc4 100644 --- a/bundles/org.openhab.binding.sensibo/src/test/java/org/openhab/binding/sensibo/internal/WireHelper.java +++ b/bundles/org.openhab.binding.sensibo/src/test/java/org/openhab/binding/sensibo/internal/WireHelper.java @@ -16,9 +16,9 @@ import static org.junit.jupiter.api.Assertions.*; import java.io.IOException; import java.lang.reflect.Type; +import java.nio.charset.StandardCharsets; import java.time.ZonedDateTime; -import org.apache.commons.io.IOUtils; import org.openhab.binding.sensibo.internal.dto.AbstractRequest; import com.google.gson.Gson; @@ -51,7 +51,8 @@ public class WireHelper { } public T deSerializeResponse(final String jsonClasspathName, final Type type) throws IOException { - final String json = IOUtils.toString(WireHelper.class.getResourceAsStream(jsonClasspathName)); + final String json = new String(WireHelper.class.getResourceAsStream(jsonClasspathName).readAllBytes(), + StandardCharsets.UTF_8); final JsonParser parser = new JsonParser(); final JsonObject o = parser.parse(json).getAsJsonObject(); @@ -61,7 +62,8 @@ public class WireHelper { } public T deSerializeFromClasspathResource(final String jsonClasspathName, final Type type) throws IOException { - final String json = IOUtils.toString(WireHelper.class.getResourceAsStream(jsonClasspathName)); + final String json = new String(WireHelper.class.getResourceAsStream(jsonClasspathName).readAllBytes(), + StandardCharsets.UTF_8); return deSerializeFromString(json, type); } diff --git a/bundles/org.openhab.binding.sensibo/src/test/java/org/openhab/binding/sensibo/internal/handler/SensiboAccountHandlerTest.java b/bundles/org.openhab.binding.sensibo/src/test/java/org/openhab/binding/sensibo/internal/handler/SensiboAccountHandlerTest.java index 7e0836d34e9..eece8537c68 100644 --- a/bundles/org.openhab.binding.sensibo/src/test/java/org/openhab/binding/sensibo/internal/handler/SensiboAccountHandlerTest.java +++ b/bundles/org.openhab.binding.sensibo/src/test/java/org/openhab/binding/sensibo/internal/handler/SensiboAccountHandlerTest.java @@ -18,9 +18,9 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; import java.io.IOException; +import java.nio.charset.StandardCharsets; import java.util.List; -import org.apache.commons.io.IOUtils; import org.eclipse.jetty.client.HttpClient; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -87,12 +87,14 @@ public class SensiboAccountHandlerTest { when(configuration.as(eq(SensiboAccountConfiguration.class))).thenReturn(accountConfig); // Setup initial response - final String getPodsResponse = IOUtils.toString(getClass().getResourceAsStream(podsResponse)); + final String getPodsResponse = new String(getClass().getResourceAsStream(podsResponse).readAllBytes(), + StandardCharsets.UTF_8); stubFor(get(urlEqualTo("/api/v2/users/me/pods?apiKey=APIKEY")) .willReturn(aResponse().withStatus(200).withBody(getPodsResponse))); // Setup 2nd response with details - final String getPodDetailsResponse = IOUtils.toString(getClass().getResourceAsStream(podDetailsResponse)); + final String getPodDetailsResponse = new String( + getClass().getResourceAsStream(podDetailsResponse).readAllBytes(), StandardCharsets.UTF_8); stubFor(get(urlEqualTo("/api/v2/pods/PODID?apiKey=APIKEY&fields=*")) .willReturn(aResponse().withStatus(200).withBody(getPodDetailsResponse))); diff --git a/bundles/org.openhab.binding.spotify/src/main/java/org/openhab/binding/spotify/internal/SpotifyAuthService.java b/bundles/org.openhab.binding.spotify/src/main/java/org/openhab/binding/spotify/internal/SpotifyAuthService.java index d12fed02cc5..3888f78401a 100644 --- a/bundles/org.openhab.binding.spotify/src/main/java/org/openhab/binding/spotify/internal/SpotifyAuthService.java +++ b/bundles/org.openhab.binding.spotify/src/main/java/org/openhab/binding/spotify/internal/SpotifyAuthService.java @@ -18,6 +18,7 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Hashtable; import java.util.List; @@ -27,7 +28,6 @@ import java.util.Optional; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; -import org.apache.commons.io.IOUtils; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.openhab.binding.spotify.internal.api.exception.SpotifyException; @@ -107,7 +107,7 @@ public class SpotifyAuthService { String.format("Cannot find '{}' - failed to initialize Spotify servlet", templateName)); } else { try (InputStream inputStream = index.openStream()) { - return IOUtils.toString(inputStream); + return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8); } } } diff --git a/bundles/org.openhab.binding.tplinksmarthome/src/test/java/org/openhab/binding/tplinksmarthome/internal/model/ModelTestUtil.java b/bundles/org.openhab.binding.tplinksmarthome/src/test/java/org/openhab/binding/tplinksmarthome/internal/model/ModelTestUtil.java index 106f2346009..3fa2ffc4250 100644 --- a/bundles/org.openhab.binding.tplinksmarthome/src/test/java/org/openhab/binding/tplinksmarthome/internal/model/ModelTestUtil.java +++ b/bundles/org.openhab.binding.tplinksmarthome/src/test/java/org/openhab/binding/tplinksmarthome/internal/model/ModelTestUtil.java @@ -15,7 +15,6 @@ package org.openhab.binding.tplinksmarthome.internal.model; import java.io.IOException; import java.nio.charset.StandardCharsets; -import org.apache.commons.io.IOUtils; import org.eclipse.jdt.annotation.NonNullByDefault; import com.google.gson.Gson; @@ -58,8 +57,7 @@ public final class ModelTestUtil { * @throws IOException when file could not be read. */ public static String readJson(String filename) throws IOException { - return IOUtils - .toString(ModelTestUtil.class.getResourceAsStream(filename + ".json"), StandardCharsets.UTF_8.name()) - .replaceAll("[\n\r\t ]", ""); + return new String(ModelTestUtil.class.getResourceAsStream(filename + ".json").readAllBytes(), + StandardCharsets.UTF_8).replaceAll("[\n\r\t ]", ""); } } diff --git a/bundles/org.openhab.binding.yamahareceiver/src/test/java/org/openhab/binding/yamahareceiver/internal/ResponseLoader.java b/bundles/org.openhab.binding.yamahareceiver/src/test/java/org/openhab/binding/yamahareceiver/internal/ResponseLoader.java index 3d845d99d2f..595598a285c 100644 --- a/bundles/org.openhab.binding.yamahareceiver/src/test/java/org/openhab/binding/yamahareceiver/internal/ResponseLoader.java +++ b/bundles/org.openhab.binding.yamahareceiver/src/test/java/org/openhab/binding/yamahareceiver/internal/ResponseLoader.java @@ -14,8 +14,7 @@ package org.openhab.binding.yamahareceiver.internal; import java.io.IOException; import java.io.InputStream; - -import org.apache.commons.io.IOUtils; +import java.nio.charset.StandardCharsets; /** * Helper for loading XML files from classpath. @@ -29,7 +28,7 @@ public class ResponseLoader { if (in == null) { return null; } - return IOUtils.toString(in); + return new String(in.readAllBytes(), StandardCharsets.UTF_8); } } diff --git a/itests/org.openhab.binding.feed.tests/src/main/java/org/openhab/binding/feed/test/FeedHandlerTest.java b/itests/org.openhab.binding.feed.tests/src/main/java/org/openhab/binding/feed/test/FeedHandlerTest.java index 87b7283b43b..a34c38b9716 100644 --- a/itests/org.openhab.binding.feed.tests/src/main/java/org/openhab/binding/feed/test/FeedHandlerTest.java +++ b/itests/org.openhab.binding.feed.tests/src/main/java/org/openhab/binding/feed/test/FeedHandlerTest.java @@ -19,13 +19,13 @@ import static org.openhab.core.thing.ThingStatus.*; import java.io.IOException; import java.math.BigDecimal; +import java.nio.charset.StandardCharsets; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.apache.commons.io.IOUtils; import org.eclipse.jetty.http.HttpStatus; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -138,7 +138,8 @@ public class FeedHandlerTest extends JavaOSGiTest { public void setFeedContent(String feedContentFile) throws IOException { String path = "input/" + feedContentFile; - feedContent = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream(path)); + feedContent = new String(getClass().getClassLoader().getResourceAsStream(path).readAllBytes(), + StandardCharsets.UTF_8); } }