SoniEx2
New Member
So I heard Terasology has an in-game computers mod. It would be cool to be able to save files from those computers onto my hard drive and send files from my hard drive onto those computers.
The idea I had for this was 2 simple client API methods:
Where loadFile() opens a file selection dialog for opening a file and saveFile opens a file selection dialog for saving a file.
If the user cancels or closes the file selection, or if there's an error opening the file, then loadFile() and saveFile() return empty.
Why use InputStream and OutputStream instead of byte[]? Well it's simple, first InputStream and OutputStream are lazy, which means you won't read the whole file at once (e.g. for 2GB files this could take quite a while), and second because that's the Java way to do it. That way, the mod can control how fast to read and how much to read, as well as how to buffer and stuff.
To prevent wearing out the user's HDD and stuff, seeking aka mark/reset should be disallowed - wrap the returned InputStream in a BufferedInputStream if you need that.
saveFile() takes a filename, not a path. Any path separators and stuff should be stripped from this filename.
While that's my idea for how the API should work, I'm not entirely sure on what the methods should be called... Does loadFile() and saveFile() sound good? They *are* client methods after all, so they don't really transfer any files - they only allow the transfer of files.
The idea I had for this was 2 simple client API methods:
Code:
Optional<InputStream> loadFile();
Optional<OutputStream> saveFile(String);
If the user cancels or closes the file selection, or if there's an error opening the file, then loadFile() and saveFile() return empty.
Why use InputStream and OutputStream instead of byte[]? Well it's simple, first InputStream and OutputStream are lazy, which means you won't read the whole file at once (e.g. for 2GB files this could take quite a while), and second because that's the Java way to do it. That way, the mod can control how fast to read and how much to read, as well as how to buffer and stuff.
To prevent wearing out the user's HDD and stuff, seeking aka mark/reset should be disallowed - wrap the returned InputStream in a BufferedInputStream if you need that.
saveFile() takes a filename, not a path. Any path separators and stuff should be stripped from this filename.
While that's my idea for how the API should work, I'm not entirely sure on what the methods should be called... Does loadFile() and saveFile() sound good? They *are* client methods after all, so they don't really transfer any files - they only allow the transfer of files.