gpt4 book ai didi

java - 在 Preon 中解析可变记录长度

转载 作者:行者123 更新时间:2023-12-02 08:34:28 24 4
gpt4 key购买 nike

我正在尝试使用 Preon解析二进制文件,这些文件被构造为一系列可变长度记录。对于每条记录,都有一个数字指定记录长度(以字节为单位)。

这是我正在尝试做的事情的简化版本:

package test.preon; 

import nl.flotsam.preon.annotation.BoundList;
import nl.flotsam.preon.annotation.BoundNumber;
import java.util.List;

public class BinFile {
@BoundNumber(size="16") int numberOfRecords;
@BoundList(type=Record.class, size="numberOfRecords") List<Record> records;

public int getNumberOfRecords() {
return numberOfRecords;
}

public List<Record> getRecords() {
return records;
}

public class Record {
@BoundNumber(size="16") int recordLength;
@BoundList(size="recordLength") byte[] data;

public int getRecordLength() {
return recordLength;
}

public byte[] getData() {
return data;
}
}
}

因此,numberOfRecords 指定文件中的记录数,recordLength 指定每个记录的长度。问题是 Preon 无法解析 Record 中的 recordLength,尽管 numberOfRecords 在 BinFile 中工作正常。

这是我得到的异常:

nl.flotsam.limbo.BindingException: Failed to resolve recordLength on class test.preon.BinFile
at nl.flotsam.preon.codec.BindingsContext$BindingsResolver.get(BindingsContext.java:412)
at nl.flotsam.preon.codec.BindingsContext$BindingReference.resolve(BindingsContext.java:247)
at nl.flotsam.preon.codec.BindingsContext$BindingReference.resolve(BindingsContext.java:189)
at nl.flotsam.limbo.ast.ReferenceNode.eval(ReferenceNode.java:57)
at nl.flotsam.limbo.ast.ArithmeticNode$Operator$5.eval(ArithmeticNode.java:109)
at nl.flotsam.limbo.ast.ArithmeticNode.eval(ArithmeticNode.java:250)
at nl.flotsam.limbo.ast.ArithmeticNode.eval(ArithmeticNode.java:33)
at nl.flotsam.limbo.ast.ArithmeticNode$Operator$3.eval(ArithmeticNode.java:83)
at nl.flotsam.limbo.ast.ArithmeticNode.eval(ArithmeticNode.java:250)
at nl.flotsam.limbo.ast.ArithmeticNode.eval(ArithmeticNode.java:33)
at nl.flotsam.limbo.ast.ArithmeticNode$Operator$5.eval(ArithmeticNode.java:109)
at nl.flotsam.limbo.ast.ArithmeticNode.eval(ArithmeticNode.java:250)
at nl.flotsam.limbo.ast.ArithmeticNode.eval(ArithmeticNode.java:33)
at nl.flotsam.preon.codec.ListCodecFactory$SwitchingListCodec.decode(ListCodecFactory.java:458)
at nl.flotsam.preon.codec.ListCodecFactory$SwitchingListCodec.decode(ListCodecFactory.java:443)
at nl.flotsam.preon.binding.StandardBindingFactory$FieldBinding.load(StandardBindingFactory.java:128)
at nl.flotsam.preon.codec.ObjectCodecFactory$ObjectCodec.decode(ObjectCodecFactory.java:251)
at nl.flotsam.preon.DefaultCodecFactory$DefaultCodec.decode(DefaultCodecFactory.java:173)
at nl.flotsam.preon.Codecs.decode(Codecs.java:218)
at nl.flotsam.preon.Codecs.decode(Codecs.java:199)
...

如果我将 size="recordLength"更改为常量,例如size="42",我没有得到异常(exception)(但是当然,记录长度必须始终相同)。

是否有其他方法可以使记录长度可变,或者我应该以不同的方式组织事物?

如果有人感兴趣,这是我使用过的 JUnit 测试:

package test.preon;

import org.junit.Test;
import static org.junit.Assert.*;
import nl.flotsam.preon.Codecs;
import nl.flotsam.preon.Codec;
import nl.flotsam.preon.DecodingException;
import test.preon.BinFile;
import test.preon.BinFile.Record;
import java.util.List;

public class BinFileTest {

@Test
public void parseBinFile() throws DecodingException {
Codec<BinFile> codec = Codecs.create(BinFile.class);
byte[] buffer = new byte[] {
2, 0,
3, 0,
'a', 'b', 'c',
4, 0,
'1', '2', '3', '4'
};
BinFile b = Codecs.decode(codec, buffer);

assertEquals(b.getNumberOfRecords(), 2);

List<Record> rL = b.getRecords();

assertEquals(rL.size(), 2);

Record r0 = rL.get(0);
assertEquals(r0.getRecordLength(), 3);
assertEquals(new String(r0.getData()), "abc");

Record r1 = rL.get(1);
assertEquals(r1.getRecordLength(), 4);
assertEquals(new String(r1.getData()), "1234");
}
}

最佳答案

原来你遇到了一个错误。 ListCodecFactory 有一个策略来决定在各种情况下生成哪种类型的编解码器,但事实证明在这种情况下它选择了错误的编解码器。我有这个补丁,如果你有兴趣的话我可以发给你。

关于java - 在 Preon 中解析可变记录长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2362412/

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