Searching for a sequence of Bytes in a Binary File with Java

前端 未结 4 2083
故里飘歌
故里飘歌 2020-12-01 05:08

I have a sequence of bytes that I have to search for in a set of Binary files using Java.

Example: I\'m searching for the byte sequence DEADBEEF (in hex

4条回答
  •  鱼传尺愫
    2020-12-01 06:07

    You can find sequence of bytes from giga-bytes order file using bigdoc.

    Lib and Example here on Github at: https://github.com/riversun/bigdoc

    package org.example;
    
    import java.io.File;
    import java.util.List;
    
    import org.riversun.bigdoc.bin.BigFileSearcher;
    
    public class Example {
    
        public static void main(String[] args) throws Exception {
    
            byte[] searchBytes = "hello world.".getBytes("UTF-8");
    
            File file = new File("/var/tmp/yourBigfile.bin");
    
            BigFileSearcher searcher = new BigFileSearcher();
    
            List findList = searcher.searchBigFile(file, searchBytes);
    
            System.out.println("positions = " + findList);
        }
    }
    

    If you want to search it on memory,check this. Examples here on Github at: https://github.com/riversun/finbin

     import java.util.List;
    
     import org.riversun.finbin.BigBinarySearcher;
    
     public class Example {
    
         public static void main(String[] args) throws Exception {
    
             BigBinarySearcher bbs = new BigBinarySearcher();
    
             byte[] iamBigSrcBytes = "Hello world.It's a small world.".getBytes("utf-8");
    
             byte[] searchBytes = "world".getBytes("utf-8");
    
             List indexList = bbs.searchBytes(iamBigSrcBytes, searchBytes);
    
             System.out.println("indexList=" + indexList);
         }
     }
    

    Returns all the matched positions in the array of bytes

    It also can withstand a large array of bytes:)

提交回复
热议问题