@Test public void dataBOOLEAN() throws SecsException { Data<?> data = MessageParser.parseData("<BOOLEAN True>"); Assert.assertTrue(data instanceof BOOLEAN); BOOLEAN b = (BOOLEAN) data; Assert.assertTrue(b.getValue()); data = MessageParser.parseData("<BOOLEAN False>"); Assert.assertTrue(data instanceof BOOLEAN); b = (BOOLEAN) data; Assert.assertFalse(b.getValue()); }
private static Data<?> parseData(byte[] data, int offset) throws SecsParseException { if (data.length < 2) { throw new SecsParseException("Invalid data length: " + data.length); } int formatByte = data[offset]; int formatCode = formatByte & 0xfc; int noOfLengthBytes = formatByte & 0x03; if (noOfLengthBytes < 1 || noOfLengthBytes > LENGTH_LENGTH) { throw new SecsParseException("Invalid number of length bytes: " + noOfLengthBytes); } if (data.length < noOfLengthBytes + 1) { throw new SecsParseException("Incomplete message data"); } int length = data[offset + 1]; if (noOfLengthBytes > 1) { length |= (data[offset + 2] << 8); } if (noOfLengthBytes > 2) { length |= (data[offset + 3] << 16); } if (noOfLengthBytes > 3) { length |= (data[offset + 4] << 24); } if (data.length < offset + 2 + length) { throw new SecsParseException("Incomplete message data"); } Data<?> dataItem = null; offset += (1 + noOfLengthBytes); switch (formatCode) { case L.FORMAT_CODE: dataItem = parseL(data, offset, length); break; case BOOLEAN.FORMAT_CODE: dataItem = parseBoolean(data, offset, length); break; case B.FORMAT_CODE: dataItem = parseB(data, offset, length); break; case A.FORMAT_CODE: dataItem = parseA(data, offset, length); break; case I1.FORMAT_CODE: dataItem = parseI1(data, offset, length); break; case I2.FORMAT_CODE: dataItem = parseI2(data, offset, length); break; case I4.FORMAT_CODE: dataItem = parseI4(data, offset, length); break; case I8.FORMAT_CODE: dataItem = parseI8(data, offset, length); break; case U1.FORMAT_CODE: dataItem = parseU1(data, offset, length); break; case U2.FORMAT_CODE: dataItem = parseU2(data, offset, length); break; case U4.FORMAT_CODE: dataItem = parseU4(data, offset, length); break; case U8.FORMAT_CODE: dataItem = parseU8(data, offset, length); break; case F4.FORMAT_CODE: dataItem = parseF4(data, offset, length); break; case F8.FORMAT_CODE: dataItem = parseF8(data, offset, length); break; default: throw new IllegalArgumentException(String.format("Invalid format code in message data: %02x", formatCode)); } return dataItem; }
MessageParser { private static Data<?> parseData(byte[] data, int offset) throws SecsParseException { if (data.length < 2) { throw new SecsParseException("Invalid data length: " + data.length); } int formatByte = data[offset]; int formatCode = formatByte & 0xfc; int noOfLengthBytes = formatByte & 0x03; if (noOfLengthBytes < 1 || noOfLengthBytes > LENGTH_LENGTH) { throw new SecsParseException("Invalid number of length bytes: " + noOfLengthBytes); } if (data.length < noOfLengthBytes + 1) { throw new SecsParseException("Incomplete message data"); } int length = data[offset + 1]; if (noOfLengthBytes > 1) { length |= (data[offset + 2] << 8); } if (noOfLengthBytes > 2) { length |= (data[offset + 3] << 16); } if (noOfLengthBytes > 3) { length |= (data[offset + 4] << 24); } if (data.length < offset + 2 + length) { throw new SecsParseException("Incomplete message data"); } Data<?> dataItem = null; offset += (1 + noOfLengthBytes); switch (formatCode) { case L.FORMAT_CODE: dataItem = parseL(data, offset, length); break; case BOOLEAN.FORMAT_CODE: dataItem = parseBoolean(data, offset, length); break; case B.FORMAT_CODE: dataItem = parseB(data, offset, length); break; case A.FORMAT_CODE: dataItem = parseA(data, offset, length); break; case I1.FORMAT_CODE: dataItem = parseI1(data, offset, length); break; case I2.FORMAT_CODE: dataItem = parseI2(data, offset, length); break; case I4.FORMAT_CODE: dataItem = parseI4(data, offset, length); break; case I8.FORMAT_CODE: dataItem = parseI8(data, offset, length); break; case U1.FORMAT_CODE: dataItem = parseU1(data, offset, length); break; case U2.FORMAT_CODE: dataItem = parseU2(data, offset, length); break; case U4.FORMAT_CODE: dataItem = parseU4(data, offset, length); break; case U8.FORMAT_CODE: dataItem = parseU8(data, offset, length); break; case F4.FORMAT_CODE: dataItem = parseF4(data, offset, length); break; case F8.FORMAT_CODE: dataItem = parseF8(data, offset, length); break; default: throw new IllegalArgumentException(String.format("Invalid format code in message data: %02x", formatCode)); } return dataItem; } }
MessageParser { private static Data<?> parseData(byte[] data, int offset) throws SecsParseException { if (data.length < 2) { throw new SecsParseException("Invalid data length: " + data.length); } int formatByte = data[offset]; int formatCode = formatByte & 0xfc; int noOfLengthBytes = formatByte & 0x03; if (noOfLengthBytes < 1 || noOfLengthBytes > LENGTH_LENGTH) { throw new SecsParseException("Invalid number of length bytes: " + noOfLengthBytes); } if (data.length < noOfLengthBytes + 1) { throw new SecsParseException("Incomplete message data"); } int length = data[offset + 1]; if (noOfLengthBytes > 1) { length |= (data[offset + 2] << 8); } if (noOfLengthBytes > 2) { length |= (data[offset + 3] << 16); } if (noOfLengthBytes > 3) { length |= (data[offset + 4] << 24); } if (data.length < offset + 2 + length) { throw new SecsParseException("Incomplete message data"); } Data<?> dataItem = null; offset += (1 + noOfLengthBytes); switch (formatCode) { case L.FORMAT_CODE: dataItem = parseL(data, offset, length); break; case BOOLEAN.FORMAT_CODE: dataItem = parseBoolean(data, offset, length); break; case B.FORMAT_CODE: dataItem = parseB(data, offset, length); break; case A.FORMAT_CODE: dataItem = parseA(data, offset, length); break; case I1.FORMAT_CODE: dataItem = parseI1(data, offset, length); break; case I2.FORMAT_CODE: dataItem = parseI2(data, offset, length); break; case I4.FORMAT_CODE: dataItem = parseI4(data, offset, length); break; case I8.FORMAT_CODE: dataItem = parseI8(data, offset, length); break; case U1.FORMAT_CODE: dataItem = parseU1(data, offset, length); break; case U2.FORMAT_CODE: dataItem = parseU2(data, offset, length); break; case U4.FORMAT_CODE: dataItem = parseU4(data, offset, length); break; case U8.FORMAT_CODE: dataItem = parseU8(data, offset, length); break; case F4.FORMAT_CODE: dataItem = parseF4(data, offset, length); break; case F8.FORMAT_CODE: dataItem = parseF8(data, offset, length); break; default: throw new IllegalArgumentException(String.format("Invalid format code in message data: %02x", formatCode)); } return dataItem; }  }
MessageParser { private static Data<?> parseData(byte[] data, int offset) throws SecsParseException { if (data.length < 2) { throw new SecsParseException("Invalid data length: " + data.length); } int formatByte = data[offset]; int formatCode = formatByte & 0xfc; int noOfLengthBytes = formatByte & 0x03; if (noOfLengthBytes < 1 || noOfLengthBytes > LENGTH_LENGTH) { throw new SecsParseException("Invalid number of length bytes: " + noOfLengthBytes); } if (data.length < noOfLengthBytes + 1) { throw new SecsParseException("Incomplete message data"); } int length = data[offset + 1]; if (noOfLengthBytes > 1) { length |= (data[offset + 2] << 8); } if (noOfLengthBytes > 2) { length |= (data[offset + 3] << 16); } if (noOfLengthBytes > 3) { length |= (data[offset + 4] << 24); } if (data.length < offset + 2 + length) { throw new SecsParseException("Incomplete message data"); } Data<?> dataItem = null; offset += (1 + noOfLengthBytes); switch (formatCode) { case L.FORMAT_CODE: dataItem = parseL(data, offset, length); break; case BOOLEAN.FORMAT_CODE: dataItem = parseBoolean(data, offset, length); break; case B.FORMAT_CODE: dataItem = parseB(data, offset, length); break; case A.FORMAT_CODE: dataItem = parseA(data, offset, length); break; case I1.FORMAT_CODE: dataItem = parseI1(data, offset, length); break; case I2.FORMAT_CODE: dataItem = parseI2(data, offset, length); break; case I4.FORMAT_CODE: dataItem = parseI4(data, offset, length); break; case I8.FORMAT_CODE: dataItem = parseI8(data, offset, length); break; case U1.FORMAT_CODE: dataItem = parseU1(data, offset, length); break; case U2.FORMAT_CODE: dataItem = parseU2(data, offset, length); break; case U4.FORMAT_CODE: dataItem = parseU4(data, offset, length); break; case U8.FORMAT_CODE: dataItem = parseU8(data, offset, length); break; case F4.FORMAT_CODE: dataItem = parseF4(data, offset, length); break; case F8.FORMAT_CODE: dataItem = parseF8(data, offset, length); break; default: throw new IllegalArgumentException(String.format("Invalid format code in message data: %02x", formatCode)); } return dataItem; }  static Message parseMessage(byte[] data, int length, Map<Integer, Class<? extends SecsMessage>> messageTypes); static Data<?> parseData(String text); }
MessageParser { private static Data<?> parseData(byte[] data, int offset) throws SecsParseException { if (data.length < 2) { throw new SecsParseException("Invalid data length: " + data.length); } int formatByte = data[offset]; int formatCode = formatByte & 0xfc; int noOfLengthBytes = formatByte & 0x03; if (noOfLengthBytes < 1 || noOfLengthBytes > LENGTH_LENGTH) { throw new SecsParseException("Invalid number of length bytes: " + noOfLengthBytes); } if (data.length < noOfLengthBytes + 1) { throw new SecsParseException("Incomplete message data"); } int length = data[offset + 1]; if (noOfLengthBytes > 1) { length |= (data[offset + 2] << 8); } if (noOfLengthBytes > 2) { length |= (data[offset + 3] << 16); } if (noOfLengthBytes > 3) { length |= (data[offset + 4] << 24); } if (data.length < offset + 2 + length) { throw new SecsParseException("Incomplete message data"); } Data<?> dataItem = null; offset += (1 + noOfLengthBytes); switch (formatCode) { case L.FORMAT_CODE: dataItem = parseL(data, offset, length); break; case BOOLEAN.FORMAT_CODE: dataItem = parseBoolean(data, offset, length); break; case B.FORMAT_CODE: dataItem = parseB(data, offset, length); break; case A.FORMAT_CODE: dataItem = parseA(data, offset, length); break; case I1.FORMAT_CODE: dataItem = parseI1(data, offset, length); break; case I2.FORMAT_CODE: dataItem = parseI2(data, offset, length); break; case I4.FORMAT_CODE: dataItem = parseI4(data, offset, length); break; case I8.FORMAT_CODE: dataItem = parseI8(data, offset, length); break; case U1.FORMAT_CODE: dataItem = parseU1(data, offset, length); break; case U2.FORMAT_CODE: dataItem = parseU2(data, offset, length); break; case U4.FORMAT_CODE: dataItem = parseU4(data, offset, length); break; case U8.FORMAT_CODE: dataItem = parseU8(data, offset, length); break; case F4.FORMAT_CODE: dataItem = parseF4(data, offset, length); break; case F8.FORMAT_CODE: dataItem = parseF8(data, offset, length); break; default: throw new IllegalArgumentException(String.format("Invalid format code in message data: %02x", formatCode)); } return dataItem; }  static Message parseMessage(byte[] data, int length, Map<Integer, Class<? extends SecsMessage>> messageTypes); static Data<?> parseData(String text);  }