blob: 54010fefa26c997b7fabc7c1f527f66808f12e5b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
package bjc.utils.ioutils.blocks;
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 final BlockReader first;
private final 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(final BlockReader primary, final BlockReader secondary) {
first = primary;
second = secondary;
}
@Override
public boolean hasNextBlock() {
return first.hasNextBlock() || second.hasNextBlock();
}
@Override
public Block getBlock() {
final Block firstBlock = first.getBlock();
return firstBlock == null ? second.getBlock() : firstBlock;
}
@Override
public boolean nextBlock() {
final boolean gotFirst = first.nextBlock();
final 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();
}
}
|