你将如何解析一个可变长度的字节序列,其中第一位(BigEndian)指示是否使用Preon跟随另一个字节?
示例
byte[] bytecode = new byte[] {
(byte) 0xf2, (byte) 0xbf, (byte) 0xbf, (byte) 0xbf, (byte) 0x50
};
备注
结果字节(十进制)
{114,63,63,63,80}
已经尝试
@BoundList + @Choices(有条件)
Limbo exp lang不支持方法调用,因此您无法检测到流的结尾(之前需要有符号1,当前块需要是最后一个,即符号需要为0)
使用@If
的递归方法public static class Entry {
@BoundNumber(size = "1", byteOrder = ByteOrder.BigEndian)
private byte hasNext;
@BoundNumber(size = "7", byteOrder = ByteOrder.BigEndian)
private byte payload;
@If("hasNext > 0")
@BoundNumber(size = "1", byteOrder = ByteOrder.BigEndian)
private byte hasNext1;
@If("hasNext > 0")
@BoundNumber(size = "7", byteOrder = ByteOrder.BigEndian)
private byte payload1;
@If("hasNext1 > 0")
@BoundObject
private Entry nextEntry;
@Override
public String toString() {
return hasNext > 0 ? String.valueOf(payload) : String.valueOf(payload)
+ ", " + String.valueOf(payload1);
}
//...
}
出于某种原因,例如上面提到的,即使应该有3个,Preon也只会解析2个Entry(父和子)实例。
感谢。
答案 0 :(得分:2)
为此,我建议您实施自己的Codec或CodecDecorator,具体取决于您想要做什么。如果您只想将字节序列存储在自己的字节数组中,那么创建自己的编解码器并将其与框架挂钩应该相当容易。
这是Codec的一个实现,它可能接近您所寻找的内容:
<强> VariableLengthByteArrayCodec:强>
package org.codehaus.preon.sample.varlength;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.codehaus.preon.Builder;
import org.codehaus.preon.Codec;
import org.codehaus.preon.CodecDescriptor;
import org.codehaus.preon.DecodingException;
import org.codehaus.preon.Resolver;
import org.codehaus.preon.buffer.BitBuffer;
import org.codehaus.preon.channel.BitChannel;
import org.codehaus.preon.el.Expression;
import nl.flotsam.pecia.Documenter;
import nl.flotsam.pecia.ParaContents;
import nl.flotsam.pecia.SimpleContents;
public class VariableLengthByteArrayCodec implements Codec<byte[]> {
public byte[] decode(BitBuffer buffer, Resolver resolver, Builder builder) throws DecodingException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
boolean cont = true;
while (cont) {
byte b = buffer.readAsByte(8);
bout.write(b);
cont = (b & (1 << 7)) > 0;
}
return bout.toByteArray();
}
public void encode(byte[] value, BitChannel channel, Resolver resolver) throws IOException {
channel.write(value, 0, value.length - 1);
}
public Expression<Integer, Resolver> getSize() {
return null;
}
public CodecDescriptor getCodecDescriptor() {
return new CodecDescriptor() {
public <C extends ParaContents<?>> Documenter<C> summary() {
return new Documenter<C>() {
public void document(C target) {
target.document(reference(Adjective.A, true));
target.text(".");
}
};
}
public <C extends ParaContents<?>> Documenter<C> reference(final Adjective adjective, final boolean startWithCapital) {
return new Documenter<C>() {
public void document(C target) {
target.text(adjective.asTextPreferA(startWithCapital))
.text("variable length encoded byte array.");
}
};
}
public <C extends SimpleContents<?>> Documenter<C> details(String bufferReference) {
return new Documenter<C>() {
public void document(C target) {
target.para()
.text("The number of bytes is determined by the ")
.text("leading bit of the individual bytes; ")
.text("if the first bit of a byte is 1, then ")
.text("more bytes are expted to follow.");
}
};
}
public boolean requiresDedicatedSection() {
return false;
}
public String getTitle() {
assert requiresDedicatedSection();
return null;
}
};
}
public Class<?>[] getTypes() {
return new Class<?>[] { Byte[].class };
}
public Class<?> getType() {
return Byte[].class;
}
}
<强> VariableLengthByteArrayCodecFactory:强>
package org.codehaus.preon.sample.varlength;
import java.lang.reflect.AnnotatedElement;
import org.codehaus.preon.Codec;
import org.codehaus.preon.CodecFactory;
import org.codehaus.preon.ResolverContext;
public class VariableLengthByteArrayCodecFactory implements CodecFactory {
public <T> Codec<T> create(AnnotatedElement metadata, Class<T> type, ResolverContext context) {
if (metadata != null && metadata.isAnnotationPresent(VarLengthEncoded.class) && type == byte[].class) {
return (Codec<T>) new VariableLengthByteArrayCodec();
} else {
return null;
}
}
}
<强> VarLengthEncoded:强>
package org.codehaus.preon.sample.varlength;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface VarLengthEncoded {
}
然后最后,你如何使用它:
public static class SomeHolder {
@VarLengthEncoded byte[] value;
public byte[] getValue() {
return value;
}
}
...
Codec<SomeHolder> codec = Codecs.create(SomeHolder.class, new VariableLengthByteArrayCodecFactory());
SomeHolder holder = Codecs.decode(codec, (byte) 0xff, (byte) 0x0f);
assertThat(holder.getValue(), is(not(nullValue())));
assertThat(holder.getValue().length, is(2));
assertThat(holder.getValue()[0], is((byte) 0xff));
assertThat(holder.getValue()[1], is((byte) 0x0f));
这看起来可能是相当多的代码,但如果仔细检查,那么您会注意到大多数代码实际上确保在生成带有@VarLengthEncoded注释的类的文档时生成正确的描述。如果您根本不关心文档,那么您只需返回默认的CodecDescriptor即可。
所以,我想,这个答案的实质是:肯定会有一些情况,即在Preon中提供一个实现会使框架过载。这并不意味着您应该依赖框架默认提供的所有内容。这只是意味着您应该插入自己的扩展程序。