]> git.basschouten.com Git - openhab-addons.git/blob
b477f21826cdcafb118db87f98aa9fec50d4cd52
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.onewire.internal;
14
15 import java.nio.ByteBuffer;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.util.HexUtils;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * The {@link OwPageBuffer} provides encapsulates a buffer for OwPacket payloads
24  *
25  * @author Jan N. Klug - Initial contribution
26  */
27
28 @NonNullByDefault
29 public class OwPageBuffer {
30     private final Logger logger = LoggerFactory.getLogger(OwPageBuffer.class);
31     public static final int PAGE_SIZE = 8;
32
33     private ByteBuffer byteBuffer;
34
35     /**
36      * constructor for empty buffer
37      *
38      */
39     public OwPageBuffer() {
40         byteBuffer = ByteBuffer.allocate(0);
41     }
42
43     /**
44      * constructor for new buffer of given length
45      *
46      * @param pageNum number of pages
47      */
48     public OwPageBuffer(int pageNum) {
49         byteBuffer = ByteBuffer.allocate(pageNum * PAGE_SIZE);
50     }
51
52     /**
53      * constructor for given byte array
54      *
55      * @param bytes byte array containing the data
56      */
57     public OwPageBuffer(byte[] bytes) {
58         if (bytes.length % PAGE_SIZE != 0) {
59             byteBuffer = ByteBuffer.allocate((bytes.length / PAGE_SIZE + 1) * PAGE_SIZE);
60             logger.warn("initializing buffer which is not aligned to pages (requested size is {})!", bytes.length);
61         } else {
62             byteBuffer = ByteBuffer.allocate(bytes.length);
63         }
64
65         byteBuffer.put(bytes);
66     }
67
68     /**
69      * get number of pages in this buffer
70      *
71      * @return number of pages
72      */
73     public int length() {
74         return byteBuffer.limit() / PAGE_SIZE;
75     }
76
77     /**
78      * get a single page as byte array
79      *
80      * @param pageNum page number, starting with 0
81      * @return byte array containing the page's data
82      */
83     public byte[] getPage(int pageNum) {
84         byte[] page = new byte[PAGE_SIZE];
85         byteBuffer.position(pageNum * PAGE_SIZE);
86         byteBuffer.get(page);
87         return page;
88     }
89
90     /**
91      * get a single page
92      *
93      * @param pageNum page number, starting with 0
94      * @return string representation of the page's data
95      */
96     public String getPageString(int pageNum) {
97         return HexUtils.bytesToHex(getPage(pageNum));
98     }
99
100     /**
101      * get a single byte in a page
102      *
103      * @param pageNum page number, starting with 0
104      * @param byteNum byte number, starting from 0 (beginning of page)
105      * @return integer of the requested byte
106      */
107     public int getByte(int pageNum, int byteNum) {
108         int index = pageNum * PAGE_SIZE + byteNum;
109         if (index < byteBuffer.limit()) {
110             return byteBuffer.get(index) & 0xFF;
111         } else {
112             return 0;
113         }
114     }
115
116     public void setByte(int pageNum, int byteNum, byte value) {
117         int index = pageNum * PAGE_SIZE + byteNum;
118         if (index < byteBuffer.limit()) {
119             byteBuffer.put(index, value);
120         } else {
121             throw new IllegalArgumentException("index out of range");
122         }
123     }
124
125     /**
126      * get this page buffer as byte array
127      *
128      * @return this page buffer as byte array
129      */
130     public byte[] getBytes() {
131         return byteBuffer.array();
132     }
133
134     @Override
135     public String toString() {
136         StringBuilder s = new StringBuilder();
137         s.append(new String("["));
138         for (int i = 0; i < length(); i++) {
139             if (i > 0) {
140                 s.append(new String(", "));
141             }
142             s.append(getPageString(i));
143         }
144         s.append(new String("]"));
145         return s.toString();
146     }
147 }