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.onewire.internal;
15 import java.nio.ByteBuffer;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.util.HexUtils;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
23 * The {@link OwPageBuffer} provides encapsulates a buffer for OwPacket payloads
25 * @author Jan N. Klug - Initial contribution
29 public class OwPageBuffer {
30 private final Logger logger = LoggerFactory.getLogger(OwPageBuffer.class);
31 public static final int PAGE_SIZE = 8;
33 private ByteBuffer byteBuffer;
36 * constructor for empty buffer
39 public OwPageBuffer() {
40 byteBuffer = ByteBuffer.allocate(0);
44 * constructor for new buffer of given length
46 * @param pageNum number of pages
48 public OwPageBuffer(int pageNum) {
49 byteBuffer = ByteBuffer.allocate(pageNum * PAGE_SIZE);
53 * constructor for given byte array
55 * @param bytes byte array containing the data
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);
62 byteBuffer = ByteBuffer.allocate(bytes.length);
65 byteBuffer.put(bytes);
69 * get number of pages in this buffer
71 * @return number of pages
74 return byteBuffer.limit() / PAGE_SIZE;
78 * get a single page as byte array
80 * @param pageNum page number, starting with 0
81 * @return byte array containing the page's data
83 public byte[] getPage(int pageNum) {
84 byte[] page = new byte[PAGE_SIZE];
85 byteBuffer.position(pageNum * PAGE_SIZE);
93 * @param pageNum page number, starting with 0
94 * @return string representation of the page's data
96 public String getPageString(int pageNum) {
97 return HexUtils.bytesToHex(getPage(pageNum));
101 * get a single byte in a page
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
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;
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);
121 throw new IllegalArgumentException("index out of range");
126 * get this page buffer as byte array
128 * @return this page buffer as byte array
130 public byte[] getBytes() {
131 return byteBuffer.array();
135 public String toString() {
136 StringBuilder s = new StringBuilder();
137 s.append(new String("["));
138 for (int i = 0; i < length(); i++) {
140 s.append(new String(", "));
142 s.append(getPageString(i));
144 s.append(new String("]"));