Skip to content

Commit 1f7b5c6

Browse files
committed
Modularize ROS2 codecs and add missing message types
1 parent 0f59643 commit 1f7b5c6

41 files changed

Lines changed: 3311 additions & 2179 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

fastproto-core/src/main/java/org/indunet/fastproto/FastProto.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
* @author Deng Ran
3232
* @since 1.0.0
3333
*/
34-
public class FastProto {
34+
public final class FastProto {
35+
private FastProto() {
36+
}
37+
3538
/**
3639
* Converts binary data into a Java object with FastProto annotations.
3740
*

fastproto-ros2/src/main/java/org/indunet/fastproto/ros2/Ros2CdrReader.java

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.indunet.fastproto.ByteOrder;
44
import org.indunet.fastproto.io.ByteBufferInputStream;
55

6+
import java.math.BigInteger;
67
import java.nio.charset.StandardCharsets;
78
import java.util.Arrays;
89

@@ -32,11 +33,26 @@ public int readInt32() {
3233
return inputStream.readInt32(ByteOrder.LITTLE);
3334
}
3435

36+
public int readInt16() {
37+
inputStream.align(2);
38+
return inputStream.readInt16(ByteOrder.LITTLE);
39+
}
40+
41+
public long readInt64() {
42+
inputStream.align(8);
43+
return inputStream.readInt64(ByteOrder.LITTLE);
44+
}
45+
3546
public long readUInt32() {
3647
inputStream.align(4);
3748
return inputStream.readUInt32(ByteOrder.LITTLE);
3849
}
3950

51+
public BigInteger readUInt64() {
52+
inputStream.align(8);
53+
return inputStream.readUInt64(ByteOrder.LITTLE);
54+
}
55+
4056
public int readUInt16() {
4157
inputStream.align(2);
4258
return inputStream.readUInt16(ByteOrder.LITTLE);
@@ -133,6 +149,76 @@ public int[] readInt32Sequence() {
133149
return values;
134150
}
135151

152+
public int[] readInt16Sequence() {
153+
long length = readUInt32();
154+
if (length < 0 || length > Integer.MAX_VALUE) {
155+
throw new IllegalArgumentException("Invalid ROS2 int16 sequence length: " + length);
156+
}
157+
158+
int[] values = new int[(int) length];
159+
for (int i = 0; i < values.length; i++) {
160+
values[i] = readInt16();
161+
}
162+
163+
return values;
164+
}
165+
166+
public long[] readInt64Sequence() {
167+
long length = readUInt32();
168+
if (length < 0 || length > Integer.MAX_VALUE) {
169+
throw new IllegalArgumentException("Invalid ROS2 int64 sequence length: " + length);
170+
}
171+
172+
long[] values = new long[(int) length];
173+
for (int i = 0; i < values.length; i++) {
174+
values[i] = readInt64();
175+
}
176+
177+
return values;
178+
}
179+
180+
public int[] readUInt16Sequence() {
181+
long length = readUInt32();
182+
if (length < 0 || length > Integer.MAX_VALUE) {
183+
throw new IllegalArgumentException("Invalid ROS2 uint16 sequence length: " + length);
184+
}
185+
186+
int[] values = new int[(int) length];
187+
for (int i = 0; i < values.length; i++) {
188+
values[i] = readUInt16();
189+
}
190+
191+
return values;
192+
}
193+
194+
public long[] readUInt32Sequence() {
195+
long length = readUInt32();
196+
if (length < 0 || length > Integer.MAX_VALUE) {
197+
throw new IllegalArgumentException("Invalid ROS2 uint32 sequence length: " + length);
198+
}
199+
200+
long[] values = new long[(int) length];
201+
for (int i = 0; i < values.length; i++) {
202+
values[i] = readUInt32();
203+
}
204+
205+
return values;
206+
}
207+
208+
public BigInteger[] readUInt64Sequence() {
209+
long length = readUInt32();
210+
if (length < 0 || length > Integer.MAX_VALUE) {
211+
throw new IllegalArgumentException("Invalid ROS2 uint64 sequence length: " + length);
212+
}
213+
214+
BigInteger[] values = new BigInteger[(int) length];
215+
for (int i = 0; i < values.length; i++) {
216+
values[i] = readUInt64();
217+
}
218+
219+
return values;
220+
}
221+
136222
public String[] readStringSequence() {
137223
long length = readUInt32();
138224
if (length < 0 || length > Integer.MAX_VALUE) {

fastproto-ros2/src/main/java/org/indunet/fastproto/ros2/Ros2CdrWriter.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.indunet.fastproto.ByteOrder;
44
import org.indunet.fastproto.io.ByteBufferOutputStream;
55

6+
import java.math.BigInteger;
67
import java.nio.charset.StandardCharsets;
78

89
/**
@@ -26,11 +27,26 @@ public void writeInt32(int value) {
2627
outputStream.writeInt32(ByteOrder.LITTLE, value);
2728
}
2829

30+
public void writeInt16(int value) {
31+
outputStream.align(2);
32+
outputStream.writeInt16(ByteOrder.LITTLE, value);
33+
}
34+
35+
public void writeInt64(long value) {
36+
outputStream.align(8);
37+
outputStream.writeInt64(ByteOrder.LITTLE, value);
38+
}
39+
2940
public void writeUInt32(long value) {
3041
outputStream.align(4);
3142
outputStream.writeUInt32(ByteOrder.LITTLE, value);
3243
}
3344

45+
public void writeUInt64(BigInteger value) {
46+
outputStream.align(8);
47+
outputStream.writeUInt64(ByteOrder.LITTLE, value);
48+
}
49+
3450
public void writeUInt16(int value) {
3551
outputStream.align(2);
3652
outputStream.writeUInt16(ByteOrder.LITTLE, value);
@@ -95,6 +111,41 @@ public void writeInt32Sequence(int[] values) {
95111
}
96112
}
97113

114+
public void writeInt16Sequence(int[] values) {
115+
writeUInt32(values.length);
116+
for (int value : values) {
117+
writeInt16(value);
118+
}
119+
}
120+
121+
public void writeInt64Sequence(long[] values) {
122+
writeUInt32(values.length);
123+
for (long value : values) {
124+
writeInt64(value);
125+
}
126+
}
127+
128+
public void writeUInt16Sequence(int[] values) {
129+
writeUInt32(values.length);
130+
for (int value : values) {
131+
writeUInt16(value);
132+
}
133+
}
134+
135+
public void writeUInt32Sequence(long[] values) {
136+
writeUInt32(values.length);
137+
for (long value : values) {
138+
writeUInt32(value);
139+
}
140+
}
141+
142+
public void writeUInt64Sequence(BigInteger[] values) {
143+
writeUInt32(values.length);
144+
for (BigInteger value : values) {
145+
writeUInt64(value);
146+
}
147+
}
148+
98149
public void writeStringSequence(String[] values) {
99150
writeUInt32(values.length);
100151
for (String value : values) {

0 commit comments

Comments
 (0)