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
14 package org.openhab.automation.jsscripting.internal.fs;
16 import java.io.IOException;
17 import java.nio.ByteBuffer;
18 import java.nio.channels.ClosedChannelException;
19 import java.nio.channels.SeekableByteChannel;
22 * Simple wrapper around a byte array to provide a SeekableByteChannel for consumption
24 * @author Dan Cunningham - Initial contribution
26 public class ReadOnlySeekableByteArrayChannel implements SeekableByteChannel {
29 private boolean closed;
31 public ReadOnlySeekableByteArrayChannel(byte[] data) {
36 public long position() {
41 public SeekableByteChannel position(long newPosition) throws IOException {
43 position = (int) Math.max(0, Math.min(newPosition, size()));
53 public int read(ByteBuffer buf) throws IOException {
55 int remaining = (int) size() - position;
59 int readBytes = buf.remaining();
60 if (readBytes > remaining) {
61 readBytes = remaining;
63 buf.put(data, position, readBytes);
64 position += readBytes;
74 public boolean isOpen() {
79 public int write(ByteBuffer b) throws IOException {
80 throw new UnsupportedOperationException();
84 public SeekableByteChannel truncate(long newSize) {
85 throw new UnsupportedOperationException();
88 private void ensureOpen() throws ClosedChannelException {
90 throw new ClosedChannelException();