Skip to content

Commit e6d8dfb

Browse files
authored
Reduce dependency on commons-io and commons-codec (openhab#10614)
Signed-off-by: Wouter Born <[email protected]>
1 parent 02b4943 commit e6d8dfb

File tree

20 files changed

+175
-124
lines changed

20 files changed

+175
-124
lines changed

bundles/org.openhab.binding.allplay/src/main/java/org/openhab/binding/allplay/internal/handler/AllPlayHandler.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.concurrent.ScheduledFuture;
2424
import java.util.concurrent.TimeUnit;
2525

26-
import org.apache.commons.io.IOUtils;
2726
import org.openhab.binding.allplay.internal.AllPlayBindingConstants;
2827
import org.openhab.binding.allplay.internal.AllPlayBindingProperties;
2928
import org.openhab.core.common.ThreadPoolManager;
@@ -538,7 +537,7 @@ public PercentType getVolume() throws SpeakerException {
538537
private byte[] getRawDataFromUrl(String urlString) throws Exception {
539538
URL url = new URL(urlString);
540539
URLConnection connection = url.openConnection();
541-
return IOUtils.toByteArray(connection.getInputStream());
540+
return connection.getInputStream().readAllBytes();
542541
}
543542

544543
private int convertPercentToAbsoluteVolume(PercentType percentVolume) throws SpeakerException {

bundles/org.openhab.binding.bluetooth.bluegiga/src/main/java/org/openhab/binding/bluetooth/bluegiga/handler/BlueGigaBridgeHandler.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import java.util.concurrent.ScheduledFuture;
3131
import java.util.concurrent.TimeUnit;
3232

33-
import org.apache.commons.io.IOUtils;
3433
import org.eclipse.jdt.annotation.NonNullByDefault;
3534
import org.eclipse.jdt.annotation.Nullable;
3635
import org.openhab.binding.bluetooth.AbstractBluetoothBridgeHandler;
@@ -411,10 +410,16 @@ private void closeSerialPort(SerialPort sp) {
411410
// Ignore all as RXTX seems to send arbitrary exceptions when BlueGiga module is detached
412411
} finally {
413412
outputStream.ifPresent(output -> {
414-
IOUtils.closeQuietly(output);
413+
try {
414+
output.close();
415+
} catch (IOException e) {
416+
}
415417
});
416418
inputStream.ifPresent(input -> {
417-
IOUtils.closeQuietly(input);
419+
try {
420+
input.close();
421+
} catch (IOException e) {
422+
}
418423
});
419424
sp.close();
420425
logger.debug("Closed serial port.");

bundles/org.openhab.binding.bluetooth.bluegiga/src/main/java/org/openhab/binding/bluetooth/bluegiga/internal/BlueGigaSerialHandler.java

+32-4
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
*/
1313
package org.openhab.binding.bluetooth.bluegiga.internal;
1414

15+
import java.io.EOFException;
1516
import java.io.IOException;
1617
import java.io.InputStream;
1718
import java.io.OutputStream;
1819
import java.util.Set;
1920
import java.util.concurrent.CopyOnWriteArraySet;
2021

21-
import org.apache.commons.io.IOUtils;
2222
import org.eclipse.jdt.annotation.NonNullByDefault;
2323
import org.openhab.binding.bluetooth.bluegiga.internal.command.gap.BlueGigaEndProcedureCommand;
2424
import org.slf4j.Logger;
@@ -96,7 +96,7 @@ private void flush() {
9696
// Wait BlueGiga controller have stopped all activity
9797
Thread.sleep(100);
9898
logger.trace("Bytes available: {}", inputStream.available());
99-
IOUtils.skipFully(inputStream, inputStream.available());
99+
skipFully(inputStream, inputStream.available());
100100
} catch (InterruptedException e) {
101101
close = true;
102102
} catch (IOException e) {
@@ -105,6 +105,28 @@ private void flush() {
105105
logger.trace("Flush done");
106106
}
107107

108+
private void skipFully(final InputStream input, final long toSkip) throws IOException {
109+
if (toSkip < 0) {
110+
throw new IllegalArgumentException("Bytes to skip must not be negative: " + toSkip);
111+
}
112+
113+
long remain = toSkip;
114+
115+
final byte[] byteArray = new byte[8192];
116+
while (remain > 0) {
117+
final long n = input.read(byteArray, 0, (int) Math.min(remain, byteArray.length));
118+
if (n < 0) { // EOF
119+
break;
120+
}
121+
remain -= n;
122+
}
123+
124+
long skipped = toSkip - remain;
125+
if (skipped != toSkip) {
126+
throw new EOFException("Bytes to skip: " + toSkip + " actual: " + skipped);
127+
}
128+
}
129+
108130
/**
109131
* Requests parser thread to shutdown. Waits forever while the parser thread is getting shut down.
110132
*/
@@ -123,8 +145,14 @@ public void close(long timeout) {
123145
parserThread.interrupt();
124146
// Give a fair chance to shutdown nicely
125147
Thread.sleep(50);
126-
IOUtils.closeQuietly(outputStream);
127-
IOUtils.closeQuietly(inputStream);
148+
try {
149+
outputStream.close();
150+
} catch (IOException e) {
151+
}
152+
try {
153+
inputStream.close();
154+
} catch (IOException e) {
155+
}
128156
parserThread.join(0);
129157
} catch (InterruptedException e) {
130158
logger.warn("Interrupted in packet parser thread shutdown join.");

bundles/org.openhab.binding.denonmarantz/src/main/java/org/openhab/binding/denonmarantz/internal/connector/http/DenonMarantzHttpConnector.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import javax.xml.stream.XMLStreamReader;
3434
import javax.xml.stream.util.StreamReaderDelegate;
3535

36-
import org.apache.commons.io.IOUtils;
3736
import org.eclipse.jdt.annotation.Nullable;
3837
import org.eclipse.jetty.client.HttpClient;
3938
import org.eclipse.jetty.client.api.Response;
@@ -310,7 +309,8 @@ private <T> T getDocument(String uri, Class<T> response) throws IOException {
310309
XMLInputFactory xif = XMLInputFactory.newInstance();
311310
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
312311
xif.setProperty(XMLInputFactory.SUPPORT_DTD, false);
313-
XMLStreamReader xsr = xif.createXMLStreamReader(IOUtils.toInputStream(result));
312+
XMLStreamReader xsr = xif
313+
.createXMLStreamReader(new ByteArrayInputStream(result.getBytes(StandardCharsets.UTF_8)));
314314
xsr = new PropertyRenamerDelegate(xsr);
315315

316316
@SuppressWarnings("unchecked")
@@ -344,7 +344,8 @@ private <T, S> T postDocument(String uri, Class<T> response, S request) throws I
344344
JAXBContext jcResponse = JAXBContext.newInstance(response);
345345

346346
@SuppressWarnings("unchecked")
347-
T obj = (T) jcResponse.createUnmarshaller().unmarshal(IOUtils.toInputStream(result));
347+
T obj = (T) jcResponse.createUnmarshaller()
348+
.unmarshal(new ByteArrayInputStream(result.getBytes(StandardCharsets.UTF_8)));
348349

349350
return obj;
350351
}

bundles/org.openhab.binding.digitalstrom/src/main/java/org/openhab/binding/digitalstrom/internal/lib/serverconnection/impl/HttpTransportImpl.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package org.openhab.binding.digitalstrom.internal.lib.serverconnection.impl;
1414

15+
import java.io.ByteArrayInputStream;
1516
import java.io.File;
1617
import java.io.FileInputStream;
1718
import java.io.FileNotFoundException;
@@ -42,7 +43,6 @@
4243
import javax.net.ssl.TrustManager;
4344
import javax.net.ssl.X509TrustManager;
4445

45-
import org.apache.commons.io.IOUtils;
4646
import org.apache.commons.lang3.StringUtils;
4747
import org.openhab.binding.digitalstrom.internal.lib.config.Config;
4848
import org.openhab.binding.digitalstrom.internal.lib.manager.ConnectionManager;
@@ -459,7 +459,7 @@ public String writePEMCertFile(String path) {
459459
correctedPath = correctedPath + "/";
460460
}
461461
}
462-
InputStream certInputStream = IOUtils.toInputStream(cert);
462+
InputStream certInputStream = new ByteArrayInputStream(cert.getBytes(StandardCharsets.UTF_8));
463463
X509Certificate trustedCert;
464464
try {
465465
trustedCert = (X509Certificate) CertificateFactory.getInstance("X.509")
@@ -488,7 +488,7 @@ public String writePEMCertFile(String path) {
488488
private SSLSocketFactory generateSSLContextFromPEMCertString(String pemCert) {
489489
if (pemCert != null && !pemCert.isBlank() && pemCert.startsWith(BEGIN_CERT)) {
490490
try {
491-
InputStream certInputStream = IOUtils.toInputStream(pemCert);
491+
InputStream certInputStream = new ByteArrayInputStream(pemCert.getBytes(StandardCharsets.UTF_8));
492492
final X509Certificate trustedCert = (X509Certificate) CertificateFactory.getInstance("X.509")
493493
.generateCertificate(certInputStream);
494494

bundles/org.openhab.binding.ihc/src/main/java/org/openhab/binding/ihc/internal/ws/IhcClient.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import java.io.ByteArrayOutputStream;
1717
import java.io.IOException;
1818
import java.io.InputStreamReader;
19+
import java.io.OutputStreamWriter;
1920
import java.net.SocketTimeoutException;
21+
import java.nio.charset.StandardCharsets;
2022
import java.util.ArrayList;
2123
import java.util.Base64;
2224
import java.util.HashMap;
@@ -25,7 +27,6 @@
2527
import java.util.Set;
2628
import java.util.zip.GZIPInputStream;
2729

28-
import org.apache.commons.io.IOUtils;
2930
import org.openhab.binding.ihc.internal.ws.datatypes.WSControllerState;
3031
import org.openhab.binding.ihc.internal.ws.datatypes.WSFile;
3132
import org.openhab.binding.ihc.internal.ws.datatypes.WSLoginResult;
@@ -325,10 +326,13 @@ public byte[] getProjectFileFromController() throws IhcExecption {
325326
}
326327
byte[] decodedBytes = Base64.getDecoder().decode(byteStream.toString());
327328
logger.debug("File size after base64 encoding: {} bytes", decodedBytes.length);
328-
try (GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(decodedBytes))) {
329-
try (InputStreamReader in = new InputStreamReader(gzis, "ISO-8859-1")) {
330-
return IOUtils.toByteArray(in, "ISO-8859-1");
331-
}
329+
try (GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(decodedBytes));
330+
InputStreamReader reader = new InputStreamReader(gzis, StandardCharsets.ISO_8859_1);
331+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
332+
OutputStreamWriter writer = new OutputStreamWriter(baos)) {
333+
reader.transferTo(writer);
334+
writer.flush();
335+
return baos.toByteArray();
332336
}
333337
}
334338
} catch (IOException | IllegalArgumentException e) {

bundles/org.openhab.binding.ihc/src/main/java/org/openhab/binding/ihc/internal/ws/projectfile/ProjectFileUtils.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import java.io.ByteArrayInputStream;
1616
import java.io.File;
17+
import java.io.FileOutputStream;
1718
import java.io.IOException;
1819
import java.util.ArrayList;
1920
import java.util.HashMap;
@@ -24,7 +25,6 @@
2425
import javax.xml.parsers.DocumentBuilderFactory;
2526
import javax.xml.parsers.ParserConfigurationException;
2627

27-
import org.apache.commons.io.FileUtils;
2828
import org.openhab.binding.ihc.internal.ws.datatypes.WSProjectInfo;
2929
import org.openhab.binding.ihc.internal.ws.exeptions.IhcExecption;
3030
import org.slf4j.Logger;
@@ -76,7 +76,10 @@ public static Document readFromFile(String filePath) throws IhcExecption {
7676
*/
7777
public static void saveToFile(String filePath, byte[] data) throws IhcExecption {
7878
try {
79-
FileUtils.writeByteArrayToFile(new File(filePath), data);
79+
try (FileOutputStream stream = new FileOutputStream(filePath)) {
80+
stream.write(data);
81+
stream.flush();
82+
}
8083
} catch (IOException e) {
8184
throw new IhcExecption(e);
8285
}

bundles/org.openhab.binding.logreader/pom.xml

+9
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,13 @@
3636
</plugins>
3737
</build>
3838

39+
<dependencies>
40+
<dependency>
41+
<groupId>commons-io</groupId>
42+
<artifactId>commons-io</artifactId>
43+
<version>2.8.0</version>
44+
<scope>compile</scope>
45+
</dependency>
46+
</dependencies>
47+
3948
</project>

bundles/org.openhab.binding.minecraft/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
</properties>
2020

2121
<dependencies>
22+
<dependency>
23+
<groupId>commons-codec</groupId>
24+
<artifactId>commons-codec</artifactId>
25+
<version>1.15</version>
26+
</dependency>
2227
<dependency>
2328
<groupId>io.reactivex</groupId>
2429
<artifactId>rxjava</artifactId>

bundles/org.openhab.binding.onkyo/src/main/java/org/openhab/binding/onkyo/internal/OnkyoAlbumArt.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.net.URLConnection;
2020
import java.util.Arrays;
2121

22-
import org.apache.commons.io.IOUtils;
2322
import org.eclipse.jdt.annotation.NonNull;
2423
import org.openhab.core.util.HexUtils;
2524
import org.slf4j.Logger;
@@ -182,11 +181,8 @@ private byte[] downloadAlbumArt(String albumArtUrl) {
182181
try {
183182
URL url = new URL(albumArtUrl);
184183
URLConnection connection = url.openConnection();
185-
InputStream inputStream = connection.getInputStream();
186-
try {
187-
return IOUtils.toByteArray(inputStream);
188-
} finally {
189-
IOUtils.closeQuietly(inputStream);
184+
try (InputStream inputStream = connection.getInputStream()) {
185+
return inputStream.readAllBytes();
190186
}
191187
} catch (MalformedURLException e) {
192188
logger.warn("Album Art download failed from url '{}', reason {}", albumArtUrl, e.getMessage());

bundles/org.openhab.binding.onkyo/src/main/java/org/openhab/binding/onkyo/internal/OnkyoConnection.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Timer;
2525
import java.util.TimerTask;
2626

27-
import org.apache.commons.io.IOUtils;
2827
import org.openhab.binding.onkyo.internal.eiscp.EiscpCommand;
2928
import org.openhab.binding.onkyo.internal.eiscp.EiscpException;
3029
import org.openhab.binding.onkyo.internal.eiscp.EiscpMessage;
@@ -222,17 +221,26 @@ private boolean closeSocket() {
222221
logger.debug("closed connection tester!");
223222
}
224223
if (inStream != null) {
225-
IOUtils.closeQuietly(inStream);
224+
try {
225+
inStream.close();
226+
} catch (IOException e) {
227+
}
226228
inStream = null;
227229
logger.debug("closed input stream!");
228230
}
229231
if (outStream != null) {
230-
IOUtils.closeQuietly(outStream);
232+
try {
233+
outStream.close();
234+
} catch (IOException e) {
235+
}
231236
outStream = null;
232237
logger.debug("closed output stream!");
233238
}
234239
if (eiscpSocket != null) {
235-
IOUtils.closeQuietly(eiscpSocket);
240+
try {
241+
eiscpSocket.close();
242+
} catch (IOException e) {
243+
}
236244
eiscpSocket = null;
237245
logger.debug("closed socket!");
238246
}

bundles/org.openhab.binding.sonos/src/main/java/org/openhab/binding/sonos/internal/SonosAudioSink.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.stream.Collectors;
2020
import java.util.stream.Stream;
2121

22-
import org.apache.commons.io.IOUtils;
2322
import org.eclipse.jdt.annotation.NonNullByDefault;
2423
import org.eclipse.jdt.annotation.Nullable;
2524
import org.openhab.binding.sonos.internal.handler.ZonePlayerHandler;
@@ -118,7 +117,10 @@ public void process(@Nullable AudioStream audioStream)
118117
logger.warn("We do not have any callback url, so Sonos cannot play the audio stream!");
119118
}
120119
} else {
121-
IOUtils.closeQuietly(audioStream);
120+
try {
121+
audioStream.close();
122+
} catch (IOException e) {
123+
}
122124
throw new UnsupportedAudioStreamException(
123125
"Sonos can only handle FixedLengthAudioStreams and URLAudioStreams.", audioStream.getClass());
124126
// Instead of throwing an exception, we could ourselves try to wrap it into a

bundles/org.openhab.io.neeo/src/main/java/org/openhab/io/neeo/internal/AbstractServlet.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
package org.openhab.io.neeo.internal;
1414

1515
import java.io.IOException;
16+
import java.nio.charset.StandardCharsets;
1617
import java.util.ArrayList;
1718
import java.util.List;
1819
import java.util.Objects;
@@ -22,7 +23,6 @@
2223
import javax.servlet.http.HttpServletRequest;
2324
import javax.servlet.http.HttpServletResponse;
2425

25-
import org.apache.commons.io.IOUtils;
2626
import org.apache.commons.lang3.StringUtils;
2727
import org.eclipse.jdt.annotation.NonNullByDefault;
2828
import org.eclipse.jdt.annotation.Nullable;
@@ -136,7 +136,8 @@ protected void doPost(@Nullable HttpServletRequest req, @Nullable HttpServletRes
136136

137137
if (logger.isDebugEnabled()) {
138138
req.getReader().mark(150000);
139-
logger.debug("doPost: {} with {}", getFullURL(req), IOUtils.toString(req.getReader()));
139+
logger.debug("doPost: {} with {}", getFullURL(req),
140+
new String(req.getInputStream().readAllBytes(), StandardCharsets.UTF_8));
140141
req.getReader().reset();
141142
}
142143

bundles/org.openhab.io.neeo/src/main/java/org/openhab/io/neeo/internal/net/HttpResponse.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import javax.ws.rs.core.Response;
2323

24-
import org.apache.commons.io.IOUtils;
2524
import org.eclipse.jdt.annotation.NonNullByDefault;
2625
import org.eclipse.jdt.annotation.Nullable;
2726

@@ -58,8 +57,7 @@ public class HttpResponse {
5857
httpReason = response.getStatusInfo().getReasonPhrase();
5958

6059
if (response.hasEntity()) {
61-
InputStream is = response.readEntity(InputStream.class);
62-
contents = IOUtils.toByteArray(is);
60+
contents = response.readEntity(InputStream.class).readAllBytes();
6361
} else {
6462
contents = null;
6563
}

0 commit comments

Comments
 (0)