2 * Copyright (c) 2010-2024 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.insteon.internal.driver.hub;
15 import java.io.IOException;
16 import java.util.Arrays;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
22 * ReadByteBuffer buffer class
24 * @author Daniel Pfrommer - Initial contribution
25 * @author Rob Nielsen - Port to openHAB 2 insteon binding
28 public class ReadByteBuffer {
29 private byte buf[]; // the actual buffer
30 private int count; // number of valid bytes
31 private int index = 0; // current read index
32 private boolean done = false;
35 * Constructor for ByteArrayIO with dynamic size
37 * @param size initial size, but will grow dynamically
39 public ReadByteBuffer(int size) {
40 this.buf = new byte[size];
46 public synchronized void done() {
52 * Number of unread bytes
54 * @return number of bytes not yet read
56 public synchronized int remaining() {
61 * Blocking read of a single byte
66 public synchronized byte get() throws IOException {
67 while (!done && remaining() < 1) {
70 } catch (InterruptedException e) {
71 throw new IOException("interrupted");
76 throw new IOException("done");
83 * Blocking read of multiple bytes
85 * @param bytes destination array for bytes read
86 * @param off offset into dest array
87 * @param len max number of bytes to read into dest array
88 * @return number of bytes actually read
91 public synchronized int get(byte @Nullable [] bytes, int off, int len) throws IOException {
92 while (!done && remaining() < 1) {
95 } catch (InterruptedException e) {
96 throw new IOException("interrupted");
101 throw new IOException("done");
104 int b = Math.min(len, remaining());
106 System.arraycopy(buf, index, bytes, off, b);
113 * Adds bytes to the byte buffer
115 * @param b byte array with new bytes
116 * @param off starting offset into buffer
117 * @param len number of bytes to add
119 private synchronized void add(byte b[], int off, int len) {
120 if ((off < 0) || (off > b.length) || (len < 0) || ((off + len) > b.length) || ((off + len) < 0)) {
121 throw new IndexOutOfBoundsException();
122 } else if (len == 0) {
125 int nCount = count + len;
126 if (nCount > buf.length) {
127 // dynamically grow the array
128 buf = Arrays.copyOf(buf, Math.max(buf.length << 1, nCount));
130 // append new data to end of buffer
131 System.arraycopy(b, off, buf, count, len);
137 * Adds bytes to the byte buffer
139 * @param b the new bytes to be added
141 public void add(byte[] b) {
146 * Shrink the buffer to smallest size possible
148 public synchronized void makeCompact() {
152 byte[] newBuf = new byte[remaining()];
153 System.arraycopy(buf, index, newBuf, 0, newBuf.length);
155 count = newBuf.length;