Skip to content

Commit

Permalink
add listener to notify the readStream state of the BigByteBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
ate47 committed Apr 5, 2022
1 parent 032e4c0 commit a2db323
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit a2db323

Please sign in to comment.