[英]RandomAccessFile.write not writing what I tell it to
大家好。 我正在從一個sql格式的文件讀取到另一個,而中間的兩個字節已損壞,並且我認為這是我沒有做的准備或保護措施。
數據損壞示例:
//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');
讀取/寫入的代碼:
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");
}
}
有任何想法嗎?
您的代碼讓我有些困惑,為什么要進行如此多的轉換,但是我在這里...
我的直覺告訴我,您要么無意間進行了一些字符集轉換,要么損壞是由於在原始字節,Java字節原語和Java int原語之間移動而引起的。 請記住,Java的byte
值范圍在-127到128之間,並且String的.getBytes()支持字符編碼方案。
具體來說,這對我來說真的很奇怪:
byte[] virusConvertedToHexString = StringUtils.getHexString(viruses[i]).getBytes();
這是正在發生的事情:
viruses[i]
給您一個byte
數組 StringUtils.getHexString()
采用的是字節數組,並為您提供了一個十六進制表示byte
數組作為String
(假設:這是什么StringUtils
它似乎不從? [org.apache.commons.lang][1]
) String
的byte
數組存儲到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');
這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.