BlockFileLoader: Add a second constructor that allows to specify a custom directory.
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,21 +54,11 @@ 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);
@@ -74,11 +66,33 @@ public class BlockFileLoader implements Iterable<Block>, Iterator<Block> {
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;
