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.regoheatpump.internal.rego6xx;
15 import java.util.Arrays;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
20 * The {@link Checksum} is responsible for calculating checksum of given data.
22 * @author Boris Krivonog - Initial contribution
26 static byte calculate(byte[]... lists) {
27 return Arrays.stream(lists).reduce((byte) 0, Checksum::calculate, (a, b) -> b);
30 static byte calculate(byte[] buffer, int offset, int count) {
31 return calculate((byte) 0, buffer, offset, count);
34 private static byte calculate(byte checksum, byte[] buffer) {
35 return calculate(checksum, buffer, 0, buffer.length);
38 private static byte calculate(byte checksum, byte[] buffer, int offset, int count) {
39 byte result = checksum;
40 int end = count + offset;
41 for (int index = offset; index < end; ++index) {
42 result = (byte) (result ^ buffer[index]);