]> git.basschouten.com Git - openhab-addons.git/blob
2474d08eefbbcf61b5c243456207715dfd1e318e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.max.internal.message;
14
15 import java.util.Enumeration;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18
19 /**
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.
23  *
24  * The tokens returned consist of the payload solely, and do not contain the first byte holding the
25  * tokens length.
26  *
27  * @author Andreas Heil (info@aheil.de) - Initial contribution
28  */
29 @NonNullByDefault
30 public final class MaxTokenizer implements Enumeration<byte[]> {
31
32     private int offset;
33
34     private byte[] decodedRawMessage;
35
36     /**
37      * Creates a new MaxTokenizer.
38      *
39      * @param decodedRawMessage
40      *            The Base64 decoded MAX! Cube protocol L message as byte array
41      */
42     public MaxTokenizer(byte[] decodedRawMessage) {
43         this.decodedRawMessage = decodedRawMessage;
44     }
45
46     @Override
47     public boolean hasMoreElements() {
48         return offset < decodedRawMessage.length;
49     }
50
51     @Override
52     public byte[] nextElement() {
53         byte length = decodedRawMessage[offset++];
54
55         // make sure to get the correct length in case > 127
56         byte[] token = new byte[length & 0xFF];
57
58         for (int i = 0; i < (length & 0xFF); i++) {
59             token[i] = decodedRawMessage[offset++];
60         }
61
62         return token;
63     }
64 }