You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by cd...@apache.org on 2022/06/13 13:26:59 UTC
[plc4x] branch develop updated: chore(plc4j): Added a first implementation of a component for helping with variable length encoded unsigned integers
This is an automated email from the ASF dual-hosted git repository.
cdutz pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/plc4x.git
The following commit(s) were added to refs/heads/develop by this push:
new 1fc36e24ae chore(plc4j): Added a first implementation of a component for helping with variable length encoded unsigned integers
1fc36e24ae is described below
commit 1fc36e24ae3a66d9f44b81383c4620bca6dccfa3
Author: cdutz <ch...@c-ware.de>
AuthorDate: Mon Jun 13 15:26:51 2022 +0200
chore(plc4j): Added a first implementation of a component for helping with variable length encoded unsigned integers
---
.../src/test/resources/mspec.example | 3 +
.../apache/plc4x/java/spi/codegen/io/VintIo.java | 69 ++++++++++++++++++++++
.../plc4x/java/spi/codegen/io/VintIoTest.java | 63 ++++++++++++++++++++
3 files changed, 135 insertions(+)
diff --git a/code-generation/protocol-base-mspec/src/test/resources/mspec.example b/code-generation/protocol-base-mspec/src/test/resources/mspec.example
index e75d55d21f..5c98e8e1e0 100644
--- a/code-generation/protocol-base-mspec/src/test/resources/mspec.example
+++ b/code-generation/protocol-base-mspec/src/test/resources/mspec.example
@@ -365,8 +365,11 @@
]
[type VariableIntegerType
+ // A default vint will be being backed by a 32 bit property
[simple vint variableLengthInt ]
+ // A default vuint will be being backed by a 32 bit property
[simple vuint variableLengthUnsignedInt ]
+ // We can override the property size, by specifying the bit size in an attribute
[simple vint variableLengthIntWithNonDefaultProperty propertySizeInBits='8' ]
[simple vuint variableLengthUnsignedInt propertySizeInBits='16']
]
\ No newline at end of file
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/codegen/io/VintIo.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/codegen/io/VintIo.java
new file mode 100644
index 0000000000..7ffd887a37
--- /dev/null
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/codegen/io/VintIo.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.plc4x.java.spi.codegen.io;
+
+import org.apache.plc4x.java.spi.generation.ParseException;
+import org.apache.plc4x.java.spi.generation.ReadBuffer;
+import org.apache.plc4x.java.spi.generation.SerializationException;
+import org.apache.plc4x.java.spi.generation.WriteBuffer;
+
+public class VintIo {
+
+ public static int getLengthInBytes(long value) {
+ int numBytes = 1;
+ // Keep on shifting the value left 7 bits, till the value is 0.
+ while((value & 0xFFFFFF80) != 0) {
+ numBytes++;
+ value = value >> 7;
+ }
+ return numBytes;
+ }
+
+ public static void serialize(long value, WriteBuffer writeBuffer) throws SerializationException {
+ serializeInternally(value, writeBuffer, true);
+ }
+
+ private static void serializeInternally(long value, WriteBuffer writeBuffer, boolean lastByte) throws SerializationException {
+ byte curValue = (byte) ((value & 0x7F) | (lastByte ? 0x00 : 0x80));
+ long restValue = value >> 7;
+ // Recursively serialize the others first.
+ if(restValue != 0) {
+ serializeInternally(restValue, writeBuffer, false);
+ }
+ // Now serialize the last byte.
+ writeBuffer.writeUnsignedShort(8, curValue);
+ }
+
+ public static long parse(ReadBuffer readBuffer) throws ParseException {
+ long curValue = 0;
+ short curByte;
+ do {
+ // Get the next byte
+ curByte = readBuffer.readUnsignedShort(8);
+ // Add the 7 least significant bits to the value.
+ curValue |= (curByte & 0x7F);
+ // If the highest bit is set, shift the value right 7 bits and read the next byte.
+ if((curByte & 0x80) != 0) {
+ curValue = curValue << 7;
+ }
+ } while ((curByte & 0x80) != 0);
+ return curValue;
+ }
+
+}
diff --git a/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/codegen/io/VintIoTest.java b/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/codegen/io/VintIoTest.java
new file mode 100644
index 0000000000..8186b505fa
--- /dev/null
+++ b/plc4j/spi/src/test/java/org/apache/plc4x/java/spi/codegen/io/VintIoTest.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.plc4x.java.spi.codegen.io;
+
+import org.apache.plc4x.java.spi.generation.ReadBuffer;
+import org.apache.plc4x.java.spi.generation.ReadBufferByteBased;
+import org.apache.plc4x.java.spi.generation.WriteBufferByteBased;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+public class VintIoTest {
+
+ @ParameterizedTest
+ @ValueSource(longs = {0, 1, 23, 42, 126, 127})
+ void oneByteTest(long value) throws Exception {
+ roundtrip(value, 1);
+ }
+
+ @ParameterizedTest
+ @ValueSource(longs = {128, 1234, 12345})
+ void twoByteTest(long value) throws Exception {
+ roundtrip(value, 2);
+ }
+
+ @ParameterizedTest
+ @ValueSource(longs = {123456})
+ void threeByteTest(long value) throws Exception {
+ roundtrip(value, 3);
+ }
+
+ private void roundtrip(long value, int expectedNumBytes) throws Exception {
+ Assertions.assertEquals(expectedNumBytes, VintIo.getLengthInBytes(value));
+ WriteBufferByteBased writeBuffer = new WriteBufferByteBased(10);
+ VintIo.serialize(value, writeBuffer);
+ Assertions.assertEquals(expectedNumBytes, writeBuffer.getPos());
+ ReadBuffer readBuffer = new ReadBufferByteBased(writeBuffer.getBytes());
+ Assertions.assertEquals(value, VintIo.parse(readBuffer));
+ // For the first bytes the top most bit must be set to 1.
+ for(int i = 0; i < (expectedNumBytes - 1); i++) {
+ Assertions.assertEquals(0x80, writeBuffer.getBytes()[i] & 0x80);
+ }
+ // For the last byte the top most bit must be set to 0.
+ Assertions.assertEquals(0x00, writeBuffer.getBytes()[expectedNumBytes - 1] & 0x80);
+ }
+
+}