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.SeekableByteChannel;
19 import java.util.Arrays;
22 * Wrapper for a {@link SeekableByteChannel} allowing prefixing the stream with a fixed array of bytes
24 * @author Jonathan Gilbert - Initial contribution
26 public class PrefixedSeekableByteChannel implements SeekableByteChannel {
28 private byte[] prefix;
29 private SeekableByteChannel source;
30 private long position;
32 public PrefixedSeekableByteChannel(byte[] prefix, SeekableByteChannel source) {
38 public int read(ByteBuffer dst) throws IOException {
41 if (position < prefix.length) {
42 dst.put(Arrays.copyOfRange(prefix, (int) position, prefix.length));
43 read += prefix.length - position;
46 read += source.read(dst);
54 public int write(ByteBuffer src) throws IOException {
55 throw new IOException("Read only!");
59 public long position() throws IOException {
64 public SeekableByteChannel position(long newPosition) throws IOException {
65 this.position = newPosition;
67 if (newPosition > prefix.length) {
68 source.position(newPosition - prefix.length);
75 public long size() throws IOException {
76 return source.size() + prefix.length;
80 public SeekableByteChannel truncate(long size) throws IOException {
81 throw new IOException("Read only!");
85 public boolean isOpen() {
86 return source.isOpen();
90 public void close() throws IOException {