Preon 无法 encode/decode little endian
Preon unable to encode/decode little endian
背景
我正在尝试使用 preon library to encode/decode binary data. My 1 byte structure works fine when I configure the byte order for big endian, but fails for little endian. It appears that the endianess effects the intra-byte data in preon. It appears this 问题触及相同的问题,但没有解决小端方面的问题。
我附上了一个简单的案例来说明这个问题。
PreonLittleEndianNumber.java是否存在缺陷,或者当指定小端字节顺序时,preon 是否根本无法工作?
PreonTest.java
package me;
import org.codehaus.preon.Codec;
import org.codehaus.preon.Codecs;
import org.codehaus.preon.DecodingException;
import org.junit.Test;
import java.io.IOException;
import org.junit.Assert;
public class PreonTest {
@Test
public void bigEndianDecodeEncodeTest() {
byte[] testByte = {(byte) 0xDE};
Codec<PreonBigEndianNumber> bigCodec = Codecs.create(PreonBigEndianNumber.class);
try {
// Big Endian Decode/Encode Test
PreonBigEndianNumber pben = Codecs.decode(bigCodec, testByte);
byte[] testByte1 = Codecs.encode(pben, bigCodec);
Assert.assertArrayEquals(testByte, testByte1);
} catch (IOException e) {
e.printStackTrace();
} catch (DecodingException e) {
e.printStackTrace();
}
}
@Test
public void bigEndianEncodeDecodeTest() {
PreonBigEndianNumber pben = new PreonBigEndianNumber((byte)0xDE);
Codec<PreonBigEndianNumber> bigCodec = Codecs.create(PreonBigEndianNumber.class);
try {
byte[] testByte1 = Codecs.encode(pben, bigCodec);
PreonBigEndianNumber pben2 = Codecs.decode(bigCodec, testByte1);
Assert.assertEquals(pben, pben2);
} catch (IOException e) {
e.printStackTrace();
} catch (DecodingException e) {
e.printStackTrace();
}
}
@Test
public void littleEndianDecodeEncodeTest() {
byte[] testByte = {(byte) 0xDE};
Codec<PreonLittleEndianNumber> littleCodec = Codecs.create(PreonLittleEndianNumber.class);
try {
// Big Endian Decode/Encode Test
PreonLittleEndianNumber plen = Codecs.decode(littleCodec, testByte);
byte[] testByte1 = Codecs.encode(plen, littleCodec);
Assert.assertArrayEquals(testByte, testByte1);
} catch (IOException e) {
e.printStackTrace();
} catch (DecodingException e) {
e.printStackTrace();
}
}
@Test
public void littleEndianEncodeDecodeTest() {
PreonLittleEndianNumber plen = new PreonLittleEndianNumber((byte)0xDE);
Codec<PreonLittleEndianNumber> littleCodec = Codecs.create(PreonLittleEndianNumber.class);
try {
byte[] testByte1 = Codecs.encode(plen, littleCodec);
PreonLittleEndianNumber plen2 = Codecs.decode(littleCodec, testByte1);
Assert.assertEquals(plen, plen2);
} catch (IOException e) {
e.printStackTrace();
} catch (DecodingException e) {
e.printStackTrace();
}
}
}
PreonBigEndianNumber.java
package me;
import org.codehaus.preon.annotation.BoundNumber;
import org.codehaus.preon.buffer.ByteOrder;
public class PreonBigEndianNumber {
@BoundNumber(size="1", byteOrder=ByteOrder.BigEndian) public byte bit7;
@BoundNumber(size="1", byteOrder=ByteOrder.BigEndian) public byte bit6;
@BoundNumber(size="1", byteOrder=ByteOrder.BigEndian) public byte bit5;
@BoundNumber(size="1", byteOrder=ByteOrder.BigEndian) public byte bit4;
@BoundNumber(size="1", byteOrder=ByteOrder.BigEndian) public byte bit3;
@BoundNumber(size="1", byteOrder=ByteOrder.BigEndian) public byte bit2;
@BoundNumber(size="1", byteOrder=ByteOrder.BigEndian) public byte bit1;
@BoundNumber(size="1", byteOrder=ByteOrder.BigEndian) public byte bit0;
/**
* Default constructor
*/
public PreonBigEndianNumber() {
}
public PreonBigEndianNumber(byte value) {
bit7 = (byte) ((0b10000000 & value) >>> 7);
bit6 = (byte) ((0b01000000 & value) >>> 6);
bit5 = (byte) ((0b00100000 & value) >>> 5);
bit4 = (byte) ((0b00010000 & value) >>> 4);
bit3 = (byte) ((0b00001000 & value) >>> 3);
bit2 = (byte) ((0b00000100 & value) >>> 2);
bit1 = (byte) ((0b00000010 & value) >>> 1);
bit0 = (byte) ((0b00000001 & value) >>> 0);
}
public byte getByte() {
// Pack bits back into an int
int b = 0;
int shift = 0;
for (int i=7;i>=0;i--) {
b = (b << shift);
if (i==0) {b += bit0;}
else if (i==1) {b += bit1;}
else if (i==2) {b += bit2;}
else if (i==3) {b += bit3;}
else if (i==4) {b += bit4;}
else if (i==5) {b += bit5;}
else if (i==6) {b += bit6;}
else if (i==7) {b += bit7;}
shift = 1;
}
return (byte) b;
}
@Override
public String toString() {
return bit7 +
bit6 +
bit5 +
bit4 +
" " +
bit3 +
bit2 +
bit1 +
bit0;
}
@Override
public boolean equals(Object o) {
if (this == o) {return true;}
if (o == null || getClass() != o.getClass()) {return false;}
PreonBigEndianNumber preonBits = (PreonBigEndianNumber) o;
if (bit7 != preonBits.bit7) {return false;}
if (bit6 != preonBits.bit6) {return false;}
if (bit5 != preonBits.bit5) {return false;}
if (bit4 != preonBits.bit4) {return false;}
if (bit3 != preonBits.bit3) {return false;}
if (bit2 != preonBits.bit2) {return false;}
if (bit1 != preonBits.bit1) {return false;}
return bit0 == preonBits.bit0;
}
@Override
public int hashCode() {
int result = (int) bit7;
result = 31 * result + (int) bit6;
result = 31 * result + (int) bit5;
result = 31 * result + (int) bit4;
result = 31 * result + (int) bit3;
result = 31 * result + (int) bit2;
result = 31 * result + (int) bit1;
result = 31 * result + (int) bit0;
return result;
}
}
PreonLittleEndianNumber.java
package me;
import org.codehaus.preon.annotation.BoundNumber;
import org.codehaus.preon.buffer.ByteOrder;
public class PreonLittleEndianNumber {
@BoundNumber(size="1", byteOrder=ByteOrder.LittleEndian) public byte bit7;
@BoundNumber(size="1", byteOrder=ByteOrder.LittleEndian) public byte bit6;
@BoundNumber(size="1", byteOrder=ByteOrder.LittleEndian) public byte bit5;
@BoundNumber(size="1", byteOrder=ByteOrder.LittleEndian) public byte bit4;
@BoundNumber(size="1", byteOrder=ByteOrder.LittleEndian) public byte bit3;
@BoundNumber(size="1", byteOrder=ByteOrder.LittleEndian) public byte bit2;
@BoundNumber(size="1", byteOrder=ByteOrder.LittleEndian) public byte bit1;
@BoundNumber(size="1", byteOrder=ByteOrder.LittleEndian) public byte bit0;
/**
* Default constructor
*/
public PreonLittleEndianNumber() {
}
public PreonLittleEndianNumber(byte value) {
bit7 = (byte) ((0b10000000 & value) >>> 7);
bit6 = (byte) ((0b01000000 & value) >>> 6);
bit5 = (byte) ((0b00100000 & value) >>> 5);
bit4 = (byte) ((0b00010000 & value) >>> 4);
bit3 = (byte) ((0b00001000 & value) >>> 3);
bit2 = (byte) ((0b00000100 & value) >>> 2);
bit1 = (byte) ((0b00000010 & value) >>> 1);
bit0 = (byte) ((0b00000001 & value) >>> 0);
}
public byte getByte() {
// Pack bits back into an int
int b = 0;
int shift = 0;
for (int i=7;i>=0;i--) {
b = (b << shift);
if (i==0) {b += bit0;}
else if (i==1) {b += bit1;}
else if (i==2) {b += bit2;}
else if (i==3) {b += bit3;}
else if (i==4) {b += bit4;}
else if (i==5) {b += bit5;}
else if (i==6) {b += bit6;}
else if (i==7) {b += bit7;}
shift = 1;
}
return (byte) b;
}
@Override
public String toString() {
return bit7 +
bit6 +
bit5 +
bit4 +
" " +
bit3 +
bit2 +
bit1 +
bit0;
}
@Override
public boolean equals(Object o) {
if (this == o) {return true;}
if (o == null || getClass() != o.getClass()) {return false;}
PreonLittleEndianNumber preonBits = (PreonLittleEndianNumber) o;
if (bit7 != preonBits.bit7) {return false;}
if (bit6 != preonBits.bit6) {return false;}
if (bit5 != preonBits.bit5) {return false;}
if (bit4 != preonBits.bit4) {return false;}
if (bit3 != preonBits.bit3) {return false;}
if (bit2 != preonBits.bit2) {return false;}
if (bit1 != preonBits.bit1) {return false;}
return bit0 == preonBits.bit0;
}
@Override
public int hashCode() {
int result = (int) bit7;
result = 31 * result + (int) bit6;
result = 31 * result + (int) bit5;
result = 31 * result + (int) bit4;
result = 31 * result + (int) bit3;
result = 31 * result + (int) bit2;
result = 31 * result + (int) bit1;
result = 31 * result + (int) bit0;
return result;
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me</groupId>
<artifactId>parser</artifactId>
<version>1.0</version>
<repositories>
<repository>
<id>atlassian-repo</id>
<name>Atlassian Repository</name>
<url>https://maven.atlassian.com/repository/public</url>
</repository>
<repository>
<id>limbo-repository</id>
<url>http://limbo.sourceforge.net/repository</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<repository>
<id>pecia-repository</id>
<url>http://pecia.sourceforge.net/repository</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.preon</groupId>
<artifactId>preon-binding</artifactId>
<version>1.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.codehaus.preon</groupId>
<artifactId>preon-el</artifactId>
<version>1.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.codehaus.preon</groupId>
<artifactId>preon-emitter</artifactId>
<version>1.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.codehaus.preon</groupId>
<artifactId>preon-io</artifactId>
<version>1.1-SNAPSHOT</version>
</dependency>
</dependencies>
我遇到了同样的问题。 ByteOrder.BigEndian
和 ByteOrder.LittleEndian
的解码似乎不同。但是,编码是相同的。这意味着当您使用 ByteOrder.LittleEndian
时,编码和解码之间的往返将不正确。
ByteOrder.BigEndian
的解码使用 MSB 0 位编号,ByteOrder.LittleEndian
的解码使用 LSB 0 位编号解码。考虑到这一点,我认为 ByteOrder.LittleEndian
的编码器坏了。
我 fork 了 preon 的当前存储库 (https://github.com/jdl17/preon/) and made the necessary changes. Along with these changes, I found that I needed to incorporate the flush fix that was originally submitted by mrumpf in a pull request (https://github.com/preon/preon/pull/25)。我进一步扩展了它以允许对刷新的字节顺序进行规范。
除了上述更改之外,该分支还包括实现 ListCodecFactory 的编码和解码方法以及 SelectFromCode 的编码方法的更改。最后,我将 ANTLR 从 3.3 更新到 3.5.2,因此它可以与 Java 8.
一起使用
所有这些都已作为拉取请求提交回 preon (https://github.com/preon/preon/pull/35)。
我还应该评论说我添加了例程以在小端和大端中正确编码布尔值。但是,Bound 的当前实现不允许 byteOrder 注释当前硬编码为 ByteOrder.BigEndian
。这超出了我的工作范围,我不需要使用布尔值进行解码和编码。但是,如果您打算在 ByteOrder.LittleEndian
编码中使用布尔值,请注意这一点。
希望这能解决您的问题。
除此之外,拉取请求还实现了 ListCodecFactory 的编码和解码方法以及 SelectFromCode 的编码方法。
背景
我正在尝试使用 preon library to encode/decode binary data. My 1 byte structure works fine when I configure the byte order for big endian, but fails for little endian. It appears that the endianess effects the intra-byte data in preon. It appears this 问题触及相同的问题,但没有解决小端方面的问题。
我附上了一个简单的案例来说明这个问题。
PreonLittleEndianNumber.java是否存在缺陷,或者当指定小端字节顺序时,preon 是否根本无法工作?
PreonTest.java
package me;
import org.codehaus.preon.Codec;
import org.codehaus.preon.Codecs;
import org.codehaus.preon.DecodingException;
import org.junit.Test;
import java.io.IOException;
import org.junit.Assert;
public class PreonTest {
@Test
public void bigEndianDecodeEncodeTest() {
byte[] testByte = {(byte) 0xDE};
Codec<PreonBigEndianNumber> bigCodec = Codecs.create(PreonBigEndianNumber.class);
try {
// Big Endian Decode/Encode Test
PreonBigEndianNumber pben = Codecs.decode(bigCodec, testByte);
byte[] testByte1 = Codecs.encode(pben, bigCodec);
Assert.assertArrayEquals(testByte, testByte1);
} catch (IOException e) {
e.printStackTrace();
} catch (DecodingException e) {
e.printStackTrace();
}
}
@Test
public void bigEndianEncodeDecodeTest() {
PreonBigEndianNumber pben = new PreonBigEndianNumber((byte)0xDE);
Codec<PreonBigEndianNumber> bigCodec = Codecs.create(PreonBigEndianNumber.class);
try {
byte[] testByte1 = Codecs.encode(pben, bigCodec);
PreonBigEndianNumber pben2 = Codecs.decode(bigCodec, testByte1);
Assert.assertEquals(pben, pben2);
} catch (IOException e) {
e.printStackTrace();
} catch (DecodingException e) {
e.printStackTrace();
}
}
@Test
public void littleEndianDecodeEncodeTest() {
byte[] testByte = {(byte) 0xDE};
Codec<PreonLittleEndianNumber> littleCodec = Codecs.create(PreonLittleEndianNumber.class);
try {
// Big Endian Decode/Encode Test
PreonLittleEndianNumber plen = Codecs.decode(littleCodec, testByte);
byte[] testByte1 = Codecs.encode(plen, littleCodec);
Assert.assertArrayEquals(testByte, testByte1);
} catch (IOException e) {
e.printStackTrace();
} catch (DecodingException e) {
e.printStackTrace();
}
}
@Test
public void littleEndianEncodeDecodeTest() {
PreonLittleEndianNumber plen = new PreonLittleEndianNumber((byte)0xDE);
Codec<PreonLittleEndianNumber> littleCodec = Codecs.create(PreonLittleEndianNumber.class);
try {
byte[] testByte1 = Codecs.encode(plen, littleCodec);
PreonLittleEndianNumber plen2 = Codecs.decode(littleCodec, testByte1);
Assert.assertEquals(plen, plen2);
} catch (IOException e) {
e.printStackTrace();
} catch (DecodingException e) {
e.printStackTrace();
}
}
}
PreonBigEndianNumber.java
package me;
import org.codehaus.preon.annotation.BoundNumber;
import org.codehaus.preon.buffer.ByteOrder;
public class PreonBigEndianNumber {
@BoundNumber(size="1", byteOrder=ByteOrder.BigEndian) public byte bit7;
@BoundNumber(size="1", byteOrder=ByteOrder.BigEndian) public byte bit6;
@BoundNumber(size="1", byteOrder=ByteOrder.BigEndian) public byte bit5;
@BoundNumber(size="1", byteOrder=ByteOrder.BigEndian) public byte bit4;
@BoundNumber(size="1", byteOrder=ByteOrder.BigEndian) public byte bit3;
@BoundNumber(size="1", byteOrder=ByteOrder.BigEndian) public byte bit2;
@BoundNumber(size="1", byteOrder=ByteOrder.BigEndian) public byte bit1;
@BoundNumber(size="1", byteOrder=ByteOrder.BigEndian) public byte bit0;
/**
* Default constructor
*/
public PreonBigEndianNumber() {
}
public PreonBigEndianNumber(byte value) {
bit7 = (byte) ((0b10000000 & value) >>> 7);
bit6 = (byte) ((0b01000000 & value) >>> 6);
bit5 = (byte) ((0b00100000 & value) >>> 5);
bit4 = (byte) ((0b00010000 & value) >>> 4);
bit3 = (byte) ((0b00001000 & value) >>> 3);
bit2 = (byte) ((0b00000100 & value) >>> 2);
bit1 = (byte) ((0b00000010 & value) >>> 1);
bit0 = (byte) ((0b00000001 & value) >>> 0);
}
public byte getByte() {
// Pack bits back into an int
int b = 0;
int shift = 0;
for (int i=7;i>=0;i--) {
b = (b << shift);
if (i==0) {b += bit0;}
else if (i==1) {b += bit1;}
else if (i==2) {b += bit2;}
else if (i==3) {b += bit3;}
else if (i==4) {b += bit4;}
else if (i==5) {b += bit5;}
else if (i==6) {b += bit6;}
else if (i==7) {b += bit7;}
shift = 1;
}
return (byte) b;
}
@Override
public String toString() {
return bit7 +
bit6 +
bit5 +
bit4 +
" " +
bit3 +
bit2 +
bit1 +
bit0;
}
@Override
public boolean equals(Object o) {
if (this == o) {return true;}
if (o == null || getClass() != o.getClass()) {return false;}
PreonBigEndianNumber preonBits = (PreonBigEndianNumber) o;
if (bit7 != preonBits.bit7) {return false;}
if (bit6 != preonBits.bit6) {return false;}
if (bit5 != preonBits.bit5) {return false;}
if (bit4 != preonBits.bit4) {return false;}
if (bit3 != preonBits.bit3) {return false;}
if (bit2 != preonBits.bit2) {return false;}
if (bit1 != preonBits.bit1) {return false;}
return bit0 == preonBits.bit0;
}
@Override
public int hashCode() {
int result = (int) bit7;
result = 31 * result + (int) bit6;
result = 31 * result + (int) bit5;
result = 31 * result + (int) bit4;
result = 31 * result + (int) bit3;
result = 31 * result + (int) bit2;
result = 31 * result + (int) bit1;
result = 31 * result + (int) bit0;
return result;
}
}
PreonLittleEndianNumber.java
package me;
import org.codehaus.preon.annotation.BoundNumber;
import org.codehaus.preon.buffer.ByteOrder;
public class PreonLittleEndianNumber {
@BoundNumber(size="1", byteOrder=ByteOrder.LittleEndian) public byte bit7;
@BoundNumber(size="1", byteOrder=ByteOrder.LittleEndian) public byte bit6;
@BoundNumber(size="1", byteOrder=ByteOrder.LittleEndian) public byte bit5;
@BoundNumber(size="1", byteOrder=ByteOrder.LittleEndian) public byte bit4;
@BoundNumber(size="1", byteOrder=ByteOrder.LittleEndian) public byte bit3;
@BoundNumber(size="1", byteOrder=ByteOrder.LittleEndian) public byte bit2;
@BoundNumber(size="1", byteOrder=ByteOrder.LittleEndian) public byte bit1;
@BoundNumber(size="1", byteOrder=ByteOrder.LittleEndian) public byte bit0;
/**
* Default constructor
*/
public PreonLittleEndianNumber() {
}
public PreonLittleEndianNumber(byte value) {
bit7 = (byte) ((0b10000000 & value) >>> 7);
bit6 = (byte) ((0b01000000 & value) >>> 6);
bit5 = (byte) ((0b00100000 & value) >>> 5);
bit4 = (byte) ((0b00010000 & value) >>> 4);
bit3 = (byte) ((0b00001000 & value) >>> 3);
bit2 = (byte) ((0b00000100 & value) >>> 2);
bit1 = (byte) ((0b00000010 & value) >>> 1);
bit0 = (byte) ((0b00000001 & value) >>> 0);
}
public byte getByte() {
// Pack bits back into an int
int b = 0;
int shift = 0;
for (int i=7;i>=0;i--) {
b = (b << shift);
if (i==0) {b += bit0;}
else if (i==1) {b += bit1;}
else if (i==2) {b += bit2;}
else if (i==3) {b += bit3;}
else if (i==4) {b += bit4;}
else if (i==5) {b += bit5;}
else if (i==6) {b += bit6;}
else if (i==7) {b += bit7;}
shift = 1;
}
return (byte) b;
}
@Override
public String toString() {
return bit7 +
bit6 +
bit5 +
bit4 +
" " +
bit3 +
bit2 +
bit1 +
bit0;
}
@Override
public boolean equals(Object o) {
if (this == o) {return true;}
if (o == null || getClass() != o.getClass()) {return false;}
PreonLittleEndianNumber preonBits = (PreonLittleEndianNumber) o;
if (bit7 != preonBits.bit7) {return false;}
if (bit6 != preonBits.bit6) {return false;}
if (bit5 != preonBits.bit5) {return false;}
if (bit4 != preonBits.bit4) {return false;}
if (bit3 != preonBits.bit3) {return false;}
if (bit2 != preonBits.bit2) {return false;}
if (bit1 != preonBits.bit1) {return false;}
return bit0 == preonBits.bit0;
}
@Override
public int hashCode() {
int result = (int) bit7;
result = 31 * result + (int) bit6;
result = 31 * result + (int) bit5;
result = 31 * result + (int) bit4;
result = 31 * result + (int) bit3;
result = 31 * result + (int) bit2;
result = 31 * result + (int) bit1;
result = 31 * result + (int) bit0;
return result;
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me</groupId>
<artifactId>parser</artifactId>
<version>1.0</version>
<repositories>
<repository>
<id>atlassian-repo</id>
<name>Atlassian Repository</name>
<url>https://maven.atlassian.com/repository/public</url>
</repository>
<repository>
<id>limbo-repository</id>
<url>http://limbo.sourceforge.net/repository</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<repository>
<id>pecia-repository</id>
<url>http://pecia.sourceforge.net/repository</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.preon</groupId>
<artifactId>preon-binding</artifactId>
<version>1.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.codehaus.preon</groupId>
<artifactId>preon-el</artifactId>
<version>1.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.codehaus.preon</groupId>
<artifactId>preon-emitter</artifactId>
<version>1.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.codehaus.preon</groupId>
<artifactId>preon-io</artifactId>
<version>1.1-SNAPSHOT</version>
</dependency>
</dependencies>
我遇到了同样的问题。 ByteOrder.BigEndian
和 ByteOrder.LittleEndian
的解码似乎不同。但是,编码是相同的。这意味着当您使用 ByteOrder.LittleEndian
时,编码和解码之间的往返将不正确。
ByteOrder.BigEndian
的解码使用 MSB 0 位编号,ByteOrder.LittleEndian
的解码使用 LSB 0 位编号解码。考虑到这一点,我认为 ByteOrder.LittleEndian
的编码器坏了。
我 fork 了 preon 的当前存储库 (https://github.com/jdl17/preon/) and made the necessary changes. Along with these changes, I found that I needed to incorporate the flush fix that was originally submitted by mrumpf in a pull request (https://github.com/preon/preon/pull/25)。我进一步扩展了它以允许对刷新的字节顺序进行规范。
除了上述更改之外,该分支还包括实现 ListCodecFactory 的编码和解码方法以及 SelectFromCode 的编码方法的更改。最后,我将 ANTLR 从 3.3 更新到 3.5.2,因此它可以与 Java 8.
一起使用所有这些都已作为拉取请求提交回 preon (https://github.com/preon/preon/pull/35)。
我还应该评论说我添加了例程以在小端和大端中正确编码布尔值。但是,Bound 的当前实现不允许 byteOrder 注释当前硬编码为 ByteOrder.BigEndian
。这超出了我的工作范围,我不需要使用布尔值进行解码和编码。但是,如果您打算在 ByteOrder.LittleEndian
编码中使用布尔值,请注意这一点。
希望这能解决您的问题。
除此之外,拉取请求还实现了 ListCodecFactory 的编码和解码方法以及 SelectFromCode 的编码方法。