From a2db3239a1a427400dca0987af8d0678c3ae65bb Mon Sep 17 00:00:00 2001
From: qaate47 <antoine.willerval@the-qa-company.com>
Date: Tue, 5 Apr 2022 08:51:25 +0200
Subject: [PATCH] add listener to notify the readStream state of the
 BigByteBuffer

---
 .../impl/section/PFCDictionarySectionBig.java         |  4 ++--
 .../java/org/rdfhdt/hdt/util/io/BigByteBuffer.java    | 11 +++++++----
 .../org/rdfhdt/hdt/util/io/BigByteBufferTest.java     |  2 +-
 3 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/hdt-java-core/src/main/java/org/rdfhdt/hdt/dictionary/impl/section/PFCDictionarySectionBig.java b/hdt-java-core/src/main/java/org/rdfhdt/hdt/dictionary/impl/section/PFCDictionarySectionBig.java
index c9f5c453..c22a87f5 100644
--- a/hdt-java-core/src/main/java/org/rdfhdt/hdt/dictionary/impl/section/PFCDictionarySectionBig.java
+++ b/hdt-java-core/src/main/java/org/rdfhdt/hdt/dictionary/impl/section/PFCDictionarySectionBig.java
@@ -194,7 +194,7 @@ public void load(Iterator<? extends CharSequence> it, long numentries, ProgressL
 				
 				//System.out.println("Loading block: "+i+" from "+previous+" to "+ current+" of size "+ (current-previous));
 				BigByteBuffer bigByteBuffer = BigByteBuffer.allocate(nextBytePos-bytePos);
-				bigByteBuffer.readStream(in, 0, bigByteBuffer.size());
+				bigByteBuffer.readStream(in, 0, bigByteBuffer.size(), listener);
 				data[buffer]=bigByteBuffer;
 				
 				posFirst[buffer] = bytePos;
@@ -486,7 +486,7 @@ public void load(InputStream input, ProgressListener listener) throws IOExceptio
 
 			//System.out.println("Loading block: "+i+" from "+previous+" to "+ current+" of size "+ (current-previous));
 			BigByteBuffer bigByteBuffer = BigByteBuffer.allocate(nextBytePos-bytePos);
-			bigByteBuffer.readStream(in, 0, bigByteBuffer.size());
+			bigByteBuffer.readStream(in, 0, bigByteBuffer.size(), listener);
 			data[buffer]=bigByteBuffer;
 
 			posFirst[buffer] = bytePos;
diff --git a/hdt-java-core/src/main/java/org/rdfhdt/hdt/util/io/BigByteBuffer.java b/hdt-java-core/src/main/java/org/rdfhdt/hdt/util/io/BigByteBuffer.java
index 4fd26012..48e40f3a 100644
--- a/hdt-java-core/src/main/java/org/rdfhdt/hdt/util/io/BigByteBuffer.java
+++ b/hdt-java-core/src/main/java/org/rdfhdt/hdt/util/io/BigByteBuffer.java
@@ -167,31 +167,34 @@ public void get(byte[] dst, long position, int offset, int length) {
 	 * @param input the input stream to read
 	 * @param index the index to start writing
 	 * @param length the length to read
+	 * @param listener listener to notify the state
 	 * @throws IOException any error with the stream
 	 */
-	public void readStream(InputStream input, long index, long length) throws IOException {
+	public void readStream(InputStream input, long index, long length, ProgressListener listener) throws IOException {
 		long remaining = length;
 		long currentIndex = index;
 		int b = getBufferIndex(index);
+		ListenerUtil.notify(listener, "Reading buffer", 0, length);
 		while (remaining > 0) {
 			int offset = getBufferOffset(currentIndex);
 			byte[] buffer = buffers.get(b);
 
 			int read = (int) Math.min(buffer.length - offset, currentIndex + remaining);
-			readStreamInto(input, buffer, offset, read);
+			readStreamInto(input, buffer, offset, read, listener, currentIndex - index, length);
 			remaining -= read;
 			currentIndex += read;
+			ListenerUtil.notify(listener, "Reading buffer", length - remaining, length);
 			b++;
 		}
 	}
 
-	private void readStreamInto(InputStream input, byte[] dst, int start, int length) throws IOException {
+	private void readStreamInto(InputStream input, byte[] dst, int start, int length, ProgressListener listener, long offset, long end) throws IOException {
 		int nRead;
 		int pos = 0;
 
 		while ((nRead = input.read(dst, start, length - pos)) > 0) {
-			// TODO: Notify progress listener
 			pos += nRead;
+			ListenerUtil.notify(listener, "Reading buffer", pos + offset, end);
 		}
 		if (pos != length) {
 			throw new IOException("EOF while reading array from InputStream");
diff --git a/hdt-java-core/src/test/java/org/rdfhdt/hdt/util/io/BigByteBufferTest.java b/hdt-java-core/src/test/java/org/rdfhdt/hdt/util/io/BigByteBufferTest.java
index a1d40e71..69db5ad8 100644
--- a/hdt-java-core/src/test/java/org/rdfhdt/hdt/util/io/BigByteBufferTest.java
+++ b/hdt-java-core/src/test/java/org/rdfhdt/hdt/util/io/BigByteBufferTest.java
@@ -124,7 +124,7 @@ public void readFileTest() throws IOException {
 		String file = Objects.requireNonNull(getClass().getClassLoader().getResource("dbpedia.hdt"), "Can't find dbpedia.hdt").getFile();
 
 		try (InputStream stream = IOUtil.getFileInputStream(file)) {
-			buffer.readStream(stream, 0, size);
+			buffer.readStream(stream, 0, size, null);
 		}
 
 		byte[] real = Files.readAllBytes(Paths.get(file));