提交 a4f11c35 authored 作者: Christian Hauser's avatar Christian Hauser

Fix for issue #446: make FILE_READ from classpath working.

上级 4a339e7a
......@@ -18,6 +18,8 @@ import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.NonWritableChannelException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
......@@ -43,6 +45,23 @@ public class FilePathDisk extends FilePath {
@Override
public long size() {
if (name.startsWith(CLASSPATH_PREFIX)) {
try {
String fileName = name.substring(CLASSPATH_PREFIX.length());
// Force absolute resolution in Class.getResource
if (!fileName.startsWith("/")) {
fileName = "/" + fileName;
}
URL resource = this.getClass().getResource(fileName);
if (resource != null) {
return Files.size(Paths.get(this.getClass().getResource(fileName).toURI()));
} else {
return 0;
}
} catch (Exception e) {
return 0;
}
}
return new File(name).length();
}
......
......@@ -648,8 +648,14 @@ public class TestFunctions extends TestDb implements AggregateFunction {
InputStreamReader r = new InputStreamReader(FileUtils.newInputStream(fileName));
String ps2 = IOUtils.readStringAndClose(r, -1);
assertEquals(ps, ps2);
conn.close();
FileUtils.delete(fileName);
// Test classpath prefix using this test class as input
fileName = "/" + this.getClass().getName().replaceAll("\\.", "/") + ".class";
rs = stat.executeQuery("SELECT LENGTH(FILE_READ('classpath:" + fileName + "')) LEN");
rs.next();
int fileSize = rs.getInt(1);
assertTrue(fileSize > 0);
conn.close();
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论