import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.SeekableByteChannel; import java.nio.file.OpenOption; import java.nio.file.Path; import java.nio.file.StandardOpenOption; import java.util.Set; public class InMemoryByteChannel implements SeekableByteChannel { private final Path path; private byte[] data; private int position = 0; private boolean isOpen = true; private final boolean writable; private final boolean forcedIOException; public InMemoryByteChannel(Path path, Set options) { this.path = path; this.writable = options.contains(StandardOpenOption.WRITE) || options.contains(StandardOpenOption.CREATE) || options.contains(StandardOpenOption.CREATE_NEW); this.forcedIOException = path.toString().contains("forcedIOException"); // Load existing data or create new byte[] existingData = InMemoryFileSystem.getStorage().get(path.toString()); if (existingData != null) { this.data = existingData.clone(); } else { this.data = new byte[0]; } if (options.contains(StandardOpenOption.TRUNCATE_EXISTING)) { this.data = new byte[0]; } if (options.contains(StandardOpenOption.APPEND)) { this.position = data.length; } } @Override public int read(ByteBuffer dst) throws IOException { if (!isOpen) { throw new IOException("Channel is closed"); } if (forcedIOException) { throw new IOException("Forced IOException for testing purposes"); } if (position >= data.length) { return -1; // EOF } int remaining = Math.min(dst.remaining(), data.length - position); dst.put(data, position, remaining); position += remaining; return remaining; } @Override public int write(ByteBuffer src) throws IOException { if (!isOpen) { throw new IOException("Channel is closed"); } if (!writable) { throw new IOException("Channel is not writable"); } int bytesToWrite = src.remaining(); int newSize = Math.max(data.length, position + bytesToWrite); if (newSize > data.length) { byte[] newData = new byte[newSize]; System.arraycopy(data, 0, newData, 0, data.length); data = newData; } src.get(data, position, bytesToWrite); position += bytesToWrite; return bytesToWrite; } @Override public long position() throws IOException { if (!isOpen) { throw new IOException("Channel is closed"); } return position; } @Override public SeekableByteChannel position(long newPosition) throws IOException { if (!isOpen) { throw new IOException("Channel is closed"); } if (newPosition < 0) { throw new IllegalArgumentException("Position cannot be negative"); } this.position = (int) newPosition; return this; } @Override public long size() throws IOException { if (!isOpen) { throw new IOException("Channel is closed"); } return data.length; } @Override public SeekableByteChannel truncate(long size) throws IOException { if (!isOpen) { throw new IOException("Channel is closed"); } if (!writable) { throw new IOException("Channel is not writable"); } if (size < data.length) { byte[] newData = new byte[(int) size]; System.arraycopy(data, 0, newData, 0, (int) size); data = newData; if (position > size) { position = (int) size; } } return this; } @Override public boolean isOpen() { return isOpen; } @Override public void close() throws IOException { if (isOpen && writable) { // Save data back to storage InMemoryFileSystem.getStorage().put(path.toString(), data); } isOpen = false; } }