Removed usage to IOUtils.toString (#8579)

Specifically the toString reading from inputstream.

Signed-off-by: Hilbrand Bouwkamp <hilbrand@h72.nl>
This commit is contained in:
Hilbrand Bouwkamp 2020-09-25 20:46:14 +02:00 committed by GitHub
parent aa3f73d423
commit 1bc55a208b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 46 additions and 43 deletions

View File

@ -32,7 +32,6 @@ import java.time.zone.ZoneRules;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; 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.config.AirVisualNodeConfig;
import org.openhab.binding.airvisualnode.internal.json.NodeData; import org.openhab.binding.airvisualnode.internal.json.NodeData;
import org.openhab.core.library.types.DateTimeType; import org.openhab.core.library.types.DateTimeType;
@ -178,7 +177,7 @@ public class AirVisualNodeHandler extends BaseThingHandler {
String url = "smb://" + nodeAddress + "/" + nodeShareName + "/" + NODE_JSON_FILE; String url = "smb://" + nodeAddress + "/" + nodeShareName + "/" + NODE_JSON_FILE;
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, nodeUsername, nodePassword); NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, nodeUsername, nodePassword);
try (SmbFileInputStream in = new SmbFileInputStream(new SmbFile(url, auth))) { 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);
} }
} }

View File

@ -21,7 +21,6 @@ import java.nio.charset.StandardCharsets;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import org.apache.commons.io.IOUtils;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; 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 { 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); return gson.fromJson(json, clazz);
} }

View File

