import java.io.IOException; import java.net.URI; import java.nio.channels.SeekableByteChannel; import java.nio.file.*; import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.FileAttribute; import java.nio.file.attribute.FileAttributeView; import java.nio.file.spi.FileSystemProvider; import java.util.*; import java.util.concurrent.ConcurrentHashMap; public class InMemoryFileSystemProvider extends FileSystemProvider { private static final String SCHEME = "memory"; private final Map fileSystems = new ConcurrentHashMap<>(); @Override public String getScheme() { return SCHEME; } @Override public FileSystem newFileSystem(URI uri, Map env) throws IOException { String key = uri.getHost(); if (key == null) key = "default"; InMemoryFileSystem fs = new InMemoryFileSystem(this); fileSystems.put(key, fs); return fs; } @Override public FileSystem getFileSystem(URI uri) { String key = uri.getHost(); if (key == null) key = "default"; InMemoryFileSystem fs = fileSystems.get(key); if (fs == null) { throw new FileSystemNotFoundException(); } return fs; } @Override public Path getPath(URI uri) { return getFileSystem(uri).getPath(uri.getPath()); } @Override public SeekableByteChannel newByteChannel(Path path, Set options, FileAttribute... attrs) throws IOException { return new InMemoryByteChannel(path, options); } @Override public DirectoryStream newDirectoryStream(Path dir, DirectoryStream.Filter filter) throws IOException { throw new UnsupportedOperationException("Directory operations not supported"); } @Override public void createDirectory(Path dir, FileAttribute... attrs) throws IOException { // For simplicity, we'll just track that this directory exists InMemoryFileSystem.getStorage().put(dir.toString() + "/", new byte[0]); } @Override public void delete(Path path) throws IOException { if (InMemoryFileSystem.getStorage().remove(path.toString()) == null) { throw new NoSuchFileException(path.toString()); } } @Override public void copy(Path source, Path target, CopyOption... options) throws IOException { throw new UnsupportedOperationException("Copy operation not supported"); } @Override public void move(Path source, Path target, CopyOption... options) throws IOException { throw new UnsupportedOperationException("Move operation not supported"); } @Override public boolean isSameFile(Path path, Path path2) throws IOException { throw new UnsupportedOperationException("isSameFile not supported"); } @Override public boolean isHidden(Path path) throws IOException { throw new UnsupportedOperationException("Hidden file operations not supported"); } @Override public FileStore getFileStore(Path path) throws IOException { throw new UnsupportedOperationException("FileStore operations not supported"); } @Override public void checkAccess(Path path, AccessMode... modes) throws IOException { throw new UnsupportedOperationException("Check access not supported"); } @Override public V getFileAttributeView(Path path, Class type, LinkOption... options) { throw new UnsupportedOperationException("File attribute views not supported"); } @Override public A readAttributes(Path path, Class type, LinkOption... options) throws IOException { throw new UnsupportedOperationException("File attributes not supported"); } @Override public Map readAttributes(Path path, String attributes, LinkOption... options) throws IOException { throw new UnsupportedOperationException("Reading attributes not supported"); } @Override public void setAttribute(Path path, String attribute, Object value, LinkOption... options) throws IOException { throw new UnsupportedOperationException("Setting attributes not supported"); } }