Skip to content

Commit

Permalink
feat: use native byte order as default
Browse files Browse the repository at this point in the history
1. use native byte order as default;
2. add AbstractProtocol class.
  • Loading branch information
Deng-Ran committed Oct 4, 2023
1 parent ffdc745 commit ca24bc9
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.indunet</groupId>
<artifactId>fastproto</artifactId>
<version>3.10.3</version>
<version>3.11.0</version>
<packaging>jar</packaging>
<name>FastProto</name>
<description>FastProto is a binary data processing tool written in Java.</description>
Expand Down Expand Up @@ -145,7 +145,7 @@

<properties>
<junit.version>5.7.2</junit.version>
<lombok.version>1.18.16</lombok.version>
<lombok.version>1.18.22</lombok.version>
<scala.version>2.11.12</scala.version>
<jmh-core>1.3.2</jmh-core>
<jmh-generator-annprocess>1.3.2</jmh-generator-annprocess>
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/org/indunet/fastproto/AbstractProtocol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.indunet.fastproto;

/**
* Abstract protocol.
*
* @author Deng Ran
* @since 3.11.0
*/
public abstract class AbstractProtocol {
public static final int NON_FIXED_LENGTH = -1;

protected int length = NON_FIXED_LENGTH;
protected BitOrder bitOrder = BitOrder.LSB_0;
protected ByteOrder byteOrder = ByteOrder.nativeOrder();

public byte[] toByteArray() {
if (length == NON_FIXED_LENGTH) {
return FastProto.encode(this);
} else {
return FastProto.encode(this, length);
}
}

public String toHexString() {
StringBuilder hexString = new StringBuilder();

for (byte b : toByteArray()) {
hexString.append(String.format("%02x", b));
}

return hexString.toString();
}

public void printHexString() {
System.out.println(toHexString());
}
}
8 changes: 8 additions & 0 deletions src/main/java/org/indunet/fastproto/ByteOrder.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@ public enum ByteOrder {

final int code;
final String name;

public static ByteOrder nativeOrder() {
if (java.nio.ByteOrder.nativeOrder().equals(java.nio.ByteOrder.LITTLE_ENDIAN)) {
return LITTLE;
} else {
return BIG;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,23 @@
* @since 2.5.0
*/
public class ByteOrderFlow extends ResolvePipeline {
protected final static ByteOrder DEFAULT_BYTE_ORDER = ByteOrder.LITTLE;

@Override
public void process(@NonNull Reference reference) {
if (reference.getReferenceType() == Reference.ReferenceType.CLASS) {
val protocolClass = reference.getProtocolClass();
val endianPolicy = Optional.ofNullable(protocolClass.getAnnotation(DefaultByteOrder.class))
.map(DefaultByteOrder::value)
.orElse(DEFAULT_BYTE_ORDER);
.orElse(ByteOrder.nativeOrder());

reference.setByteOrder(endianPolicy);
} else if (reference.getReferenceType() == Reference.ReferenceType.FIELD) {
val field = reference.getField();
val endianPolicy = Optional.ofNullable(field.getAnnotation(DefaultByteOrder.class))
.map(DefaultByteOrder::value)
.orElseGet(() -> Optional.ofNullable(reference.getField().getDeclaringClass())
.orElseGet(() -> Optional.of(reference.getField().getDeclaringClass())
.map(c -> c.getAnnotation(DefaultByteOrder.class))
.map(DefaultByteOrder::value)
.orElse(DEFAULT_BYTE_ORDER)); // Inherit endian of declaring class.
.orElse(ByteOrder.nativeOrder())); // Inherit endian of declaring class.

reference.setByteOrder(endianPolicy);
}
Expand Down

0 comments on commit ca24bc9

Please sign in to comment.