changeset 508:7730d65dfd55 onclient

Add MapiSocket.BlockInputStream.Raw An alternative interface that exposes the block based nature of the MAPI protocol
author Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com>
date Thu, 19 Aug 2021 15:15:50 +0200 (2021-08-19)
parents 1db3398b78f7
children 455497783026
files src/main/java/org/monetdb/mcl/net/MapiSocket.java
diffstat 1 files changed, 38 insertions(+), 2 deletions(-) [+]
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
@@ -855,6 +855,7 @@ public class MapiSocket {	/* cannot (yet
 	final class BlockInputStream extends FilterInputStream {
 		private int readPos = 0;
 		private int blockLen = 0;
+		private boolean wasEndBlock = false;
 		private final byte[] block = new byte[BLOCK + 3]; // \n.\n
 		private boolean insertFakeFlush = true;
 
@@ -968,10 +969,12 @@ public class MapiSocket {	/* cannot (yet
 					(blklen[0] & 0xFF) >> 1 |
 					(blklen[1] & 0xFF) << 7
 					);
+			wasEndBlock = (blklen[0] & 0x1) == 1;
+
 			readPos = 0;
 
 			if (debug) {
-				if ((blklen[0] & 0x1) == 1) {
+				if (wasEndBlock) {
 					log("RD ", "read final block: " + blockLen + " bytes", false);
 				} else {
 					log("RD ", "read new block: " + blockLen + " bytes", false);
@@ -990,7 +993,7 @@ public class MapiSocket {	/* cannot (yet
 				log("RX ", new String(block, 0, blockLen, "UTF-8"), true);
 
 			// if this is the last block, make it end with a newline and prompt
-			if ((blklen[0] & 0x1) == 1) {
+			if (wasEndBlock) {
 				// insert 'fake' newline and flush
 				if (insertFakeFlush) {
 					if (blockLen > 0 && block[blockLen - 1] != '\n') {
@@ -1076,6 +1079,39 @@ public class MapiSocket {	/* cannot (yet
 			}
 			return n;
 		}
+
+		Raw getRaw() {
+			return new Raw();
+		}
+
+		/** An alternative I/O interface that exposes the block based nature of the MAPI protocol */
+		final class Raw {
+			byte[] getBytes() {
+				return block;
+			}
+
+			int getLength() {
+				return blockLen;
+			}
+
+			int getPosition() {
+				return readPos;
+			}
+
+			int consume(int delta) {
+				int pos = readPos;
+				readPos += delta;
+				return pos;
+			}
+
+			int readBlock() throws IOException {
+				return BlockInputStream.this.readBlock();
+			}
+
+			boolean wasEndBlock() {
+				return wasEndBlock;
+			}
+		}
 	}
 
 	/**