2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.caddx.internal;
15 import java.io.BufferedReader;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.InputStreamReader;
19 import java.util.stream.Collectors;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.core.util.HexUtils;
25 * Util class to read test input messages.
27 * @author Georgios Moutsos - Initial contribution
30 public final class CaddxMessageReaderUtil {
31 private static final String MESSAGE_EXT = ".msg";
33 private CaddxMessageReaderUtil() {
38 * Reads the raw bytes of the message given the file relative to this package and returns the objects.
40 * @param messageName name of the telegram file to read
41 * @return The raw bytes of a telegram
43 public static byte[] readRawMessage(String messageName) {
44 try (InputStream is = CaddxMessageReaderUtil.class.getResourceAsStream(messageName + MESSAGE_EXT);
45 InputStreamReader isr = new InputStreamReader(is);
46 BufferedReader br = new BufferedReader(isr)) {
47 String hexString = br.lines().collect(Collectors.joining("\n"));
49 return HexUtils.hexToBytes(hexString, " ");
50 } catch (IOException e) {
51 throw new AssertionError("IOException reading message data: ", e);
56 * Reads a message given the file relative to this package and returns the object.
58 * @param messageName name of the message file to read
59 * @return a CaddxMessage object
61 public static CaddxMessage readCaddxMessage(String messageName) {
62 byte[] bytes = readRawMessage(messageName);
63 return new CaddxMessage(CaddxMessageContext.NONE, bytes, true);