mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-07-12 15:02:52 +00:00

- Separate FileMount into separate FileMount and WritableFileMount classes. This separates the (relatively simple) read-only code from the (soon to be even more complex) read/write code. It also allows you to create read-only mounts which don't bother with filesystem accounting, which is nice. - Make openForWrite/openForAppend always return a SeekableFileHandle. Appendable files still cannot be seeked within, but that check is now done on the FS side. - Refactor the various mount tests to live in test contract interfaces, allowing us to reuse them between mounts. - Clean up our error handling a little better. (Most) file-specific code has been moved to FileMount, and ArchiveMount-derived classes now throw correct path-localised exceptions.
61 lines
1.8 KiB
Java
61 lines
1.8 KiB
Java
/*
|
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
|
* Copyright Daniel Ratcliffe, 2011-2022. Do not distribute without permission.
|
|
* Send enquiries to dratcliffe@gmail.com
|
|
*/
|
|
package dan200.computercraft.test.core.filesystem;
|
|
|
|
import dan200.computercraft.api.filesystem.FileOperationException;
|
|
import dan200.computercraft.api.filesystem.Mount;
|
|
import dan200.computercraft.core.apis.handles.ArrayByteChannel;
|
|
|
|
import java.nio.channels.SeekableByteChannel;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.*;
|
|
|
|
/**
|
|
* A read-only mount {@link Mount} which provides a list of in-memory set of files.
|
|
*/
|
|
public class MemoryMount implements Mount {
|
|
private final Map<String, byte[]> files = new HashMap<>();
|
|
private final Set<String> directories = new HashSet<>();
|
|
|
|
public MemoryMount() {
|
|
directories.add("");
|
|
}
|
|
|
|
@Override
|
|
public boolean exists(String path) {
|
|
return files.containsKey(path) || directories.contains(path);
|
|
}
|
|
|
|
@Override
|
|
public boolean isDirectory(String path) {
|
|
return directories.contains(path);
|
|
}
|
|
|
|
@Override
|
|
public void list(String path, List<String> files) {
|
|
for (var file : this.files.keySet()) {
|
|
if (file.startsWith(path)) files.add(file.substring(path.length() + 1));
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public long getSize(String path) {
|
|
throw new RuntimeException("Not implemented");
|
|
}
|
|
|
|
@Override
|
|
public SeekableByteChannel openForRead(String path) throws FileOperationException {
|
|
var file = files.get(path);
|
|
if (file == null) throw new FileOperationException(path, "File not found");
|
|
return new ArrayByteChannel(file);
|
|
}
|
|
|
|
public MemoryMount addFile(String file, String contents) {
|
|
files.put(file, contents.getBytes(StandardCharsets.UTF_8));
|
|
return this;
|
|
}
|
|
}
|