Binary serialization to existing OutputStream [message #1852533] |
Sat, 21 May 2022 17:12 |
mila borans Messages: 1 Registered: May 2022 |
Junior Member |
|
|
The following snippet is taken from a working code, it persists two 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); //file extension is .bin
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?
|
|
|
|
Powered by
FUDForum. Page generated in 0.01768 seconds