2 * Copyright (c) 2010-2021 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;
18 * The {@link Checksum} is responsible for calculating checksum of given data.
20 * @author Boris Krivonog - Initial contribution
23 static byte calculate(byte[]... lists) {
24 return Arrays.stream(lists).reduce((byte) 0, Checksum::calculate, (a, b) -> b);
27 static byte calculate(byte[] buffer, int offset, int count) {
28 return calculate((byte) 0, buffer, offset, count);
31 private static byte calculate(byte checksum, byte[] buffer) {
32 return calculate(checksum, buffer, 0, buffer.length);
35 private static byte calculate(byte checksum, byte[] buffer, int offset, int count) {
36 byte result = checksum;
37 int end = count + offset;
38 for (int index = offset; index < end; ++index) {
39 result = (byte) (result ^ buffer[index]);