@@ -1,6 +1,7 @@
|
||||
package net.woggioni.jwo;
|
||||
|
||||
import java.io.FilterOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
|
||||
/**
|
||||
@@ -14,6 +15,7 @@ public class UncloseableOutputStream extends FilterOutputStream {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
public void close() throws IOException {
|
||||
flush();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,5 +10,7 @@ public class UncloseableWriter extends FilterWriter {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {}
|
||||
public void close() throws IOException {
|
||||
flush();
|
||||
}
|
||||
}
|
||||
@@ -7,5 +7,6 @@ module net.woggioni.jwo {
|
||||
exports net.woggioni.jwo;
|
||||
exports net.woggioni.jwo.exception;
|
||||
exports net.woggioni.jwo.url.classpath;
|
||||
exports net.woggioni.jwo.url.jpms;
|
||||
exports net.woggioni.jwo.xml;
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package net.woggioni.jwo.url.jpms;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.Module;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLStreamHandler;
|
||||
import java.util.Optional;
|
||||
|
||||
public class Handler extends URLStreamHandler {
|
||||
private final ClassLoader classLoader;
|
||||
|
||||
public Handler() {
|
||||
this.classLoader = getClass().getClassLoader();
|
||||
}
|
||||
|
||||
public Handler(ClassLoader classLoader) {
|
||||
this.classLoader = classLoader;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected URLConnection openConnection(URL u) throws IOException {
|
||||
final Module thisModule = getClass().getModule();
|
||||
final Module sourceModule = Optional.ofNullable(thisModule)
|
||||
.map(Module::getLayer)
|
||||
.flatMap(layer -> {
|
||||
final String moduleName = u.getHost();
|
||||
return layer.findModule(moduleName);
|
||||
}).orElse(thisModule);
|
||||
return new ModuleResourceURLConnection(u, sourceModule);
|
||||
}
|
||||
|
||||
private static class ModuleResourceURLConnection extends URLConnection {
|
||||
private final Module module;
|
||||
|
||||
private ModuleResourceURLConnection(URL url, Module module) {
|
||||
super(url);
|
||||
this.module = module;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return module.getResourceAsStream(getURL().getPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package net.woggioni.jwo.url;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import net.woggioni.jwo.JWO;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class JpmsUrlTest {
|
||||
@Test
|
||||
@SneakyThrows
|
||||
public void test() {
|
||||
JWO.registerUrlProtocolHandler();
|
||||
final var module = getClass().getModule();
|
||||
final var url = new URL("jpms://net.woggioni.jwo.test.module/my/test/resource/file.txt");
|
||||
final var baos = new ByteArrayOutputStream();
|
||||
try(final var is = url.openConnection().getInputStream()) {
|
||||
JWO.copy(is, baos);
|
||||
}
|
||||
final var content = baos.toString(StandardCharsets.UTF_8);
|
||||
Assertions.assertEquals("test", content);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user