From 27c7fdbef8653ab1727548be76f5b8b53086462d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Sautter?= Date: Wed, 11 Oct 2023 19:53:32 +0200 Subject: [PATCH] [java] replaced usage of Guavas ByteStreams with native Java 11 methods --- java/src/org/openqa/selenium/chrome/ChromeDriverService.java | 4 ++-- .../src/org/openqa/selenium/devtools/CdpClientGenerator.java | 3 +-- java/src/org/openqa/selenium/devtools/v116/v116Network.java | 5 ++--- java/src/org/openqa/selenium/devtools/v117/v117Network.java | 5 ++--- java/src/org/openqa/selenium/devtools/v118/v118Network.java | 5 ++--- java/src/org/openqa/selenium/devtools/v85/V85Network.java | 5 ++--- java/src/org/openqa/selenium/edge/EdgeDriverService.java | 4 ++-- java/src/org/openqa/selenium/grid/web/JarFileResource.java | 3 +-- .../org/openqa/selenium/netty/server/ResponseConverter.java | 3 +-- java/src/org/openqa/selenium/remote/http/Contents.java | 5 ++--- .../selenium/remote/http/netty/NettyDomainSocketClient.java | 3 +-- .../org/openqa/selenium/remote/service/DriverService.java | 5 ++--- java/src/org/openqa/selenium/safari/SafariDriverService.java | 4 ++-- .../selenium/safari/SafariTechPreviewDriverService.java | 4 ++-- 14 files changed, 24 insertions(+), 34 deletions(-) diff --git a/java/src/org/openqa/selenium/chrome/ChromeDriverService.java b/java/src/org/openqa/selenium/chrome/ChromeDriverService.java index cca32a3ace22f..7aba516bf86b0 100644 --- a/java/src/org/openqa/selenium/chrome/ChromeDriverService.java +++ b/java/src/org/openqa/selenium/chrome/ChromeDriverService.java @@ -22,9 +22,9 @@ import static org.openqa.selenium.remote.Browser.CHROME; import com.google.auto.service.AutoService; -import com.google.common.io.ByteStreams; import java.io.File; import java.io.IOException; +import java.io.OutputStream; import java.time.Duration; import java.util.ArrayList; import java.util.HashMap; @@ -295,7 +295,7 @@ protected List createArgs() { args.add("--append-log"); } withLogOutput( - ByteStreams.nullOutputStream()); // Do not overwrite log file in getLogOutput() + OutputStream.nullOutputStream()); // Do not overwrite log file in getLogOutput() } if (logLevel != null) { diff --git a/java/src/org/openqa/selenium/devtools/CdpClientGenerator.java b/java/src/org/openqa/selenium/devtools/CdpClientGenerator.java index e12235edcd0f2..c68fb15e2584d 100644 --- a/java/src/org/openqa/selenium/devtools/CdpClientGenerator.java +++ b/java/src/org/openqa/selenium/devtools/CdpClientGenerator.java @@ -29,7 +29,6 @@ import com.github.javaparser.ast.body.TypeDeclaration; import com.github.javaparser.ast.stmt.BlockStmt; import com.google.common.collect.ImmutableMap; -import com.google.common.io.ByteStreams; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -102,7 +101,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) JarEntry entry = new JarEntry(devtoolsDir + relative); jos.putNextEntry(entry); try (InputStream is = Files.newInputStream(file)) { - ByteStreams.copy(is, jos); + is.transferTo(jos); } jos.closeEntry(); return CONTINUE; diff --git a/java/src/org/openqa/selenium/devtools/v116/v116Network.java b/java/src/org/openqa/selenium/devtools/v116/v116Network.java index 24935d6bb97f5..d76f6b8f1f2f6 100644 --- a/java/src/org/openqa/selenium/devtools/v116/v116Network.java +++ b/java/src/org/openqa/selenium/devtools/v116/v116Network.java @@ -20,7 +20,6 @@ import static java.net.HttpURLConnection.HTTP_OK; import com.google.common.collect.ImmutableList; -import com.google.common.io.ByteStreams; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; @@ -188,7 +187,7 @@ protected Command continueWithoutModification(RequestPaused pausedRequest) protected Command continueRequest(RequestPaused pausedReq, HttpRequest req) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (InputStream is = req.getContent().get()) { - ByteStreams.copy(is, bos); + is.transferTo(bos); } catch (IOException e) { return continueWithoutModification(pausedReq); } @@ -212,7 +211,7 @@ protected Command fulfillRequest(RequestPaused pausedReq, HttpResponse res ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (InputStream is = res.getContent().get()) { - ByteStreams.copy(is, bos); + is.transferTo(bos); } catch (IOException e) { bos.reset(); } diff --git a/java/src/org/openqa/selenium/devtools/v117/v117Network.java b/java/src/org/openqa/selenium/devtools/v117/v117Network.java index cb3742002c0b5..cd87374285315 100644 --- a/java/src/org/openqa/selenium/devtools/v117/v117Network.java +++ b/java/src/org/openqa/selenium/devtools/v117/v117Network.java @@ -20,7 +20,6 @@ import static java.net.HttpURLConnection.HTTP_OK; import com.google.common.collect.ImmutableList; -import com.google.common.io.ByteStreams; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; @@ -183,7 +182,7 @@ protected Command continueWithoutModification(RequestPaused pausedRequest) protected Command continueRequest(RequestPaused pausedReq, HttpRequest req) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (InputStream is = req.getContent().get()) { - ByteStreams.copy(is, bos); + is.transferTo(bos); } catch (IOException e) { return continueWithoutModification(pausedReq); } @@ -207,7 +206,7 @@ protected Command fulfillRequest(RequestPaused pausedReq, HttpResponse res ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (InputStream is = res.getContent().get()) { - ByteStreams.copy(is, bos); + is.transferTo(bos); } catch (IOException e) { bos.reset(); } diff --git a/java/src/org/openqa/selenium/devtools/v118/v118Network.java b/java/src/org/openqa/selenium/devtools/v118/v118Network.java index 332335374402d..cbd4964dbb41b 100644 --- a/java/src/org/openqa/selenium/devtools/v118/v118Network.java +++ b/java/src/org/openqa/selenium/devtools/v118/v118Network.java @@ -20,7 +20,6 @@ import static java.net.HttpURLConnection.HTTP_OK; import com.google.common.collect.ImmutableList; -import com.google.common.io.ByteStreams; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; @@ -183,7 +182,7 @@ protected Command continueWithoutModification(RequestPaused pausedRequest) protected Command continueRequest(RequestPaused pausedReq, HttpRequest req) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (InputStream is = req.getContent().get()) { - ByteStreams.copy(is, bos); + is.transferTo(bos); } catch (IOException e) { return continueWithoutModification(pausedReq); } @@ -207,7 +206,7 @@ protected Command fulfillRequest(RequestPaused pausedReq, HttpResponse res ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (InputStream is = res.getContent().get()) { - ByteStreams.copy(is, bos); + is.transferTo(bos); } catch (IOException e) { bos.reset(); } diff --git a/java/src/org/openqa/selenium/devtools/v85/V85Network.java b/java/src/org/openqa/selenium/devtools/v85/V85Network.java index e365534045ed1..ec9f507666456 100644 --- a/java/src/org/openqa/selenium/devtools/v85/V85Network.java +++ b/java/src/org/openqa/selenium/devtools/v85/V85Network.java @@ -20,7 +20,6 @@ import static java.net.HttpURLConnection.HTTP_OK; import com.google.common.collect.ImmutableList; -import com.google.common.io.ByteStreams; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; @@ -192,7 +191,7 @@ protected Command continueWithoutModification(RequestPaused pausedRequest) protected Command continueRequest(RequestPaused pausedReq, HttpRequest req) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (InputStream is = req.getContent().get()) { - ByteStreams.copy(is, bos); + is.transferTo(bos); } catch (IOException e) { return continueWithoutModification(pausedReq); } @@ -215,7 +214,7 @@ protected Command fulfillRequest(RequestPaused pausedReq, HttpResponse res ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (InputStream is = res.getContent().get()) { - ByteStreams.copy(is, bos); + is.transferTo(bos); } catch (IOException e) { bos.reset(); } diff --git a/java/src/org/openqa/selenium/edge/EdgeDriverService.java b/java/src/org/openqa/selenium/edge/EdgeDriverService.java index 030f4c1e8e340..cb0cc51189bdd 100644 --- a/java/src/org/openqa/selenium/edge/EdgeDriverService.java +++ b/java/src/org/openqa/selenium/edge/EdgeDriverService.java @@ -23,9 +23,9 @@ import static org.openqa.selenium.remote.Browser.EDGE; import com.google.auto.service.AutoService; -import com.google.common.io.ByteStreams; import java.io.File; import java.io.IOException; +import java.io.OutputStream; import java.time.Duration; import java.util.ArrayList; import java.util.HashMap; @@ -297,7 +297,7 @@ protected List createArgs() { args.add("--append-log"); } withLogOutput( - ByteStreams.nullOutputStream()); // Do not overwrite log file in getLogOutput() + OutputStream.nullOutputStream()); // Do not overwrite log file in getLogOutput() } if (logLevel != null) { diff --git a/java/src/org/openqa/selenium/grid/web/JarFileResource.java b/java/src/org/openqa/selenium/grid/web/JarFileResource.java index b830279743198..bee53ef725e55 100644 --- a/java/src/org/openqa/selenium/grid/web/JarFileResource.java +++ b/java/src/org/openqa/selenium/grid/web/JarFileResource.java @@ -18,7 +18,6 @@ package org.openqa.selenium.grid.web; import com.google.common.collect.ImmutableSet; -import com.google.common.io.ByteStreams; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; @@ -113,7 +112,7 @@ public Optional read() { try (InputStream is = jarFile.getInputStream(entry); ByteArrayOutputStream bos = new ByteArrayOutputStream()) { - ByteStreams.copy(is, bos); + is.transferTo(bos); return Optional.of(bos.toByteArray()); } catch (IOException e) { throw new UncheckedIOException(e); diff --git a/java/src/org/openqa/selenium/netty/server/ResponseConverter.java b/java/src/org/openqa/selenium/netty/server/ResponseConverter.java index 8bf5ebe9bf76b..fa5c3a9b1bc42 100644 --- a/java/src/org/openqa/selenium/netty/server/ResponseConverter.java +++ b/java/src/org/openqa/selenium/netty/server/ResponseConverter.java @@ -22,7 +22,6 @@ import static io.netty.handler.codec.http.HttpHeaderValues.CHUNKED; import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; -import com.google.common.io.ByteStreams; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; @@ -61,7 +60,7 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) // We may not know how large the response is, but figure it out if we can. byte[] ary = CHUNK_CACHE.get(); InputStream is = seResponse.getContent().get(); - int byteCount = ByteStreams.read(is, ary, 0, ary.length); + int byteCount = is.readNBytes(ary, 0, ary.length); // If there are no bytes left to read, then -1 is returned by read, and this is bad. byteCount = byteCount == -1 ? 0 : byteCount; diff --git a/java/src/org/openqa/selenium/remote/http/Contents.java b/java/src/org/openqa/selenium/remote/http/Contents.java index 4bb9bac16a5f8..2962f229f23dc 100644 --- a/java/src/org/openqa/selenium/remote/http/Contents.java +++ b/java/src/org/openqa/selenium/remote/http/Contents.java @@ -19,7 +19,6 @@ import static java.nio.charset.StandardCharsets.UTF_8; -import com.google.common.io.ByteStreams; import com.google.common.io.FileBackedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -75,7 +74,7 @@ public static byte[] bytes(Supplier supplier) { try (InputStream is = supplier.get(); ByteArrayOutputStream bos = new ByteArrayOutputStream()) { - ByteStreams.copy(is, bos); + is.transferTo(bos); return bos.toByteArray(); } catch (IOException e) { throw new UncheckedIOException(e); @@ -171,7 +170,7 @@ public InputStream get() { if (!initialized) { try (InputStream is = delegate.get()) { this.fos = new FileBackedOutputStream(3 * 1024 * 1024, true); - ByteStreams.copy(is, fos); + is.transferTo(fos); initialized = true; } catch (IOException e) { throw new UncheckedIOException(e); diff --git a/java/src/org/openqa/selenium/remote/http/netty/NettyDomainSocketClient.java b/java/src/org/openqa/selenium/remote/http/netty/NettyDomainSocketClient.java index 8bceb93195b69..a303ef33ae300 100644 --- a/java/src/org/openqa/selenium/remote/http/netty/NettyDomainSocketClient.java +++ b/java/src/org/openqa/selenium/remote/http/netty/NettyDomainSocketClient.java @@ -25,7 +25,6 @@ import com.google.common.base.Joiner; import com.google.common.base.Throwables; -import com.google.common.io.ByteStreams; import io.netty.bootstrap.Bootstrap; import io.netty.buffer.ByteBufInputStream; import io.netty.buffer.Unpooled; @@ -197,7 +196,7 @@ public void channelRead0( try (InputStream is = new ByteBufInputStream(msg.content()); ByteArrayOutputStream bos = new ByteArrayOutputStream()) { - ByteStreams.copy(is, bos); + is.transferTo(bos); res.setContent(bytes(bos.toByteArray())); outRef.set(res); latch.countDown(); diff --git a/java/src/org/openqa/selenium/remote/service/DriverService.java b/java/src/org/openqa/selenium/remote/service/DriverService.java index 270367c7bd477..38f4649273b2e 100644 --- a/java/src/org/openqa/selenium/remote/service/DriverService.java +++ b/java/src/org/openqa/selenium/remote/service/DriverService.java @@ -22,7 +22,6 @@ import static org.openqa.selenium.concurrent.ExecutorServices.shutdownGracefully; import com.google.common.collect.ImmutableMap; -import com.google.common.io.ByteStreams; import java.io.Closeable; import java.io.File; import java.io.FileNotFoundException; @@ -460,7 +459,7 @@ protected OutputStream getLogOutput() { } try { File logFile = getLogFile(); - return logFile == null ? ByteStreams.nullOutputStream() : new FileOutputStream(logFile); + return logFile == null ? OutputStream.nullOutputStream() : new FileOutputStream(logFile); } catch (FileNotFoundException e) { throw new RuntimeException(e); } @@ -480,7 +479,7 @@ protected void parseLogOutput(String logProperty) { withLogOutput(System.err); break; case LOG_NULL: - withLogOutput(ByteStreams.nullOutputStream()); + withLogOutput(OutputStream.nullOutputStream()); break; default: withLogFile(new File(logLocation)); diff --git a/java/src/org/openqa/selenium/safari/SafariDriverService.java b/java/src/org/openqa/selenium/safari/SafariDriverService.java index 493501188600a..32979c6ed5aba 100644 --- a/java/src/org/openqa/selenium/safari/SafariDriverService.java +++ b/java/src/org/openqa/selenium/safari/SafariDriverService.java @@ -23,9 +23,9 @@ import static org.openqa.selenium.remote.Browser.SAFARI; import com.google.auto.service.AutoService; -import com.google.common.io.ByteStreams; import java.io.File; import java.io.IOException; +import java.io.OutputStream; import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; @@ -164,7 +164,7 @@ protected List createArgs() { protected SafariDriverService createDriverService( File exe, int port, Duration timeout, List args, Map environment) { try { - withLogOutput(ByteStreams.nullOutputStream()); + withLogOutput(OutputStream.nullOutputStream()); return new SafariDriverService(exe, port, timeout, args, environment); } catch (IOException e) { throw new WebDriverException(e); diff --git a/java/src/org/openqa/selenium/safari/SafariTechPreviewDriverService.java b/java/src/org/openqa/selenium/safari/SafariTechPreviewDriverService.java index fc636ce75375b..d8a26e134fc28 100644 --- a/java/src/org/openqa/selenium/safari/SafariTechPreviewDriverService.java +++ b/java/src/org/openqa/selenium/safari/SafariTechPreviewDriverService.java @@ -23,9 +23,9 @@ import static org.openqa.selenium.remote.Browser.SAFARI_TECH_PREVIEW; import com.google.auto.service.AutoService; -import com.google.common.io.ByteStreams; import java.io.File; import java.io.IOException; +import java.io.OutputStream; import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; @@ -167,7 +167,7 @@ protected List createArgs() { protected SafariTechPreviewDriverService createDriverService( File exe, int port, Duration timeout, List args, Map environment) { try { - withLogOutput(ByteStreams.nullOutputStream()); + withLogOutput(OutputStream.nullOutputStream()); return new SafariTechPreviewDriverService(exe, port, timeout, args, environment); } catch (IOException e) { throw new WebDriverException(e);