简体   繁体   English

RandomAccessFile.write不写我告诉的内容

[英]RandomAccessFile.write not writing what I tell it to

Hey all. 大家好。 I'm reading from one sql-format file to another, and two bytes in the middle are being corrupted, and I assume it's some preparation or safeguard that I didn't do. 我正在从一个sql格式的文件读取到另一个,而中间的两个字节已损坏,并且我认为这是我没有做的准备或保护措施。

Example of corrupted data: 数据损坏示例:

//From the file that is read from. added ** to emphasize the corrupted byte
insert into viruses (virusSig,virusHash) values (
X'579fdc569b170419e15750f0feb360aa9c58d8**90**eede50def97ee7cb03b9e905',
X'ee002fe5');

//From the file that is written to. added ** to emphasize the corrupted byte
insert into changes (filepath,loc,dat,vir,hash) values (
'E:\MyDocs\intel\antivirus\RandomFiles\0\2\5\11\24\49\EG1AxxeJSr.data',
243540,
X'9f4246ff8c73c5a5b470cab8c38416929c4eacc1e0021d5ac1fdbb88145d3e6f',
X'579fdc569b170419e15750f0feb360aa9c58d8**3f**eede50def97ee7cb03b9e905',
X'6546dd27');

Code that reads from/writes to: 读取/写入的代码:

public static void insertViruses(FileLocation[] locations, byte[][] viruses, String logpath)
{
    int numViruses = viruses.length;
    int virusLength = GenerateRandomCorpus.virusSignatureLengthInBytes;

    try{


        for (int i = 0; i < numViruses; i++)
        {   
            FileOutputStream logwriter = new FileOutputStream(logpath, true);

            // Prep to copy section
            int locationOfChange = locations[i].index;
            String filepathToChange = locations[i].filepath;
            File checkIfBackupExists = new File(filepathToChange + ".bak");
            if (!checkIfBackupExists.exists())
                copyFile(filepathToChange, filepathToChange + ".bak");
            copyFile(filepathToChange, filepathToChange + ".tmp");

            RandomAccessFile x = new RandomAccessFile(filepathToChange, "rw");
            x.seek(locationOfChange);

            // Copy section into byte array to write in log
            byte[] removedSection = new byte[virusLength];
            x.read(removedSection, 0, virusLength);
            if (GenerateRandomCorpus.dbg)
                System.out.println(filepathToChange + ":" + locationOfChange);
            x.close();

            // Write changes to log
            byte[] removedSectionConvertedToHexString = StringUtils.getHexString(removedSection).getBytes();
            byte[] virusConvertedToHexString = StringUtils.getHexString(viruses[i]).getBytes();
            byte[] hashConvertedToHexString = StringUtils.getHexString(GenerateRandomViruses.intToByteArray(new String(viruses[i]).hashCode())).getBytes();
            System.out.println(StringUtils.getHexString(removedSection));
            System.out.println(StringUtils.getHexString(viruses[i]));
            logwriter.write(String.format("insert into changes (filepath,loc,dat,vir,hash) values " +
                    "('%s',%d,X'", filepathToChange, locationOfChange).getBytes());
            logwriter.write(removedSectionConvertedToHexString);
            logwriter.write("',X'".getBytes());
            logwriter.write(virusConvertedToHexString);
            logwriter.write("',X'".getBytes());
            logwriter.write(hashConvertedToHexString);
            logwriter.write("');\n".getBytes());

            // Insert virus into file
            File original = new File(filepathToChange);
            original.delete();
            RandomAccessFile fileToInsertIn = new RandomAccessFile(filepathToChange + ".tmp", "rw");
            fileToInsertIn.seek(locationOfChange);
            fileToInsertIn.write(viruses[i]);
            fileToInsertIn.close();

            File a = new File(filepathToChange + ".tmp");
            original = new File(filepathToChange);
            a.renameTo(original);
            a.delete();

            logwriter.close();
        }


    } catch (Exception e)
    {   
        System.err.println(e.toString());
        System.err.println("Error: InsertVirusesIntoCorpus, line 100");
    }
}

Any ideas? 有任何想法吗?

I'm a bit perplexed by your code and why there are so many conversions going on, but here I go... 您的代码让我有些困惑,为什么要进行如此多的转换,但是我在这里...

My gut tells me you've got either some character set conversion going on, unintentionally, or that the corruption is due to moving between raw bytes, Java byte primitives and Java int primitives. 我的直觉告诉我,您要么无意间进行了一些字符集转换,要么损坏是由于在原始字节,Java字节原语和Java int原语之间移动而引起的。 Remember that Java the byte value range is between -127 and 128 and that String's .getBytes() is character encoding scheme aware. 请记住,Java的byte值范围在-127到128之间,并且String的.getBytes()支持字符编码方案。

Specifically, this just looks really odd to me: 具体来说,这对我来说真的很奇怪:

byte[] virusConvertedToHexString = StringUtils.getHexString(viruses[i]).getBytes();

This is what is happening: 这是正在发生的事情:

  1. viruses[i] is giving you a byte array viruses[i]给您一个byte数组
  2. StringUtils.getHexString() takes that byte array and gives you a hexadecimal representation of that byte array as a String (assumed: What is this StringUtils ? It does not seem to be from [org.apache.commons.lang][1] .) StringUtils.getHexString()采用的是字节数组,并为您提供了一个十六进制表示byte数组作为String (假设:这是什么StringUtils它似乎不从? [org.apache.commons.lang][1]
  3. Finally, you are storing the String 's byte array into virusConvertedToHexString 最后,将Stringbyte数组存储到virusConvertedToHexString

Step 2 is were I would suspect trouble. 第2步是我怀疑会遇到麻烦。

Also, the code block above does not include the code that produced: 另外,上面的代码块不包含生成的代码:

//From the file that is read from. added ** to emphasize the corrupted byte
insert into viruses (virusSig,virusHash) values (
X'579fdc569b170419e15750f0feb360aa9c58d8**90**eede50def97ee7cb03b9e905',
X'ee002fe5');

It would help. 这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM