diff options
author | CGantert345 <57003061+CGantert345@users.noreply.github.com> | 2022-02-10 14:00:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-10 14:00:06 +0100 |
commit | 7fe844da786691bf839f169bcab8e71b5021329e (patch) | |
tree | dad727a39dc76d330fe28480e24e8a846c36b760 /src/main/java/org/uic/barcode/dynamicFrame/api/DynamicFrameCoder.java | |
parent | Merge pull request #41 from UnionInternationalCheminsdeFer/1.2.13 (diff) | |
parent | version number update (diff) | |
download | UIC-barcode-1.3.0.tar UIC-barcode-1.3.0.tar.gz UIC-barcode-1.3.0.tar.bz2 UIC-barcode-1.3.0.tar.lz UIC-barcode-1.3.0.tar.xz UIC-barcode-1.3.0.tar.zst UIC-barcode-1.3.0.zip |
Diffstat (limited to '')
-rw-r--r-- | src/main/java/org/uic/barcode/dynamicFrame/api/DynamicFrameCoder.java | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/src/main/java/org/uic/barcode/dynamicFrame/api/DynamicFrameCoder.java b/src/main/java/org/uic/barcode/dynamicFrame/api/DynamicFrameCoder.java new file mode 100644 index 0000000..53efb3e --- /dev/null +++ b/src/main/java/org/uic/barcode/dynamicFrame/api/DynamicFrameCoder.java @@ -0,0 +1,107 @@ +package org.uic.barcode.dynamicFrame.api;
+
+import org.uic.barcode.dynamicFrame.Constants;
+import org.uic.barcode.dynamicFrame.v1.DynamicFrameCoderV1;
+import org.uic.barcode.dynamicFrame.v2.DynamicFrameCoderV2;
+import org.uic.barcode.ticket.EncodingFormatException;
+
+public class DynamicFrameCoder {
+
+ /**
+ * Encode.
+ *
+ * Encode the header as ASN.1 PER UNALIGNED byte array
+ *
+ * @return the byte[]
+ * @throws EncodingFormatException
+ */
+ public static byte[] encode(IDynamicFrame frame) throws EncodingFormatException {
+
+ if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1.equals(frame.getFormat())) {
+
+ return DynamicFrameCoderV1.encode(frame);
+
+ } else if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2.equals(frame.getFormat())) {
+
+ return DynamicFrameCoderV2.encode(frame);
+
+ }
+
+ throw new EncodingFormatException("Frame version not supported for encoding");
+ }
+
+
+ /**
+ * Decode.
+ *
+ * Decode the header from an ASN.1 PER UNALIGNED encoded byte array
+ *
+ * @param bytes the bytes
+ * @return the dynamic header
+ * @throws EncodingFormatException
+ */
+ public static IDynamicFrame decode(byte[] bytes) throws EncodingFormatException {
+
+ IDynamicFrame frame = new SimpleDynamicFrame();
+
+ try {
+ DynamicFrameCoderV1.decode(frame,bytes);
+
+ if (frame.getFormat() != null && frame.getFormat().equals(Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1)) {
+ return frame;
+ }
+ } catch(Exception e1) {
+ frame = null;
+ // failed, try next
+ }
+
+ frame = new SimpleDynamicFrame();
+ try {
+ DynamicFrameCoderV2.decode(frame,bytes);
+
+ if (frame.getFormat() != null && frame.getFormat().equals(Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2)) {
+ return frame;
+ }
+ } catch(Exception e1) {
+ throw new EncodingFormatException("Dynamic Header Version not supported");
+ // failed
+ }
+
+ throw new EncodingFormatException("Dynamic Header Version not supported");
+
+ }
+
+
+ public static byte[] encodeLevel1(IDynamicFrame frame) throws EncodingFormatException {
+
+ if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1.equals(frame.getFormat())) {
+
+ return DynamicFrameCoderV1.encodeLevel1(frame);
+
+ } else if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2.equals(frame.getFormat())) {
+
+ return DynamicFrameCoderV2.encodeLevel1(frame);
+
+ }
+
+ throw new EncodingFormatException("Frame version not supported for encoding");
+
+ }
+
+
+ public static byte[] encodeLevel2Data(IDynamicFrame frame) throws EncodingFormatException {
+
+ if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_1.equals(frame.getFormat())) {
+
+ return DynamicFrameCoderV1.encodeLevel2Data(frame.getLevel2Data());
+
+ } else if (Constants.DYNAMIC_BARCODE_FORMAT_VERSION_2.equals(frame.getFormat())) {
+
+ return DynamicFrameCoderV2.encodeLevel2Data(frame.getLevel2Data());
+
+ }
+
+ throw new EncodingFormatException("Dynamic Header Version not supported: " + frame.getFormat());
+ }
+
+}
|