|
9 | 9 | import java.io.FileNotFoundException; |
10 | 10 | import java.io.IOException; |
11 | 11 | import java.io.InputStream; |
12 | | -import java.lang.invoke.MethodHandle; |
13 | | -import java.lang.invoke.MethodHandles; |
14 | | -import java.lang.invoke.MethodType; |
15 | | -import java.nio.channels.FileChannel; |
16 | 12 | import java.nio.channels.SeekableByteChannel; |
17 | 13 | import java.nio.file.AccessMode; |
18 | 14 | import java.nio.file.DirectoryStream; |
|
42 | 38 | import java.util.stream.IntStream; |
43 | 39 | import java.util.stream.StreamSupport; |
44 | 40 |
|
45 | | -import net.minecraftforge.unsafe.UnsafeHacks; |
| 41 | +import cpw.mods.util.ZipUtils; |
46 | 42 |
|
47 | 43 | public class UnionFileSystem extends FileSystem { |
48 | | - private static final MethodHandle ZIPFS_EXISTS; |
49 | | - private static final MethodHandle ZIPFS_CH; |
50 | | - private static final MethodHandle FCI_UNINTERUPTIBLE; |
51 | 44 | static final String SEP_STRING = "/"; |
52 | 45 |
|
53 | | - static { |
54 | | - try { |
55 | | - var hackfield = MethodHandles.Lookup.class.getDeclaredField("IMPL_LOOKUP"); |
56 | | - UnsafeHacks.setAccessible(hackfield); |
57 | | - MethodHandles.Lookup hack = (MethodHandles.Lookup) hackfield.get(null); |
58 | | - |
59 | | - var clz = Class.forName("jdk.nio.zipfs.ZipPath"); |
60 | | - ZIPFS_EXISTS = hack.findSpecial(clz, "exists", MethodType.methodType(boolean.class), clz); |
61 | | - |
62 | | - clz = Class.forName("jdk.nio.zipfs.ZipFileSystem"); |
63 | | - ZIPFS_CH = hack.findGetter(clz, "ch", SeekableByteChannel.class); |
64 | | - |
65 | | - clz = Class.forName("sun.nio.ch.FileChannelImpl"); |
66 | | - FCI_UNINTERUPTIBLE = hack.findSpecial(clz, "setUninterruptible", MethodType.methodType(void.class), clz); |
67 | | - } catch (NoSuchFieldException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException e) { |
68 | | - throw new RuntimeException(e); |
69 | | - } |
70 | | - } |
71 | | - |
72 | 46 | public InputStream buildInputStream(final UnionPath path) { |
73 | 47 | try { |
74 | 48 | var bytes = Files.readAllBytes(path); |
@@ -142,11 +116,9 @@ public UnionFileSystem(final UnionFileSystemProvider provider, final BiPredicate |
142 | 116 | private static Optional<EmbeddedFileSystemMetadata> openFileSystem(final Path path) { |
143 | 117 | try { |
144 | 118 | var zfs = FileSystems.newFileSystem(path); |
145 | | - SeekableByteChannel fci = (SeekableByteChannel) ZIPFS_CH.invoke(zfs); |
146 | | - if (fci instanceof FileChannel) { // we only make file channels uninterruptible because byte channels (JIJ) already are |
147 | | - FCI_UNINTERUPTIBLE.invoke(fci); |
148 | | - } |
149 | | - return Optional.of(new EmbeddedFileSystemMetadata(path, zfs, fci)); |
| 119 | + SeekableByteChannel ch = ZipUtils.getByteChannel(zfs); |
| 120 | + ZipUtils.setUninterruptible(ch); |
| 121 | + return Optional.of(new EmbeddedFileSystemMetadata(path, zfs, ch)); |
150 | 122 | } catch (IOException e) { |
151 | 123 | throw new UncheckedIOException(e); |
152 | 124 | } catch (Throwable t) { |
@@ -245,7 +217,7 @@ private Optional<BasicFileAttributes> getFileAttributes(final Path path) { |
245 | 217 | private static boolean zipFsExists(UnionFileSystem ufs, Path path) { |
246 | 218 | try { |
247 | 219 | if (Optional.ofNullable(ufs.embeddedFileSystems.get(path.getFileSystem())).filter(efs->!efs.fsCh.isOpen()).isPresent()) throw new IllegalStateException("The zip file has closed!"); |
248 | | - return (boolean) ZIPFS_EXISTS.invoke(path); |
| 220 | + return ZipUtils.exists(path); |
249 | 221 | } catch (Throwable t) { |
250 | 222 | throw new IllegalStateException(t); |
251 | 223 | } |
|
0 commit comments