package org.ethereum.manager;


import org.ethereum.core.Block;
import org.ethereum.facade.Blockchain;
import org.ethereum.net.BlockQueue;
import org.spongycastle.util.encoders.Hex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.List;

import static org.ethereum.config.SystemProperties.CONFIG;

@Component
public class BlockLoader {

    @Autowired
    private Blockchain blockchain;


    public void loadBlocks(){

        String fileSrc = CONFIG.blocksLoader();
        try {
            File blocksFile = new File(fileSrc);
            System.out.println("Loading blocks: " + fileSrc);
            List<String> blocksList = Files.readAllLines(blocksFile.toPath(), StandardCharsets.UTF_8);

            for (String blockRLP : blocksList){

                byte[] blockRLPBytes = Hex.decode( blockRLP );
                Block block = new Block(blockRLPBytes);

                if (block.getNumber() > blockchain.getBestBlock().getNumber()){
                    System.out.println("Importing block #" + block.getNumber());
                    blockchain.tryToConnect(block);
                } else
                    System.out.println("Skipping block #" + block.getNumber());


            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}

@@ -100,44 +100,8 @@
 in;%0A
-import org.ethereum.net.BlockQueue;%0A
 impo
@@ -292,76 +292,51 @@
 .io.
-IOException;%0Aimport java.net.URISyntaxException;%0Aimport java.net.URL
+FileInputStream;%0Aimport java.io.IOException
 ;%0Aim
@@ -425,16 +425,42 @@
 il.List;
+%0Aimport java.util.Scanner;
 %0A%0Aimport
@@ -603,16 +603,45 @@
 chain;%0A%0A
+    Scanner scanner = null;%0A%0A
 %0A    pub
@@ -726,16 +726,17 @@
   try %7B%0A
+%0A
         
@@ -747,40 +747,153 @@
 File
- blocksFile = new File(fileSrc);
+InputStream inputStream = null;%0A            inputStream = new FileInputStream(fileSrc);%0A            scanner = new Scanner(inputStream, %22UTF-8%22);%0A
 %0A   
@@ -943,32 +943,33 @@
 : %22 + fileSrc);%0A
+%0A
             List
@@ -968,146 +968,38 @@
     
-List%3CString%3E blocksList = Files.readAllLines(blocksFile.toPath(), StandardCharsets.UTF_8);%0A%0A            for (String blockRLP : blocksList)
+while (scanner.hasNextLine()) 
 %7B%0A%0A 
@@ -1052,17 +1052,26 @@
 de( 
-blockRLP 
+scanner.nextLine()
 );%0A 
@@ -1122,24 +1122,69 @@
 RLPBytes);%0A%0A
+                long t1 = System.nanoTime();%0A
             
@@ -1275,34 +1275,147 @@
     
-System.out.println
+blockchain.tryToConnect(block);%0A                    long t1_ = System.nanoTime();%0A                    String result = String.format
 (%22Import
 ing 
@@ -1410,27 +1410,26 @@
 (%22Import
-ing
+ed
  block #
 %22 + bloc
@@ -1424,32 +1424,115 @@
 ck #
-%22 + block.getNumber(
+%25d took: %5B%2502.2f msec%5D%22,%0A                            block.getNumber(), (float)((t1_ - t1) / 1_000_000
 ));%0A
+%0A
     
@@ -1551,37 +1551,33 @@
     
-blockchain.tryToConnect(block
+System.out.println(result
 );%0A 
@@ -1775,14 +1775,16 @@
   %7D%0A
-%0A%0A
     %7D%0A
+%0A%0A%0A%0A
 %7D%0A

Improve BlockLoader, now it will not keep full blocks file in memory