From ac9918a78790eb0fcc950550728c94c03c8b57b8 Mon Sep 17 00:00:00 2001 From: Oleksandr Krutko Date: Wed, 25 Sep 2024 23:42:40 +0300 Subject: [PATCH] simplify write() method, remove unnecessary Exception Signed-off-by: Oleksandr Krutko --- .../jetty/client/PathResponseListener.java | 64 ++++--------------- .../org/eclipse/jetty/client/Request.java | 2 +- .../jetty/client/transport/HttpRequest.java | 2 +- 3 files changed, 16 insertions(+), 52 deletions(-) diff --git a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/PathResponseListener.java b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/PathResponseListener.java index 402f1c5dc19f..ecaa7a92be59 100644 --- a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/PathResponseListener.java +++ b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/PathResponseListener.java @@ -13,13 +13,13 @@ package org.eclipse.jetty.client; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.file.FileAlreadyExistsException; +import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardOpenOption; import java.util.concurrent.CompletableFuture; @@ -51,33 +51,21 @@ public class PathResponseListener extends CompletableFuture implements { private static final Logger LOG = LoggerFactory.getLogger(InputStreamResponseListener.class); - private Path path; - private Throwable failure; + private final Path path; private FileChannel fileOut; - private int bytesWrite; - public PathResponseListener(Path path, boolean overwrite) throws FileNotFoundException, IOException, FileAlreadyExistsException + public PathResponseListener(Path path, boolean overwrite) throws IOException { this.path = path; // Throws the exception if file can't be overwritten // otherwise truncate it. - if (this.path.toFile().exists() && !overwrite) + if (Files.exists(path) && !overwrite) { throw new FileAlreadyExistsException("File can't be overwritten"); } - try - { - fileOut = FileChannel.open(this.path, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); - } - catch (IOException e) - { - if (LOG.isDebugEnabled()) - LOG.debug("Unable to instantiate object", e); - - throw e; - } + fileOut = FileChannel.open(this.path, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); } @Override @@ -86,7 +74,7 @@ public void onHeaders(Response response) if (response.getStatus() != HttpStatus.OK_200) { this.cancel(true); - throw new HttpResponseException(String.format("HTTP status code of this response %d", response.getStatus()), response); + response.abort(new HttpResponseException(String.format("HTTP status code of response %d", response.getStatus()), response)); } } @@ -95,25 +83,25 @@ public void onContent(Response response, ByteBuffer content) { try { - this.bytesWrite += this.write(content).get(); + var bytesWritten = fileOut.write(content); if (LOG.isDebugEnabled()) - LOG.debug("%d bytes written", bytesWrite); + LOG.debug("%d bytes written", bytesWritten); } - catch (InterruptedException | ExecutionException e) + catch (IOException e) { - e.printStackTrace(); + response.abort(e); } } @Override public void onComplete(Result result) { - if (result.isFailed() && this.failure == null) + if (result.isFailed()) { if (LOG.isDebugEnabled()) - LOG.debug("Result failure", failure); + LOG.debug("Result failure", result.getFailure()); - this.cancel(true); + completeExceptionally(result.getFailure()); return; } @@ -126,7 +114,7 @@ public static CompletableFuture write(Request request, Path path, boolean { try { - if (path.toFile().exists() && !overwrite) + if (Files.exists(path) && !overwrite) { throw new FileAlreadyExistsException("File can't be overwritten"); } @@ -169,28 +157,4 @@ public static CompletableFuture write(Request request, Path path, boolean return future; } - - private CompletableFuture write(ByteBuffer content) - { - return CompletableFuture.supplyAsync(() -> - { - int bytesWritten = 0; - try - { - bytesWritten += fileOut.write(content); - } - catch (IOException e) - { - if (LOG.isDebugEnabled()) - LOG.debug("Unable to write file", e); - - throw new CompletionException(e); - } - - if (LOG.isDebugEnabled()) - LOG.debug("%d bytes have been written into a file", bytesWritten); - - return bytesWritten; - }); - } } diff --git a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/Request.java b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/Request.java index 569249de5fa8..4a7982d801a1 100644 --- a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/Request.java +++ b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/Request.java @@ -507,7 +507,7 @@ default Transport getTransport() * @param listener the listener that receives response events */ void send(Response.CompleteListener listener); - + /** * Attempts to abort the send of this request. * diff --git a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/transport/HttpRequest.java b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/transport/HttpRequest.java index 6ad6dfd2a6c3..4f1162d14ed8 100644 --- a/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/transport/HttpRequest.java +++ b/jetty-core/jetty-client/src/main/java/org/eclipse/jetty/client/transport/HttpRequest.java @@ -744,7 +744,7 @@ public void send(Response.CompleteListener listener) Destination destination = client.resolveDestination(this); destination.send(this, listener); } - + void sendAsync(HttpDestination destination, Response.CompleteListener listener) { if (listener != null)