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.max.internal.message;
15 import java.util.Enumeration;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
20 * The MaxTokenizer parses a L message into the MAX! devices encoded within. The L message contains
21 * real time information for multiple devices. Each device starts with the length n bytes.
22 * The MaxTokenzier starts with the first device and chops off one device after another from the byte stream.
24 * The tokens returned consist of the payload solely, and do not contain the first byte holding the
27 * @author Andreas Heil (info@aheil.de) - Initial contribution
30 public final class MaxTokenizer implements Enumeration<byte[]> {
34 private byte[] decodedRawMessage;
37 * Creates a new MaxTokenizer.
39 * @param decodedRawMessage
40 * The Base64 decoded MAX! Cube protocol L message as byte array
42 public MaxTokenizer(byte[] decodedRawMessage) {
43 this.decodedRawMessage = decodedRawMessage;
47 public boolean hasMoreElements() {
48 return offset < decodedRawMessage.length;
52 public byte[] nextElement() {
53 byte length = decodedRawMessage[offset++];
55 // make sure to get the correct length in case > 127
56 byte[] token = new byte[length & 0xFF];
58 for (int i = 0; i < (length & 0xFF); i++) {
59 token[i] = decodedRawMessage[offset++];