Skip to content

Commit

Permalink
[java] replaced usage of Guavas ByteStreams with native Java 11 methods
Browse files Browse the repository at this point in the history
  • Loading branch information
joerg1985 committed Oct 11, 2023
1 parent 1e6e5ec commit 27c7fdb
Show file tree
Hide file tree
Showing 14 changed files with 24 additions and 34 deletions.
4 changes: 2 additions & 2 deletions java/src/org/openqa/selenium/chrome/ChromeDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -295,7 +295,7 @@ protected List<String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions java/src/org/openqa/selenium/devtools/v116/v116Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -188,7 +187,7 @@ protected Command<Void> continueWithoutModification(RequestPaused pausedRequest)
protected Command<Void> 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);
}
Expand All @@ -212,7 +211,7 @@ protected Command<Void> 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();
}
Expand Down
5 changes: 2 additions & 3 deletions java/src/org/openqa/selenium/devtools/v117/v117Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -183,7 +182,7 @@ protected Command<Void> continueWithoutModification(RequestPaused pausedRequest)
protected Command<Void> 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);
}
Expand All @@ -207,7 +206,7 @@ protected Command<Void> 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();
}
Expand Down
5 changes: 2 additions & 3 deletions java/src/org/openqa/selenium/devtools/v118/v118Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -183,7 +182,7 @@ protected Command<Void> continueWithoutModification(RequestPaused pausedRequest)
protected Command<Void> 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);
}
Expand All @@ -207,7 +206,7 @@ protected Command<Void> 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();
}
Expand Down
5 changes: 2 additions & 3 deletions java/src/org/openqa/selenium/devtools/v85/V85Network.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -192,7 +191,7 @@ protected Command<Void> continueWithoutModification(RequestPaused pausedRequest)
protected Command<Void> 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);
}
Expand All @@ -215,7 +214,7 @@ protected Command<Void> 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();
}
Expand Down
4 changes: 2 additions & 2 deletions java/src/org/openqa/selenium/edge/EdgeDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -297,7 +297,7 @@ protected List<String> 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) {
Expand Down
3 changes: 1 addition & 2 deletions java/src/org/openqa/selenium/grid/web/JarFileResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -113,7 +112,7 @@ public Optional<byte[]> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
5 changes: 2 additions & 3 deletions java/src/org/openqa/selenium/remote/http/Contents.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,7 +74,7 @@ public static byte[] bytes(Supplier<InputStream> 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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions java/src/org/openqa/selenium/safari/SafariDriverService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -164,7 +164,7 @@ protected List<String> createArgs() {
protected SafariDriverService createDriverService(
File exe, int port, Duration timeout, List<String> args, Map<String, String> environment) {
try {
withLogOutput(ByteStreams.nullOutputStream());
withLogOutput(OutputStream.nullOutputStream());
return new SafariDriverService(exe, port, timeout, args, environment);
} catch (IOException e) {
throw new WebDriverException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -167,7 +167,7 @@ protected List<String> createArgs() {
protected SafariTechPreviewDriverService createDriverService(
File exe, int port, Duration timeout, List<String> args, Map<String, String> environment) {
try {
withLogOutput(ByteStreams.nullOutputStream());
withLogOutput(OutputStream.nullOutputStream());
return new SafariTechPreviewDriverService(exe, port, timeout, args, environment);
} catch (IOException e) {
throw new WebDriverException(e);
Expand Down

0 comments on commit 27c7fdb

Please sign in to comment.