From b04582745b715e80a11cd65eb7e1ab0ddb0b8f79 Mon Sep 17 00:00:00 2001
From: OZGCloud <ozgcloud@mgm-tp.com>
Date: Mon, 18 Nov 2024 08:53:06 +0100
Subject: [PATCH] OZG-7143 Remove OnReadyHandler

---
 .../GrpcBinaryFileServerDownloader.java       | 39 +++++++------------
 1 file changed, 15 insertions(+), 24 deletions(-)

diff --git a/ozgcloud-common-lib/src/main/java/de/ozgcloud/common/binaryfile/GrpcBinaryFileServerDownloader.java b/ozgcloud-common-lib/src/main/java/de/ozgcloud/common/binaryfile/GrpcBinaryFileServerDownloader.java
index 70020e1..3128ba0 100644
--- a/ozgcloud-common-lib/src/main/java/de/ozgcloud/common/binaryfile/GrpcBinaryFileServerDownloader.java
+++ b/ozgcloud-common-lib/src/main/java/de/ozgcloud/common/binaryfile/GrpcBinaryFileServerDownloader.java
@@ -23,15 +23,6 @@
  */
 package de.ozgcloud.common.binaryfile;
 
-import com.google.protobuf.ByteString;
-import de.ozgcloud.common.errorhandling.TechnicalException;
-import io.grpc.Context;
-import io.grpc.stub.CallStreamObserver;
-import lombok.Builder;
-import lombok.extern.log4j.Log4j2;
-import org.apache.commons.io.IOUtils;
-import org.springframework.core.task.TaskExecutor;
-
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.PipedInputStream;
@@ -40,6 +31,17 @@ import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.function.Consumer;
 import java.util.function.Function;
 
+import org.apache.commons.io.IOUtils;
+import org.springframework.core.task.TaskExecutor;
+
+import com.google.protobuf.ByteString;
+
+import de.ozgcloud.common.errorhandling.TechnicalException;
+import io.grpc.Context;
+import io.grpc.stub.CallStreamObserver;
+import lombok.Builder;
+import lombok.extern.log4j.Log4j2;
+
 @Log4j2
 public class GrpcBinaryFileServerDownloader<T> {
 
@@ -52,7 +54,6 @@ public class GrpcBinaryFileServerDownloader<T> {
 
 	private final byte[] buffer = new byte[GrpcBinaryFileServerDownloader.CHUNK_SIZE];
 	private final AtomicBoolean started = new AtomicBoolean(false);
-	private final AtomicBoolean downloadInProgress = new AtomicBoolean(false);
 
 	private PipedInputStream inputStream;
 	private PipedOutputStream outputStream;
@@ -79,7 +80,7 @@ public class GrpcBinaryFileServerDownloader<T> {
 	void doStart() {
 		handleSafety(this::setupStreams);
 		taskExecutor.execute(Context.current().wrap(this::startDownload));
-		callObserver.setOnReadyHandler(this::onReadyHandler);
+		sendChunks();
 	}
 
 	void setupStreams() throws IOException {
@@ -93,31 +94,21 @@ public class GrpcBinaryFileServerDownloader<T> {
 	}
 
 	void doDownload() throws IOException {
-		downloadInProgress.set(true);
 		downloadConsumer.accept(outputStream);
-		downloadInProgress.set(false);
 		outputStream.close();
 	}
 
-	synchronized void onReadyHandler() {
-		if (callObserver.isReady()) {
-			sendChunks();
-		}
-	}
-
 	void sendChunks() {
 		handleSafety(this::doSendChunks);
 	}
 
 	void doSendChunks() throws IOException {
 		int bytesRead;
-		while (callObserver.isReady() && (bytesRead = inputStream.read(buffer)) != -1) {
+		while ((bytesRead = inputStream.read(buffer)) != -1) {
 			callObserver.onNext(chunkBuilder.apply(ByteString.copyFrom(buffer, 0, bytesRead)));
 		}
-		if (!downloadInProgress.get()) {
-			inputStream.close();
-			callObserver.onCompleted();
-		}
+		inputStream.close();
+		callObserver.onCompleted();
 	}
 
 	void handleSafety(ExceptionalRunnable runnable) {
-- 
GitLab