]> git.basschouten.com Git - openhab-addons.git/commitdiff
Removed usage to IOUtils.toString (#8579)
authorHilbrand Bouwkamp <hilbrand@h72.nl>
Fri, 25 Sep 2020 18:46:14 +0000 (20:46 +0200)
committerGitHub <noreply@github.com>
Fri, 25 Sep 2020 18:46:14 +0000 (11:46 -0700)
Specifically the toString reading from inputstream.

Signed-off-by: Hilbrand Bouwkamp <hilbrand@h72.nl>
13 files changed:
bundles/org.openhab.binding.airvisualnode/src/main/java/org/openhab/binding/airvisualnode/internal/handler/AirVisualNodeHandler.java
bundles/org.openhab.binding.deconz/src/test/java/org/openhab/binding/deconz/DeconzTest.java
bundles/org.openhab.binding.digitalstrom/src/main/java/org/openhab/binding/digitalstrom/internal/lib/serverconnection/impl/HttpTransportImpl.java
bundles/org.openhab.binding.foobot/src/test/java/org/openhab/binding/foobot/internal/handler/FoobotAccountHandlerTest.java
bundles/org.openhab.binding.foobot/src/test/java/org/openhab/binding/foobot/internal/handler/FoobotDeviceHandlerTest.java
bundles/org.openhab.binding.fsinternetradio/src/test/java/org/openhab/binding/fsinternetradio/test/RadioServiceDummy.java
bundles/org.openhab.binding.millheat/src/test/java/org/openhab/binding/millheat/internal/MillHeatAccountHandlerTest.java
bundles/org.openhab.binding.sensibo/src/test/java/org/openhab/binding/sensibo/internal/WireHelper.java
bundles/org.openhab.binding.sensibo/src/test/java/org/openhab/binding/sensibo/internal/handler/SensiboAccountHandlerTest.java
bundles/org.openhab.binding.spotify/src/main/java/org/openhab/binding/spotify/internal/SpotifyAuthService.java
bundles/org.openhab.binding.tplinksmarthome/src/test/java/org/openhab/binding/tplinksmarthome/internal/model/ModelTestUtil.java
bundles/org.openhab.binding.yamahareceiver/src/test/java/org/openhab/binding/yamahareceiver/internal/ResponseLoader.java
itests/org.openhab.binding.feed.tests/src/main/java/org/openhab/binding/feed/test/FeedHandlerTest.java

index dd8a369d3dcbd93495d0862ec0655c3e39e0b74c..1b22c18b40b5eaaacc7dfd78605111647d95f3b6 100644 (file)
@@ -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);
         }
     }
 
index 4eba739960e3f718362dc537e117da8a57aa2934..fae1d38be339c0f3caf776318f25ab3b5e1e441f 100644 (file)
@@ -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> T getObjectFromJson(String filename, Class<T> 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);
     }
 
index 77cea9a99a831ed32a4a89b632aa2fdf5c0a976d..4f5df416816354c977adab7803e01813a51cef90 100644 (file)
@@ -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) {
index 7824f180cf987447df0e484d14de7aa1d10623e1..bd86d6bebd4bbe9afdcbd7b2344730592eaee9c1 100644 (file)
@@ -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());
             }
index 8041d8bb662cff55432a595c64d29bacd015b516..b0f049d905bdd54964a42a98c0b397c453e09010 100644 (file)
@@ -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());
             }
index 4a4ad6730597813a389bbdbee992e5303707b148..c8909b7dcad78745589ca90dced954e512a87a23 100644 (file)
@@ -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) {
index 93c9d38bc78f839ffae77cbfb3e0a1d1ad4b0630..0868f5f0d330aab808e2c074a9bd5f74d1f1b1ea 100644 (file)
@@ -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)));
index 271fe644fd21bd6553229a8ba4c4ed1f27800276..427aaacddc4bef3cd36d028c8085ee18b85842a2 100644 (file)
@@ -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> 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> 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);
     }
 
index 7e0836d34e96c62d7ff8c764bc75ab68f5d9ffca..eece8537c68500fb36b78e837f3c26f8e2c1c870 100644 (file)
@@ -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)));
 
index d12fed02cc59b685df25c533a55b75dde1000e8c..3888f78401a2ddbe6e544dafbefe21a0b23db61e 100644 (file)
@@ -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);
             }
         }
     }
index 106f2346009a2b07533914850e0dcf06fdc46c77..3fa2ffc4250b65b72482369c65df0791b230ecf3 100644 (file)
@@ -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 ]", "");
     }
 }
index 3d845d99d2fd533ab413835c5b7141fc2e65a465..595598a285c46152dd22b7919930fdc77d268f2b 100644 (file)
@@ -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);
         }
     }
 
index 87b7283b43b6b1a8750c20dca45aa5cbb1eaa45a..a34c38b9716c57f32dff8b24c27d063eea7acd6b 100644 (file)
@@ -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);
         }
     }