Skip to content

Commit

Permalink
#168 Handle windows file modes
Browse files Browse the repository at this point in the history
  • Loading branch information
mmhelloworld authored Feb 1, 2024
1 parent 9d17b4c commit cf6844a
Showing 1 changed file with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.nio.file.NoSuchFileException;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.PosixFilePermission;
Expand All @@ -31,6 +30,11 @@

import static io.github.mmhelloworld.idrisjvm.runtime.Directories.getWorkingDir;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.StandardOpenOption.APPEND;
import static java.nio.file.StandardOpenOption.CREATE;
import static java.nio.file.StandardOpenOption.READ;
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING;
import static java.nio.file.StandardOpenOption.WRITE;
import static java.nio.file.attribute.PosixFilePermission.GROUP_EXECUTE;
import static java.nio.file.attribute.PosixFilePermission.GROUP_READ;
import static java.nio.file.attribute.PosixFilePermission.GROUP_WRITE;
Expand Down Expand Up @@ -208,18 +212,23 @@ private static boolean isReadOnlyMode(String mode) {
private static Collection<OpenOption> getOpenOptions(String mode) {
switch (mode.toLowerCase()) {
case "r":
return singletonList(StandardOpenOption.READ);
case "rb":
return singletonList(READ);
case "w":
return asList(StandardOpenOption.CREATE, StandardOpenOption.WRITE,
StandardOpenOption.TRUNCATE_EXISTING);
case "wb":
return asList(CREATE, WRITE, TRUNCATE_EXISTING);
case "a":
return asList(StandardOpenOption.CREATE, StandardOpenOption.APPEND);
case "ab":
return asList(CREATE, APPEND);
case "r+":
return asList(StandardOpenOption.READ, StandardOpenOption.WRITE);
case "rb+":
return asList(READ, WRITE);
case "w+":
return asList(StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE);
case "wb+":
return asList(CREATE, READ, WRITE);
case "a+":
return asList(StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.APPEND);
case "ab+":
return asList(CREATE, READ, APPEND);
default:
throw new IllegalArgumentException("Unknown file mode " + mode);
}
Expand Down

0 comments on commit cf6844a

Please sign in to comment.