|
28 | 28 |
|
29 | 29 | import static java.nio.file.attribute.PosixFilePermission.*;
|
30 | 30 |
|
| 31 | +import java.io.IOException; |
| 32 | +import java.io.OutputStream; |
| 33 | +import java.nio.channels.Channels; |
31 | 34 | import java.nio.file.Files;
|
| 35 | +import java.nio.file.OpenOption; |
| 36 | +import java.nio.file.Path; |
32 | 37 | import java.nio.file.StandardOpenOption;
|
33 | 38 | import java.nio.file.attribute.FileAttribute;
|
34 | 39 | import java.nio.file.attribute.PosixFilePermission;
|
@@ -76,20 +81,21 @@ public static void load() {
|
76 | 81 |
|
77 | 82 | private static FileAttribute<Set<PosixFilePermission>> PRIVATE_FILE = PosixFilePermissions.asFileAttribute(Set.of(OWNER_READ, OWNER_WRITE , OWNER_EXECUTE));
|
78 | 83 |
|
| 84 | + private static OutputStream openPrivateStream(Path forFile, OpenOption... flags) throws IOException { |
| 85 | + return Channels.newOutputStream( |
| 86 | + Files.newByteChannel(forFile, Set.of(flags), PRIVATE_FILE) |
| 87 | + ); |
| 88 | + } |
| 89 | + |
79 | 90 | private static void loadLibrary(String path) {
|
80 | 91 | try {
|
81 |
| - var localFile = NativeLibrary.class.getResource(path); |
82 |
| - if (localFile != null && localFile.getProtocol().equals("file")) { |
83 |
| - System.load(localFile.getPath()); |
84 |
| - return; |
85 |
| - } |
86 | 92 | // in most cases the file is inside of a jar
|
87 | 93 | // so we have to copy it out and load that file instead
|
88 | 94 | var localCopy = Files.createTempFile("watch", ".dylib", PRIVATE_FILE);
|
89 | 95 | localCopy.toFile().deleteOnExit();
|
90 | 96 | try (var libStream = NativeLibrary.class.getResourceAsStream(path)) {
|
91 | 97 | if (libStream != null) {
|
92 |
| - try (var writer = Files.newOutputStream(localCopy, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) { |
| 98 | + try (var writer = openPrivateStream(localCopy, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) { |
93 | 99 | libStream.transferTo(writer);
|
94 | 100 | }
|
95 | 101 | System.load(localCopy.toString());
|
|
0 commit comments