@@ -0,0 +1,3163 @@
+/*%0A * Copyright 2012 Matt Corallo.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *    http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0Apackage com.google.bitcoin.examples;%0A%0Aimport com.google.bitcoin.core.Block;%0Aimport com.google.bitcoin.core.FullPrunedBlockChain;%0Aimport com.google.bitcoin.core.NetworkParameters;%0Aimport com.google.bitcoin.core.Utils;%0Aimport com.google.bitcoin.store.FullPrunedBlockStore;%0Aimport com.google.bitcoin.store.H2FullPrunedBlockStore;%0A%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.IOException;%0A%0A/**%0A * This class reads block files stored in the reference/Satoshi clients format. This is simply a way to concatenate%0A * blocks together. Importing block data with this tool can be a lot faster than syncing over the network, if you%0A * have the files available.%0A */%0Apublic class BlockImporter %7B%0A    public static void main(String%5B%5D args) throws Exception %7B%0A        NetworkParameters params = NetworkParameters.prodNet();%0A        FullPrunedBlockStore store = new H2FullPrunedBlockStore(params, %22toy-full.blockchain%22, 100);%0A        FullPrunedBlockChain chain = new FullPrunedBlockChain(params, store);%0A        %0A        String defaultDataDir;%0A        if (System.getProperty(%22os.name%22).toLowerCase().indexOf(%22win%22) %3E= 0) %7B%0A            defaultDataDir = System.getenv(%22APPDATA%22) + %22%5C%5C.bitcoin/%22;%0A        %7D else %7B%0A            defaultDataDir = System.getProperty(%22user.home%22) + %22/.bitcoin/%22;%0A        %7D%0A        %0A        // TODO: Move this to a library function%0A        FileInputStream stream = new FileInputStream(new File(defaultDataDir + %22blk0001.dat%22));%0A        int i = 0;%0A        while (stream.available() %3E 0) %7B%0A            try %7B%0A                while(stream.read() != ((params.packetMagic %3E%3E%3E 24) & 0xff) %7C%7C stream.read() != ((params.packetMagic %3E%3E%3E 16) & 0xff) %7C%7C%0A                        stream.read() != ((params.packetMagic %3E%3E%3E 8) & 0xff) %7C%7C stream.read() != (params.packetMagic & 0xff))%0A                    ;%0A            %7D catch (IOException e) %7B%0A                break;%0A            %7D%0A            byte%5B%5D bytes = new byte%5B4%5D;%0A            stream.read(bytes, 0, 4);%0A            long size = Utils.readUint32BE(Utils.reverseBytes(bytes), 0);%0A            if (size %3E Block.MAX_BLOCK_SIZE %7C%7C size %3C= 0)%0A                continue;%0A            bytes = new byte%5B(int) size%5D;%0A            stream.read(bytes, 0, (int)size);%0A            Block block = new Block(params, bytes);%0A            if (store.get(block.getHash()) == null)%0A                chain.add(block);%0A            %0A            if (i %25 10000 == 0)%0A                System.out.println(i);%0A            i++;%0A        %7D%0A        System.out.println(%22Imported %22 + chain.getChainHead().getHeight() + %22 blocks.%22);%0A    %7D%0A%7D%0A

Add a BlockImporter tool to examples.