@ -22,6 +22,7 @@ import java.net.HttpURLConnection;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.KeyManagementException; import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom; import java.security.SecureRandom;
@ -257,9 +258,9 @@ public class HttpTransportImpl implements HttpTransport {
final int responseCode = connection.getResponseCode(); final int responseCode = connection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_FORBIDDEN) { if (responseCode != HttpURLConnection.HTTP_FORBIDDEN) {
if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) { if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
response = IOUtils.toString(connection.getErrorStream()); response = new String(connection.getErrorStream().readAllBytes(), StandardCharsets.UTF_8);
} else { } else {
response = IOUtils.toString(connection.getInputStream()); response = new String(connection.getInputStream().readAllBytes(), StandardCharsets.UTF_8);
} }
if (response != null) { if (response != null) {
if (!response.contains("Authentication failed")) { if (!response.contains("Authentication failed")) {
@ -380,7 +381,8 @@ public class HttpTransportImpl implements HttpTransport {
if (connection != null) { if (connection != null) {
connection.connect(); connection.connect();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { 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; return ConnectionManager.AUTHENTIFICATION_PROBLEM;
} }
} }
@ -419,13 +421,12 @@ public class HttpTransportImpl implements HttpTransport {
File dssCert = new File(path); File dssCert = new File(path);
if (dssCert.exists()) { if (dssCert.exists()) {
if (path.endsWith(".crt")) { if (path.endsWith(".crt")) {
try { try (InputStream certInputStream = new FileInputStream(dssCert)) {
InputStream certInputStream = new FileInputStream(dssCert); String cert = new String(certInputStream.readAllBytes(), StandardCharsets.UTF_8);
String cert = IOUtils.toString(certInputStream);
if (cert.startsWith(BEGIN_CERT)) { if (cert.startsWith(BEGIN_CERT)) {
return cert; return cert;
} else { } 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); BEGIN_CERT);
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {

View File

@ -16,9 +16,9 @@ import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock; import org.mockito.Mock;
import org.openhab.binding.foobot.internal.FoobotApiConnector; import org.openhab.binding.foobot.internal.FoobotApiConnector;
@ -38,7 +38,7 @@ public class FoobotAccountHandlerTest {
@Override @Override
protected String request(String url, String apiKey) throws FoobotApiException { protected String request(String url, String apiKey) throws FoobotApiException {
try (InputStream stream = getClass().getResourceAsStream("../devices.json")) { try (InputStream stream = getClass().getResourceAsStream("../devices.json")) {
return IOUtils.toString(stream); return new String(stream.readAllBytes(), StandardCharsets.UTF_8);
} catch (IOException e) { } catch (IOException e) {
throw new AssertionError(e.getMessage()); throw new AssertionError(e.getMessage());
} }

View File

@ -16,8 +16,8 @@ import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.Mock; import org.mockito.Mock;
import org.openhab.binding.foobot.internal.FoobotApiConnector; import org.openhab.binding.foobot.internal.FoobotApiConnector;
@ -40,7 +40,7 @@ public class FoobotDeviceHandlerTest {
@Override @Override
protected String request(String url, String apiKey) throws FoobotApiException { protected String request(String url, String apiKey) throws FoobotApiException {
try (InputStream stream = getClass().getResourceAsStream("../sensors.json")) { try (InputStream stream = getClass().getResourceAsStream("../sensors.json")) {
return IOUtils.toString(stream); return new String(stream.readAllBytes(), StandardCharsets.UTF_8);
} catch (IOException e) { } catch (IOException e) {
throw new AssertionError(e.getMessage()); throw new AssertionError(e.getMessage());
} }

View File

@ -14,6 +14,7 @@ package org.openhab.binding.fsinternetradio.test;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
@ -24,7 +25,6 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.http.HttpStatus;
import org.openhab.binding.fsinternetradio.internal.radio.FrontierSiliconRadioConstants; import org.openhab.binding.fsinternetradio.internal.radio.FrontierSiliconRadioConstants;
@ -238,11 +238,11 @@ public class RadioServiceDummy extends HttpServlet {
} }
private String makeValidXMLResponse() throws IOException { 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 { 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) { public void setInvalidResponse(boolean value) {

View File

@ -18,8 +18,8 @@ import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.HttpClient;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -80,13 +80,15 @@ public class MillHeatAccountHandlerTest {
@Test @Test
public void testUpdateModel() throws InterruptedException, IOException, MillheatCommunicationException { public void testUpdateModel() throws InterruptedException, IOException, MillheatCommunicationException {
final String getHomesResponse = IOUtils.toString(getClass().getResourceAsStream("/select_home_list_ok.json")); final String getHomesResponse = new String(
final String getRoomsByHomeResponse = IOUtils getClass().getResourceAsStream("/select_home_list_ok.json").readAllBytes(), StandardCharsets.UTF_8);
.toString(getClass().getResourceAsStream("/get_rooms_by_home_ok.json")); final String getRoomsByHomeResponse = new String(
final String getDeviceByRoomResponse = IOUtils getClass().getResourceAsStream("/get_rooms_by_home_ok.json").readAllBytes(), StandardCharsets.UTF_8);
.toString(getClass().getResourceAsStream("/get_device_by_room_ok.json")); final String getDeviceByRoomResponse = new String(
final String getIndependentDevicesResponse = IOUtils getClass().getResourceAsStream("/get_device_by_room_ok.json").readAllBytes(), StandardCharsets.UTF_8);
.toString(getClass().getResourceAsStream("/get_independent_devices_ok.json")); final String getIndependentDevicesResponse = new String(
getClass().getResourceAsStream("/get_independent_devices_ok.json").readAllBytes(),
StandardCharsets.UTF_8);
stubFor(post(urlEqualTo("/millService/v1/selectHomeList")) stubFor(post(urlEqualTo("/millService/v1/selectHomeList"))
.willReturn(aResponse().withStatus(200).withBody(getHomesResponse))); .willReturn(aResponse().withStatus(200).withBody(getHomesResponse)));

View File

@ -16,9 +16,9 @@ import static org.junit.jupiter.api.Assertions.*;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import org.apache.commons.io.IOUtils;
import org.openhab.binding.sensibo.internal.dto.AbstractRequest; import org.openhab.binding.sensibo.internal.dto.AbstractRequest;
import com.google.gson.Gson; import com.google.gson.Gson;
@ -51,7 +51,8 @@ public class WireHelper {
} }
public <T> T deSerializeResponse(final String jsonClasspathName, final Type type) throws IOException { 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 JsonParser parser = new JsonParser();
final JsonObject o = parser.parse(json).getAsJsonObject(); 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 { 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); return deSerializeFromString(json, type);
} }

View File

@ -18,9 +18,9 @@ import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.HttpClient;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -87,12 +87,14 @@ public class SensiboAccountHandlerTest {
when(configuration.as(eq(SensiboAccountConfiguration.class))).thenReturn(accountConfig); when(configuration.as(eq(SensiboAccountConfiguration.class))).thenReturn(accountConfig);
// Setup initial response // 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")) stubFor(get(urlEqualTo("/api/v2/users/me/pods?apiKey=APIKEY"))
.willReturn(aResponse().withStatus(200).withBody(getPodsResponse))); .willReturn(aResponse().withStatus(200).withBody(getPodsResponse)));
// Setup 2nd response with details // 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=*")) stubFor(get(urlEqualTo("/api/v2/pods/PODID?apiKey=APIKEY&fields=*"))
.willReturn(aResponse().withStatus(200).withBody(getPodDetailsResponse))); .willReturn(aResponse().withStatus(200).withBody(getPodDetailsResponse)));

View File

@ -18,6 +18,7 @@ import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Hashtable; import java.util.Hashtable;
import java.util.List; import java.util.List;
@ -27,7 +28,6 @@ import java.util.Optional;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import org.apache.commons.io.IOUtils;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.spotify.internal.api.exception.SpotifyException; 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)); String.format("Cannot find '{}' - failed to initialize Spotify servlet", templateName));
} else { } else {
try (InputStream inputStream = index.openStream()) { try (InputStream inputStream = index.openStream()) {
return IOUtils.toString(inputStream); return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
} }
} }
} }

View File

@ -15,7 +15,6 @@ package org.openhab.binding.tplinksmarthome.internal.model;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import com.google.gson.Gson; import com.google.gson.Gson;
@ -58,8 +57,7 @@ public final class ModelTestUtil {
* @throws IOException when file could not be read. * @throws IOException when file could not be read.
*/ */
public static String readJson(String filename) throws IOException { public static String readJson(String filename) throws IOException {
return IOUtils return new String(ModelTestUtil.class.getResourceAsStream(filename + ".json").readAllBytes(),
.toString(ModelTestUtil.class.getResourceAsStream(filename + ".json"), StandardCharsets.UTF_8.name()) StandardCharsets.UTF_8).replaceAll("[\n\r\t ]", "");
.replaceAll("[\n\r\t ]", "");
} }
} }

View File

@ -14,8 +14,7 @@ package org.openhab.binding.yamahareceiver.internal;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
/** /**
* Helper for loading XML files from classpath. * Helper for loading XML files from classpath.
@ -29,7 +28,7 @@ public class ResponseLoader {
if (in == null) { if (in == null) {
return null; return null;
} }
return IOUtils.toString(in); return new String(in.readAllBytes(), StandardCharsets.UTF_8);
} }
} }

View File

@ -19,13 +19,13 @@ import static org.openhab.core.thing.ThingStatus.*;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.eclipse.jetty.http.HttpStatus; import org.eclipse.jetty.http.HttpStatus;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -138,7 +138,8 @@ public class FeedHandlerTest extends JavaOSGiTest {
public void setFeedContent(String feedContentFile) throws IOException { public void setFeedContent(String feedContentFile) throws IOException {
String path = "input/" + feedContentFile; String path = "input/" + feedContentFile;
feedContent = IOUtils.toString(this.getClass().getClassLoader().getResourceAsStream(path)); feedContent = new String(getClass().getClassLoader().getResourceAsStream(path).readAllBytes(),
StandardCharsets.UTF_8);
} }
} }