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.mybmw.internal.util;
15 import static org.junit.jupiter.api.Assertions.fail;
17 import java.io.BufferedReader;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.openhab.binding.mybmw.internal.utils.Constants;
27 * The {@link FileReader} Helper Util to read test resource files
29 * @author Bernd Weymann - Initial contribution
30 * @author Martin Grassl - added reading of image
33 public class FileReader {
36 * reads a file into a string
41 public static String fileToString(String filename) {
42 try (BufferedReader br = new BufferedReader(
43 new InputStreamReader(FileReader.class.getClassLoader().getResourceAsStream(filename), "UTF-8"))) {
44 StringBuilder buf = new StringBuilder();
47 while ((sCurrentLine = br.readLine()) != null) {
48 buf.append(sCurrentLine);
50 return buf != null ? buf.toString() : "";
51 } catch (IOException e) {
52 fail("Read failure " + filename, e);
54 return Constants.UNDEF;
58 * reads a file into a byte[]
63 public static byte[] fileToByteArray(String filename) {
64 File file = new File(filename);
65 byte[] bytes = new byte[(int) file.length()];
67 try (InputStream is = (FileReader.class.getClassLoader().getResourceAsStream(filename))) {
69 } catch (IOException e) {
70 fail("Read failure " + filename, e);