summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/uic/barcode/dynamicFrame/api/DynamicFrameCoder.java
blob: 091cd4cdbba8114bc28055e888e1af91fcc10fb7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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 (Throwable e) {
		    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(Throwable e) {
			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());
	}

}