2009-07-08 109 views
0

嘿所有。我正在從一個sql格式文件讀到另一個文件,並且中間的兩個字節被破壞了,我認爲這是一些我沒有做過的準備或保護措施。RandomAccessFile.write不寫我告訴它

損壞的數據的實施例:

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"); 
    } 
} 

任何想法:讀取自/寫入到

//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'); 

碼?

+2

是的,我有一個想法,你可以簡化你的代碼到相關的部分。這樣,有人可能真的想讀它。 – skaffman 2009-07-08 09:34:32

回答

0

我有點被你的代碼困惑,爲什麼有這麼多的轉換正在進行,但在這裏我去...

我的直覺告訴我,你有兩種一些字符集轉換回事,無意中,或者腐敗是由於在原始字節,Java字節原語和Java int原語之間移動造成的。請記住,Java的byte值範圍介於-127和128之間,並且該字符串的.getBytes()是字符編碼方案感知。

具體而言,這只是看起來太奇怪了吧:

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

這是正在發生的事情:

  1. viruses[i]是給你一個byte陣列
  2. StringUtils.getHexString()採用的是字節數組,並給出你的byte數組的十六進制表示形式爲String(假設:這是什麼StringUtils?它沒有看到米是從[org.apache.commons.lang][1]。)
  3. 最後,將所述Stringbyte陣列存儲到virusConvertedToHexString

步驟2是我會懷疑麻煩。

另外,代碼塊的上方不包括所生成的代碼:

//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'); 

這將有助於。

+0

StringUtils只是我的其他課程。 – montooner 2009-07-08 10:07:55