Skip to content

Commit 7cd8481

Browse files
authored
Add lastModified&contentLength to SJH URLConnections (#55)
Implement last modification time and content length in URLConnection for SJH modular class loaders. Enables support for javacpp. javacpp tries to extract the native DLLs it is wrapping into a cache directory. In normal operation, it retrieves the lastModifiedDate+size from a JarURLConnection to handle conflicts. When using javacpp in Neoforge, this fails. javacpp cannot use its JarUrlConnection special case and will fall back to using the generic URLConnection methods `getContentLength` and `getLastModified`, which SJH does not implement. That leads to crashes (since javacpp can no longer detect if the DLL it is trying to extract is different from the one already on disk). Implementing these two methods in SJH fixes the issue. Fixes bytedeco/javacpp#697
1 parent 67e23de commit 7cd8481

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

src/main/java/cpw/mods/cl/ModularURLHandler.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,35 @@ public InputStream getInputStream() throws IOException {
6767
throw e.getCause();
6868
}
6969
}
70+
71+
@Override
72+
public int getContentLength() {
73+
var length = getContentLengthLong();
74+
if (length < 0 || length > Integer.MAX_VALUE) {
75+
return -1;
76+
}
77+
return (int) length;
78+
}
79+
80+
@Override
81+
public long getContentLengthLong() {
82+
return provider.getContentLength(url);
83+
}
84+
85+
@Override
86+
public long getLastModified() {
87+
return provider.getLastModified(url);
88+
}
89+
7090
}
7191
public interface IURLProvider {
7292
String protocol();
7393
Function<URL, InputStream> inputStreamFunction();
94+
default long getLastModified(URL url) {
95+
return 0;
96+
}
97+
default long getContentLength(URL url) {
98+
return -1;
99+
}
74100
}
75101
}

src/main/java/cpw/mods/cl/UnionURLStreamHandler.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,26 @@ public Function<URL, InputStream> inputStreamFunction() {
3333

3434
};
3535
}
36+
37+
@Override
38+
public long getLastModified(URL u) {
39+
try {
40+
return Files.getLastModifiedTime(Paths.get(u.toURI())).toMillis();
41+
} catch (URISyntaxException e) {
42+
throw new RuntimeException(e);
43+
} catch (IOException e) {
44+
throw new UncheckedIOException(e);
45+
}
46+
}
47+
48+
@Override
49+
public long getContentLength(URL u) {
50+
try {
51+
return Files.size(Paths.get(u.toURI()));
52+
} catch (URISyntaxException e) {
53+
throw new RuntimeException(e);
54+
} catch (IOException e) {
55+
throw new UncheckedIOException(e);
56+
}
57+
}
3658
}

0 commit comments

Comments
 (0)