Reduce dependency on commons-io and commons-codec (#10614)

Signed-off-by: Wouter Born <github@maindrain.net>
This commit is contained in:
Wouter Born
2021-04-30 16:53:44 +02:00
committed by GitHub
parent 02b4943a11
commit e6d8dfb7e1
20 changed files with 175 additions and 124 deletions

View File

@@ -30,7 +30,6 @@ import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.IOUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.bluetooth.AbstractBluetoothBridgeHandler;
@@ -411,10 +410,16 @@ public class BlueGigaBridgeHandler extends AbstractBluetoothBridgeHandler<BlueGi
// Ignore all as RXTX seems to send arbitrary exceptions when BlueGiga module is detached
} finally {
outputStream.ifPresent(output -> {
IOUtils.closeQuietly(output);
try {
output.close();
} catch (IOException e) {
}
});
inputStream.ifPresent(input -> {
IOUtils.closeQuietly(input);
try {
input.close();
} catch (IOException e) {
}
});
sp.close();
logger.debug("Closed serial port.");

View File

@@ -12,13 +12,13 @@
*/
package org.openhab.binding.bluetooth.bluegiga.internal;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import org.apache.commons.io.IOUtils;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.bluetooth.bluegiga.internal.command.gap.BlueGigaEndProcedureCommand;
import org.slf4j.Logger;
@@ -96,7 +96,7 @@ public class BlueGigaSerialHandler {
// Wait BlueGiga controller have stopped all activity
Thread.sleep(100);
logger.trace("Bytes available: {}", inputStream.available());
IOUtils.skipFully(inputStream, inputStream.available());
skipFully(inputStream, inputStream.available());
} catch (InterruptedException e) {
close = true;
} catch (IOException e) {
@@ -105,6 +105,28 @@ public class BlueGigaSerialHandler {
logger.trace("Flush done");
}
private void skipFully(final InputStream input, final long toSkip) throws IOException {
if (toSkip < 0) {
throw new IllegalArgumentException("Bytes to skip must not be negative: " + toSkip);
}
long remain = toSkip;
final byte[] byteArray = new byte[8192];
while (remain > 0) {
final long n = input.read(byteArray, 0, (int) Math.min(remain, byteArray.length));
if (n < 0) { // EOF
break;
}
remain -= n;
}
long skipped = toSkip - remain;
if (skipped != toSkip) {
throw new EOFException("Bytes to skip: " + toSkip + " actual: " + skipped);
}
}
/**
* Requests parser thread to shutdown. Waits forever while the parser thread is getting shut down.
*/
@@ -123,8 +145,14 @@ public class BlueGigaSerialHandler {
parserThread.interrupt();
// Give a fair chance to shutdown nicely
Thread.sleep(50);
IOUtils.closeQuietly(outputStream);
IOUtils.closeQuietly(inputStream);
try {
outputStream.close();
} catch (IOException e) {
}
try {
inputStream.close();
} catch (IOException e) {
}
parserThread.join(0);
} catch (InterruptedException e) {
logger.warn("Interrupted in packet parser thread shutdown join.");