/*
 * Copyright (c) 2015. All Rights Reserved.
 */
package com.ndpmedia.rocketmq.store.tool;

import com.alibaba.fastjson.JSON;
import com.alibaba.rocketmq.common.message.MessageExt;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LocalMessageStoreVerificationTool {

    private static final String STORE_FILE_NAME_REGEX = "\\d+";

    private static final Pattern STORE_FILE_NAME_PATTERN = Pattern.compile(STORE_FILE_NAME_REGEX);

    private static final int MAGIC_CODE = 0xAABBCCDD ^ 1880681586 + 8;


    public static void main(String[] args) throws IOException {
        if (1 != args.length) {
            System.out.println("Usage: java -cp rocketmq-client-3.2.2.jar com.ndpmedia.rocketmq.store.tool.LocalMessageStoreVerificationTool /path/to/store");
            return;
        }

        checkRecursively(new File(args[0]));
    }

    private static void checkRecursively(File file) throws IOException {
        if (file.isFile()) {
            checkFile(file);
        } else {
            String[] files = file.list(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    File f = new File(dir, name);
                    if (f.isDirectory()) {
                        return true;
                    }

                    Matcher matcher = STORE_FILE_NAME_PATTERN.matcher(name);
                    return matcher.matches();
                }
            });

            for (String f : files) {
                checkRecursively(new File(file, f));
            }
        }
    }

    private static void checkFile(File file) throws IOException {

        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
        boolean hasError = false;
        while (randomAccessFile.getFilePointer() < randomAccessFile.length() - 4) {
            int msgSize = randomAccessFile.readInt();
            int magicCode = randomAccessFile.readInt();

            if (magicCode != MAGIC_CODE) {
                System.err.println("Illegal magic code found! Position: " + (randomAccessFile.getFilePointer() - 4));
                System.err.println("Illegal Code: [" + magicCode + "], Assumed Code: [" + MAGIC_CODE + "]");
                hasError = true;
                break;
            }

            byte[] data = new byte[msgSize];
            randomAccessFile.readFully(data);
            JSON.parseObject(data, MessageExt.class);
            System.out.println("Msg Size: " + msgSize);
            System.out.println("MSG Body: " + new String(data, Charset.forName("UTF-8")));
        }

        if (hasError) {
            System.err.println("Fatal: File " + file.getAbsolutePath() + " is tampered!");
        }

    }

}

@@ -91,42 +91,8 @@
 l;%0A%0A
-import com.alibaba.fastjson.JSON;%0A
 impo
@@ -137,19 +137,23 @@
 .Message
-Ext
+Decoder
 ;%0A%0Aimpor
@@ -256,24 +256,52 @@
 AccessFile;%0A
+import java.nio.ByteBuffer;%0A
 import java.
@@ -1969,16 +1969,24 @@
 ointer()
+ + 4 + 4
  %3C rando
@@ -2005,20 +2005,16 @@
 length()
- - 4
 ) %7B%0A    
@@ -2503,16 +2503,24 @@
 %5BmsgSize
+ - 4 - 4
 %5D;%0A     
@@ -2576,47 +2576,188 @@
     
-JSON.parseObject(data, MessageExt.class
+ByteBuffer byteBuffer = ByteBuffer.allocate(msgSize);%0A            byteBuffer.putInt(msgSize);%0A            byteBuffer.putInt(magicCode);%0A            MessageDecoder.decode(byteBuffer
 );%0A 

Update LocalMessageStoreVerificationTool as the encoding paradigm has changed