BlockFileLoader: Add a second constructor that allows to specify a custom directory.

This commit is contained in:
Andreas Schildbach
2018-04-06 19:18:40 +02:00
parent 1700ec0be4
commit 93b9229397

View File

@@ -16,6 +16,8 @@
package org.bitcoinj.utils;
import static com.google.common.base.Preconditions.checkArgument;
import org.bitcoinj.core.Block;
import org.bitcoinj.core.NetworkParameters;
import org.bitcoinj.core.ProtocolException;
@@ -52,33 +54,45 @@ public class BlockFileLoader implements Iterable<Block>, Iterator<Block> {
/**
* Gets the list of files which contain blocks from Bitcoin Core.
*/
public static List<File> getReferenceClientBlockFileList() {
String defaultDataDir;
if (Utils.isWindows()) {
defaultDataDir = System.getenv("APPDATA") + "\\.bitcoin\\blocks\\";
} else if (Utils.isMac()) {
defaultDataDir = System.getProperty("user.home") + "/Library/Application Support/Bitcoin/blocks/";
} else if (Utils.isLinux()) {
defaultDataDir = System.getProperty("user.home") + "/.bitcoin/blocks/";
} else {
throw new RuntimeException("Unsupported system");
}
public static List<File> getReferenceClientBlockFileList(File blocksDir) {
checkArgument(blocksDir.isDirectory(), "%s is not a directory", blocksDir);
List<File> list = new LinkedList<>();
for (int i = 0; true; i++) {
File file = new File(defaultDataDir + String.format(Locale.US, "blk%05d.dat", i));
File file = new File(blocksDir, String.format(Locale.US, "blk%05d.dat", i));
if (!file.exists())
break;
list.add(file);
}
return list;
}
public static List<File> getReferenceClientBlockFileList() {
return getReferenceClientBlockFileList(defaultBlocksDir());
}
public static File defaultBlocksDir() {
final File defaultBlocksDir;
if (Utils.isWindows()) {
defaultBlocksDir = new File(System.getenv("APPDATA") + "\\.bitcoin\\blocks\\");
} else if (Utils.isMac()) {
defaultBlocksDir = new File(System.getProperty("user.home") + "/Library/Application Support/Bitcoin/blocks/");
} else if (Utils.isLinux()) {
defaultBlocksDir = new File(System.getProperty("user.home") + "/.bitcoin/blocks/");
} else {
throw new RuntimeException("Unsupported system");
}
return defaultBlocksDir;
}
private Iterator<File> fileIt;
private FileInputStream currentFileStream = null;
private Block nextBlock = null;
private NetworkParameters params;
public BlockFileLoader(NetworkParameters params, File blocksDir) {
this(params, getReferenceClientBlockFileList(blocksDir));
}
public BlockFileLoader(NetworkParameters params, List<File> files) {
fileIt = files.iterator();
this.params = params;