gpt4 book ai didi

java - 用不同大小的 bitset 替换所有内部 bitset

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:13:53 26 4
gpt4 key购买 nike

我目前正在处理一个二进制文件,稍后将写入另一个二进制文件。这一点非常重要,也是我对使用 ArrayList 和其他列表犹豫不决的原因,因为它们往往不太适合我尝试将其直接写入文件。

我已经从这个二进制文件中检索出字节,并使用 BitSet 将它们分成位。我想我已经想出如何找到我想要替换的位集。目前这看起来有点像这样:

try {
InputStream inputStream = new FileInputStream(filepath);
OutputStream outputStream = new FileOutputStream("output.bin");
byte[] buffer = new byte[4096];
BitSet bitSet = new BitSet(4096 * 8);
BitSet bitString = new BitSet(search.length());
BitSet bitReplace = new BitSet(replace.length());
// Search String to bitset
for (int i = search.length() - 1; i >= 0; i--) {
if (search.charAt(i) == '1') {
bitString.set(search.length() - i - 1);
}
}
// Replace String to bitset
for (int i = replace.length() - 1; i >= 0; i--) {
if (replace.charAt(i) == '1') {
bitReplace.set(replace.length() - i - 1);
}
}
while (inputStream.read(buffer) != -1) {
bitSet = BitSet.valueOf(buffer);
bufferCount++;
// GET 4096 BYTES AT THE SAME TIME
// TURN THEM INTO BITS, WE END UP WITH 4096*8 bits
// COMPARE EVERY SEARCHSIZE BITS
for (int i = 0; i < bitSet.length(); i++) {
if (bitSet.get(i, i + bitString.length()).equals(bitString)) {
//TODO: Replace bitset with a different bit set
}
}
}
inputStream.close();
outputStream.close();

} catch (IOException e) {
System.out.println("IOException");
System.exit(1);
}

我缺少的是一旦找到具有不同位集(可能大小不同)的位模式,如何设置现有位集。

为了说明:

Find: 01010 replace with: 001111

将把这个位序列:

00|01010|01000000000000010

进入:

00|001111|010000000000000010

抽象地我想到了一个解决方案,就像这样:

1. Find the pattern that matches the SEARCHed pattern
2. Replace a bitset with a completely different bitset(this is what I'm struggling with, I was thinking about just appending everything to the end of the file, but that would not be very efficient in terms of read/write
3. Shift the other bits to the left or to the right based on the difference between the sizes of the searched pattern and the pattern we're replacing with.
4. Write into file.

最佳答案

你可以定义一个函数setBitsFromIndex(int i, BitSet source, BitSet dest):

private static void setBitsFromIndex(int i, BitSet source, BitSet dest) {
for (int j = 0; j < source.length(); j++) {
dest.set(i+j, source.get(j));
}
}

然后,在您的代码中:

for (int i = 0; i < bitSet.length() - bitString.length(); i++) { 
if (bitSet.get(i, i + bitString.length()).equals(bitString)) {

//Replace bitset with a different bit set

BitSet tempBitSet = bitSet.get(i + bitString.length(), bitSet.length());
setBitsFromIndex(i, bitReplace, bitSet);
setBitsFromIndex(i + bitReplace.length(), tempBitSet, bitSet);

// if bitReplace is shorter than bitString, we may need to clear trailing bits
if (bitReplace.length() < bitString.length()) {
bitSet.clear(i + bitReplace.length() + tempBitSet.length(), bitSet.length());
}
break;
}
}

警告:BitSet 的长度不是它的容量,甚至不是您最后一次设置位之前的长度。它是 HIGHEST SET (1) BIT 的索引 + 1,因此您的 bitReplacebitStringbitSet 如果最高有效位为 0,则 BitSet 可能不是您认为的长度。如果您想包含前导零,则必须单独跟踪所需的 bitReplacebitString BitSet 大小。

关于java - 用不同大小的 bitset 替换所有内部 bitset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52883309/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com