From 573d513ada6cdecfce7ec55e2c91e8cd1f1751dc Mon Sep 17 00:00:00 2001 From: Pascal Essiembre Date: Sat, 19 Aug 2023 01:21:44 -0400 Subject: [PATCH] Resolved Commons IO deprecation warnings. --- .../commons/lang/url/URLStreamer.java | 8 +++-- .../commons/lang/exec/ExecUtilTest.java | 35 ++++++++++--------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/norconex/commons/lang/url/URLStreamer.java b/src/main/java/com/norconex/commons/lang/url/URLStreamer.java index fe1cea7d..17d38b7e 100644 --- a/src/main/java/com/norconex/commons/lang/url/URLStreamer.java +++ b/src/main/java/com/norconex/commons/lang/url/URLStreamer.java @@ -147,7 +147,7 @@ public static InputStream stream( LOG.debug("Streaming with proxy: {}:{}", proxy.getHostName(), proxy.getPort()); } - Proxy p = new Proxy(Proxy.Type.HTTP, new InetSocketAddress( + var p = new Proxy(Proxy.Type.HTTP, new InetSocketAddress( proxy.getHostName(), proxy.getPort())); //Authenticator. conn = new URL(url).openConnection(p); @@ -398,10 +398,12 @@ public static String streamToString(HttpURL url) { private static InputStream responseInputStream( URLConnection conn) throws IOException { conn.connect(); - return new AutoCloseInputStream(conn.getInputStream()); + return AutoCloseInputStream.builder() + .setInputStream(conn.getInputStream()) + .get(); } private static String base64BasicAuth(String username, String password) { - String userpass = username + ':' + password; + var userpass = username + ':' + password; return "Basic " + DatatypeConverter.printBase64Binary(userpass.getBytes()); } diff --git a/src/test/java/com/norconex/commons/lang/exec/ExecUtilTest.java b/src/test/java/com/norconex/commons/lang/exec/ExecUtilTest.java index bddc5b61..fd9b96a4 100644 --- a/src/test/java/com/norconex/commons/lang/exec/ExecUtilTest.java +++ b/src/test/java/com/norconex/commons/lang/exec/ExecUtilTest.java @@ -29,14 +29,14 @@ class ExecUtilTest { @Test void testWatchProcessProcess() throws IOException { - Process process = Runtime.getRuntime().exec("java --help"); + var process = Runtime.getRuntime().exec("java --help"); assertThat(ExecUtil.watchProcess(process)).isZero(); } @Test void testWatchProcessProcessInputStreamListener() throws IOException { - StringBuilder stdout = new StringBuilder(); - Process process = Runtime.getRuntime().exec("java -help"); + var stdout = new StringBuilder(); + var process = Runtime.getRuntime().exec("java -help"); assertThat(ExecUtil.watchProcess(process, (t, b, l) -> stdout.append(new String(b)))).isZero(); assertThat(stdout) @@ -46,9 +46,9 @@ void testWatchProcessProcessInputStreamListener() throws IOException { @Test void testWatchProcessProcessInputStreamListenerArray() throws IOException { - StringBuilder stdout1 = new StringBuilder(); - StringBuilder stdout2 = new StringBuilder(); - Process process = Runtime.getRuntime().exec("java -help"); + var stdout1 = new StringBuilder(); + var stdout2 = new StringBuilder(); + var process = Runtime.getRuntime().exec("java -help"); assertThat(ExecUtil.watchProcess( process, new InputStreamListener[] { @@ -65,9 +65,9 @@ void testWatchProcessProcessInputStreamListenerArray() throws IOException { @Test void testWatchProcessProcessInputStreamListenerInputStreamListener() throws IOException { - StringBuilder stdout = new StringBuilder(); - StringBuilder stderr = new StringBuilder(); - Process process = Runtime.getRuntime().exec("java --help"); + var stdout = new StringBuilder(); + var stderr = new StringBuilder(); + var process = Runtime.getRuntime().exec("java --help"); assertThat(ExecUtil.watchProcess( process, (t, b, l) -> stdout.append(new String(b)), @@ -92,9 +92,9 @@ void testWatchProcessProcessInputStreamListenerInputStreamListener() @Test void testWatchProcessAsyncProcessInputStreamListenerInputStreamListener() throws IOException { - StringBuilder stdout = new StringBuilder(); - StringBuilder stderr = new StringBuilder(); - Process process = Runtime.getRuntime().exec("java --help"); + var stdout = new StringBuilder(); + var stderr = new StringBuilder(); + var process = Runtime.getRuntime().exec("java --help"); assertDoesNotThrow(() -> ExecUtil.watchProcessAsync( process, (t, b, l) -> stdout.append(new String(b)), @@ -108,12 +108,15 @@ void testWatchProcessAsyncProcessInputStreamListenerInputStreamListener() // @Test void testWatchProcessAsyncProcessInputStream() throws IOException { - StringBuilder stdout = new StringBuilder(); - StringBuilder stderr = new StringBuilder(); - Process process = Runtime.getRuntime().exec("java --help"); + var stdout = new StringBuilder(); + var stderr = new StringBuilder(); + var process = Runtime.getRuntime().exec("java --help"); assertDoesNotThrow(() -> ExecUtil.watchProcessAsync( process, - new CharSequenceInputStream("java -h\n", UTF_8), + CharSequenceInputStream.builder() + .setCharSequence("java -h\n") + .setCharset(UTF_8) + .get(), new InputStreamListener[] { (t, b, l) -> stdout.append(new String(b)) },