diff src/main/java/org/monetdb/mcl/net/MapiSocket.java @ 557:ce2b616ed22e onclient

Add a cancellation callback to UploadHandler
author Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
date Thu, 16 Sep 2021 15:12:26 +0200 (2021-09-16)
parents 87feb93330a6
children 0fbf1cbde56c
line wrap: on
line diff
--- a/src/main/java/org/monetdb/mcl/net/MapiSocket.java
+++ b/src/main/java/org/monetdb/mcl/net/MapiSocket.java
@@ -1233,6 +1233,7 @@ public class MapiSocket {	/* cannot (yet
 		private boolean serverCancelled = false;
 		private int chunkLeft;
 		private byte[] promptBuffer;
+		private Runnable cancellationCallback = null;
 
 		/** Create an UploadStream with the given chunk size */
 		UploadStream(int chunkSize) {
@@ -1252,6 +1253,12 @@ public class MapiSocket {	/* cannot (yet
 			this(DEFAULT_CHUNK_SIZE);
 		}
 
+		/** Set a callback to be invoked if the server cancels the upload
+		 */
+		public void setCancellationCallback(Runnable cancellationCallback) {
+			this.cancellationCallback = cancellationCallback;
+		}
+
 		@Override
 		public void write(int b) throws IOException {
 			if (serverCancelled) {
@@ -1345,6 +1352,9 @@ public class MapiSocket {	/* cannot (yet
 					// Note, if the caller is calling print methods instead of write, the IO exception gets hidden.
 					// This is unfortunate but there's nothing we can do about it.
 					serverCancelled = true;
+					if (cancellationCallback != null) {
+						cancellationCallback.run();
+					}
 					throw new IOException("Server aborted the upload");
 				default:
 					throw new IOException("Expected MORE/DONE from server, got " + lineType);