mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-03-10 00:09:31 +01:00
BlockFileLoader: make stream of ByteBuffer
available
* Inner class `BlockFileIterator` now iterates `ByteBuffer` * `stream()` method calls `MessageSerializer.makeBlock()` * `streamBuffers()` method makes raw `ByteBuffer` blocks available * Add integration test `streamEntireBitcoindBlockChainAsBuffers()`
This commit is contained in:
parent
62e2e013c4
commit
59ab13680d
2 changed files with 24 additions and 8 deletions
|
@ -119,10 +119,10 @@ public class BlockFileLoader implements Iterable<Block> {
|
|||
/**
|
||||
* Iterates all the blocks in a single block file.
|
||||
*/
|
||||
public class BlockFileIterator implements Iterator<Block> {
|
||||
public class BlockFileIterator implements Iterator<ByteBuffer> {
|
||||
private final File file;
|
||||
private final BufferedInputStream currentFileStream;
|
||||
private org.bitcoinj.core.Block nextBlock = null;
|
||||
private ByteBuffer nextBlock = null;
|
||||
|
||||
public BlockFileIterator(File blockFile) throws FileNotFoundException {
|
||||
this.file = blockFile;
|
||||
|
@ -137,10 +137,10 @@ public class BlockFileLoader implements Iterable<Block> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public Block next() throws NoSuchElementException {
|
||||
public ByteBuffer next() throws NoSuchElementException {
|
||||
if (!hasNext())
|
||||
throw new NoSuchElementException();
|
||||
Block next = nextBlock;
|
||||
ByteBuffer next = nextBlock;
|
||||
nextBlock = null;
|
||||
return next;
|
||||
}
|
||||
|
@ -181,7 +181,7 @@ public class BlockFileLoader implements Iterable<Block> {
|
|||
return;
|
||||
}
|
||||
try {
|
||||
nextBlock = serializer.makeBlock(ByteBuffer.wrap(dataBytes));
|
||||
nextBlock = ByteBuffer.wrap(dataBytes);
|
||||
} catch (ProtocolException e) {
|
||||
nextBlock = null;
|
||||
} catch (Exception e) {
|
||||
|
@ -204,17 +204,23 @@ public class BlockFileLoader implements Iterable<Block> {
|
|||
}
|
||||
|
||||
public Stream<Block> stream() {
|
||||
return files.stream()
|
||||
.flatMap(this::fileBlockStream)
|
||||
.map(serializer::makeBlock);
|
||||
}
|
||||
|
||||
public Stream<ByteBuffer> streamBuffers() {
|
||||
return files.stream()
|
||||
.flatMap(this::fileBlockStream);
|
||||
}
|
||||
|
||||
protected Stream<Block> fileBlockStream(File file) {
|
||||
protected Stream<ByteBuffer> fileBlockStream(File file) {
|
||||
return StreamSupport.stream(fileBlockSpliterator(file), false);
|
||||
}
|
||||
|
||||
protected Spliterator<Block> fileBlockSpliterator(File file) {
|
||||
protected Spliterator<ByteBuffer> fileBlockSpliterator(File file) {
|
||||
try {
|
||||
Iterator<Block> iterator = new BlockFileIterator(file);
|
||||
Iterator<ByteBuffer> iterator = new BlockFileIterator(file);
|
||||
int characteristics = Spliterator.DISTINCT | Spliterator.ORDERED;
|
||||
return Spliterators.spliteratorUnknownSize(iterator, characteristics);
|
||||
} catch (FileNotFoundException e) {
|
||||
|
|
|
@ -84,4 +84,14 @@ public class BlockFileLoaderBitcoindTest {
|
|||
System.out.println("Final block height: " + (blockCount - 1));
|
||||
assertTrue(blockCount > 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void streamEntireBitcoindBlockchainAsBuffers() {
|
||||
BlockFileLoader loader = new BlockFileLoader(BitcoinNetwork.MAINNET, BlockFileLoader.getReferenceClientBlockFileList());
|
||||
|
||||
long blockCount = loader.streamBuffers().count();
|
||||
System.out.println("Final block height: " + (blockCount - 1));
|
||||
assertTrue(blockCount > 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue