summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/ioutils/LayeredBlockReader.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2017-03-27 23:25:47 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2017-03-27 23:25:47 -0400
commit5b8c57f577151d8de4bd4ef95d9568ec3ba99fc9 (patch)
tree25512cc880b5139742db116b6638a4bf916c9305 /BJC-Utils2/src/main/java/bjc/utils/ioutils/LayeredBlockReader.java
parent4da4974456179168125fd78e5bde09c2de81b258 (diff)
Implement more BlockReader types
Adds additional useful block reader types. * LayeredBlockReader, which represents priority * SerialBlockReader, which concatenates readers
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/ioutils/LayeredBlockReader.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/ioutils/LayeredBlockReader.java70
1 files changed, 70 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/LayeredBlockReader.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/LayeredBlockReader.java
new file mode 100644
index 0000000..ed7a1b9
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/LayeredBlockReader.java
@@ -0,0 +1,70 @@
+package bjc.utils.ioutils;
+
+import java.io.IOException;
+
+/**
+ * A block reader that supports draining all the blocks from one reading before
+ * swapping to another.
+ *
+ * This is more a 'prioritize blocks from one over the other', than a 'read all
+ * the blocks from one, then all the blocks from the other'. If you need that,
+ * look at {@link SerialBlockReader}.
+ *
+ * @author bjculkin
+ *
+ */
+public class LayeredBlockReader implements BlockReader {
+ private BlockReader first;
+ private BlockReader second;
+
+ private int blockNo;
+
+ /**
+ * Create a new layered block reader.
+ *
+ * @param primary
+ * The first source to read blocks from.
+ *
+ * @param secondary
+ * The second source to read blocks from.
+ */
+ public LayeredBlockReader(BlockReader primary, BlockReader secondary) {
+ first = primary;
+ second = secondary;
+ }
+
+ @Override
+ public boolean hasNextBlock() {
+ return first.hasNextBlock() || second.hasNextBlock();
+ }
+
+ @Override
+ public Block getBlock() {
+ Block firstBlock = first.getBlock();
+
+ return firstBlock == null ? second.getBlock() : firstBlock;
+ }
+
+ @Override
+ public boolean nextBlock() {
+ boolean gotFirst = first.nextBlock();
+
+ boolean succ = gotFirst ? gotFirst : second.nextBlock();
+
+ if (succ) blockNo += 1;
+
+ return succ;
+ }
+
+ @Override
+ public int getBlockCount() {
+ return blockNo;
+ }
+
+ @Override
+ public void close() throws IOException {
+ second.close();
+
+ first.close();
+ }
+} \ No newline at end of file