1 package au.gov.amsa.ais;
2
3 import au.gov.amsa.util.SixBit;
4 import au.gov.amsa.util.SixBitException;
5
6
7
8
9
10
11
12
13 public class AisExtractor {
14
15 private final boolean[] bitSet;
16 private final boolean[] calculated;
17 private final int padBits;
18 private final String message;
19
20
21
22
23
24
25
26
27 public AisExtractor(String message, Integer minLength, int padBits) {
28 if (message.length() == 0)
29 throw new AisParseException("message length cannot be 0");
30 if (padBits > 6 || padBits < 0)
31 throw new AisParseException("padBits must be between 0 and 6");
32 this.message = message;
33 boolean[] bits = new boolean[message.length() * 6 - padBits];
34 boolean[] calculated = new boolean[message.length()];
35 this.bitSet = bits;
36 this.calculated = calculated;
37 this.padBits = padBits;
38 if (minLength != null && bitSet.length < minLength) {
39 throw new AisParseException(AisParseException.NOT_CONSISTENT_DECODED_STRING
40 + ", length was " + bitSet.length + " and should be >=" + minLength);
41 }
42 }
43
44
45
46
47
48
49
50 public int getMessageId() {
51 return getValue(0, 6);
52 }
53
54
55
56
57
58
59
60
61
62 public synchronized int getValue(int from, int to) {
63 try {
64
65
66
67 SixBit.convertSixBitToBits(message, padBits, bitSet, calculated, from, to);
68 return (int) SixBit.getValue(from, to, bitSet);
69 } catch (SixBitException | ArrayIndexOutOfBoundsException e) {
70 throw new AisParseException(e);
71 }
72 }
73
74
75
76
77
78
79
80
81
82 public synchronized int getSignedValue(int from, int to) {
83 try {
84
85
86
87 SixBit.convertSixBitToBits(message, padBits, bitSet, calculated, from, to);
88 return (int) SixBit.getSignedValue(from, to, bitSet);
89 } catch (SixBitException e) {
90 throw new AisParseException(e);
91 }
92 }
93
94 public synchronized String getString(int from, int to) {
95 try {
96
97
98
99 SixBit.convertSixBitToBits(message, padBits, bitSet, calculated, from, to);
100 return SixBit.getString(from, to, bitSet);
101 } catch (SixBitException e) {
102 throw new AisParseException(e);
103 }
104 }
105
106 }