-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #668 from molgenis/bug/add-streaming-pager
fix: add pager to insight files
- Loading branch information
Showing
14 changed files
with
612 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
armadillo/src/main/java/org/molgenis/armadillo/metadata/TextBlockReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.molgenis.armadillo.metadata; | ||
|
||
import java.io.*; | ||
|
||
/** | ||
* Reads a block or frame between 2 '\n' from given position. | ||
* | ||
* <p>It does this by seeking previous and next new lines. | ||
*/ | ||
public class TextBlockReader { | ||
private RandomAccessFile raf; | ||
|
||
public TextBlockReader(String filePath) throws FileNotFoundException { | ||
this.raf = new RandomAccessFile(filePath, "r"); | ||
} | ||
|
||
long[] updatePositions(long startPosition, long endPosition) throws IOException { | ||
long updatedStartPosition = startPosition; | ||
long updatedEndPosition = endPosition; | ||
|
||
// Seek back for first occurrence of '\n' | ||
raf.seek(updatedStartPosition); | ||
while (updatedStartPosition > 0 && raf.readByte() != '\n') { | ||
updatedStartPosition--; | ||
raf.seek(updatedStartPosition); | ||
} | ||
|
||
// Seek forward for first occurrence of '\n' | ||
raf.seek(updatedEndPosition); | ||
while (updatedEndPosition < raf.length() && raf.readByte() != '\n') { | ||
updatedEndPosition++; | ||
raf.seek(updatedEndPosition); | ||
} | ||
|
||
return new long[] {updatedStartPosition, updatedEndPosition}; | ||
} | ||
|
||
public BufferedReader readBlock(long startPosition, long endPosition) throws IOException { | ||
long[] positions = updatePositions(startPosition, endPosition); | ||
long start = positions[0]; | ||
long end = positions[1]; | ||
|
||
InputStream is = | ||
new InputStream() { | ||
long current = start; | ||
|
||
@Override | ||
public int read() throws IOException { | ||
if (current < end) { | ||
raf.seek(current++); | ||
return raf.readByte(); | ||
} else { | ||
return -1; // end of stream | ||
} | ||
} | ||
}; | ||
|
||
// Create a BufferedReader from the InputStream | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(is)); | ||
|
||
return reader; | ||
} | ||
} |
Oops, something went wrong.