首页 > 数据库 >SQLiteHeaderParser

SQLiteHeaderParser

时间:2024-10-02 10:00:29浏览次数:7  
标签:SQLiteHeaderParser buffer System getInt int println out

package com.tencent.map.dataengine.converter;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class SQLiteHeaderParser {
    public static void main(String[] args) {
        String filePath = "xxx.db"; // 替换为你要解析的SQLite3数据库文件的路径

        try (FileInputStream fileInputStream = new FileInputStream(filePath)) {
            byte[] headerBytes = new byte[100]; // SQLite3数据库文件头部通常为100字节
            fileInputStream.read(headerBytes);

            ByteBuffer buffer = ByteBuffer.wrap(headerBytes);
            buffer.order(ByteOrder.BIG_ENDIAN);

            // 解析头部结构
            String magic = new String(headerBytes, 0, 16);
            short pageSize = buffer.getShort(16);
            byte fileFormatWriteVersion = buffer.get(18);
            byte fileFormatReadVersion = buffer.get(19);
            short reservedSpace = buffer.getShort(20);
            byte maxPayloadFraction = buffer.get(22);
            byte minPayloadFraction = buffer.get(23);
            byte leafPayloadFraction = buffer.get(24);
            int fileChangeCounter = buffer.getInt(28);
            int databaseSizePages = buffer.getInt(32);
            int firstFreelistPage = buffer.getInt(36);
            int totalFreelistPages = buffer.getInt(40);
            int schemaCookie = buffer.getInt(44);
            int schemaFormatNumber = buffer.getInt(48);
            int defaultPageCacheSize = buffer.getInt(52);
            int largestBtreePage = buffer.getInt(56);
            int textEncoding = buffer.getInt(60);
            int userVersion = buffer.getInt(64);
            int incrementalVacuumMode = buffer.getInt(68);
            int applicationId = buffer.getInt(72);
            byte[] reserved = new byte[20];
            buffer.get(reserved);
            int versionValidFor = buffer.getInt(92);
            int sqliteVersion = buffer.getInt(96);

            // 打印解析结果
            System.out.println("Magic: " + magic);
            System.out.println("Page Size: " + pageSize);
            System.out.println("File Format Write Version: " + fileFormatWriteVersion);
            System.out.println("File Format Read Version: " + fileFormatReadVersion);
            System.out.println("Reserved Space: " + reservedSpace);
            System.out.println("Max Payload Fraction: " + maxPayloadFraction);
            System.out.println("Min Payload Fraction: " + minPayloadFraction);
            System.out.println("Leaf Payload Fraction: " + leafPayloadFraction);
            System.out.println("File Change Counter: " + fileChangeCounter);
            System.out.println("Database Size (in pages): " + databaseSizePages);
            System.out.println("First Freelist Page: " + firstFreelistPage);
            System.out.println("Total Freelist Pages: " + totalFreelistPages);
            System.out.println("Schema Cookie: " + schemaCookie);
            System.out.println("Schema Format Number: " + schemaFormatNumber);
            System.out.println("Default Page Cache Size: " + defaultPageCacheSize);
            System.out.println("Largest B-Tree Page: " + largestBtreePage);
            System.out.println("Text Encoding: " + textEncoding);
            System.out.println("User Version: " + userVersion);
            System.out.println("Incremental Vacuum Mode: " + incrementalVacuumMode);
            System.out.println("Application ID: " + applicationId);
            System.out.println("Reserved: " + new String(reserved));
            System.out.println("Version Valid For: " + versionValidFor);
            System.out.println("SQLite Version: " + sqliteVersion);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

标签:SQLiteHeaderParser,buffer,System,getInt,int,println,out
From: https://www.cnblogs.com/kk0716/p/18444452

相关文章