如何将EMF对象持续到OutputStream?
这是我如何将两个EMF对象(静态架子
和动态md5
)持续到一个zipped二进制文件中:
static void save(Shelf shelf, String file, EClass md5class) throws Exception {
EObject md5 = stringToMD5EObject("0x0abc", md5class);
Resource resource = createResource(file, md5class.getEPackage(), shelf.eClass().getEPackage());
resource.getContents().add(md5);
resource.getContents().add(shelf);
resource.save(options());
}
static Resource createResource(String file, EPackage... packages) {
ResourceSet resourceSet = new ResourceSetImpl();
Resource.Factory factory = uri -> new BinaryResourceImpl(uri);
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("bin", factory);
for (EPackage p : packages) resourceSet.getPackageRegistry().put(p.getNsURI(), p);
URI fileURI = URI.createFileURI(new File(file).getAbsolutePath());
return resourceSet.createResource(fileURI);
}
static Map<String, Object> options() {
return Collections.singletonMap(BinaryResourceImpl.OPTION_ZIP, Boolean.TRUE);
}
问题是,如果我给出了我一个,则如何配置资源已经打开 outputStream,那是没有对文件名的访问?
This how I persist two EMF objects (a static shelf
and a dynamic md5
) into a zipped binary file:
static void save(Shelf shelf, String file, EClass md5class) throws Exception {
EObject md5 = stringToMD5EObject("0x0abc", md5class);
Resource resource = createResource(file, md5class.getEPackage(), shelf.eClass().getEPackage());
resource.getContents().add(md5);
resource.getContents().add(shelf);
resource.save(options());
}
static Resource createResource(String file, EPackage... packages) {
ResourceSet resourceSet = new ResourceSetImpl();
Resource.Factory factory = uri -> new BinaryResourceImpl(uri);
resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("bin", factory);
for (EPackage p : packages) resourceSet.getPackageRegistry().put(p.getNsURI(), p);
URI fileURI = URI.createFileURI(new File(file).getAbsolutePath());
return resourceSet.createResource(fileURI);
}
static Map<String, Object> options() {
return Collections.singletonMap(BinaryResourceImpl.OPTION_ZIP, Boolean.TRUE);
}
The question is how to configure the resource if I'm given an already open OutputStream, that is without no access whatsoever to the filename?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是供将来参考的解决方案:
Here's a solution for future reference: