在二进制流中搜索字符串(作为byte [])

时间:2014-03-06 19:23:43

标签: java serialization seek randomaccessfile

Hi Team,我试图在二进制文件中找到一个String“Henry”,并将String更改为另一个字符串。 FYI文件是对象序列化的输出。 Original Question here

我是新手搜索字节,想象这个代码会搜索我的byte []并交换它。但它并没有接近工作它甚至找不到匹配。

{
    byte[] bytesHenry = new String("Henry").getBytes();
    byte[] bytesSwap = new String("Zsswd").getBytes();

    byte[] seekHenry = new byte[bytesHenry.length];

    RandomAccessFile file = new RandomAccessFile(fileString,"rw");

    long filePointer;
    while (seekHenry != null) {
       filePointer = file.getFilePointer();
       file.readFully(seekHenry);
       if (bytesHenry == seekHenry) {
           file.seek(filePointer);
           file.write(bytesSwap);
           break;
       }
     }
}

好的,我看到了bytesHenry==seekHenry问题并将换成Arrays.equals( bytesHenry , seekHenry )

我想每次读取5个字节时,我需要移动-4个字节的位置。


宾果它现在找到它

    while (seekHenry != null) {
                    filePointer = file.getFilePointer();
                    file.readFully(seekHenry);;
                    if (Arrays.equals(bytesHenry,
                                      seekHenry)) {
                        file.seek(filePointer);
                        file.write(bytesSwap);
                        break;
                    }
                    file.seek(filePointer);
                    file.read();
                }

2 个答案:

答案 0 :(得分:1)

以下内容适用于您,请参阅返回第一个匹配项匹配的索引的方法search(byte[] input, byte[] searchedFor),或-1。

public class SearchBuffer {

    public static void main(String[] args) throws UnsupportedEncodingException {
        String charset= "US-ASCII";
        byte[] searchedFor = "ciao".getBytes(charset);
        byte[] input = "aaaciaaaciaojjcia".getBytes(charset);

        int idx = search(input, searchedFor);
        System.out.println("index: "+idx); //should be 8
    }

    public static int search(byte[] input, byte[] searchedFor) {
        //convert byte[] to Byte[]
        Byte[] searchedForB = new Byte[searchedFor.length];
        for(int x = 0; x<searchedFor.length; x++){
            searchedForB[x] = searchedFor[x];
        }

        int idx = -1;

        //search:
        Deque<Byte> q = new ArrayDeque<Byte>(input.length);
        for(int i=0; i<input.length; i++){
            if(q.size() == searchedForB.length){
                //here I can check
                Byte[] cur = q.toArray(new Byte[]{});
                if(Arrays.equals(cur, searchedForB)){
                    //found!
                    idx = i - searchedForB.length;
                    break;
                } else {
                    //not found
                    q.pop();
                    q.addLast(input[i]);
                }
            } else {
                q.addLast(input[i]);
            }
        }

        return idx;
    }
}

答案 1 :(得分:0)

来自Fastest way to find a string in a text file with java

我在MIMEParser中发现的最佳实现:https://github.com/samskivert/ikvm-openjdk/blob/master/build/linux-amd64/impsrc/com/sun/xml/internal/org/jvnet/mimepull/MIMEParser.java

/**
  * Finds the boundary in the given buffer using Boyer-Moore algo.
  * Copied from java.util.regex.Pattern.java
  *
  * @param mybuf boundary to be searched in this mybuf
  * @param off start index in mybuf
  * @param len number of bytes in mybuf
  *
  * @return -1 if there is no match or index where the match starts
  */

  private int match(byte[] mybuf, int off, int len) {

还需要:

  private void compileBoundaryPattern();