Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Binary serialization to existing OutputStream
Binary serialization to existing OutputStream [message #1852533] Sat, 21 May 2022 17:12 Go to next message
mila borans is currently offline mila boransFriend
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?
Re: Binary serialization to existing OutputStream [message #1852535 is a reply to message #1852533] Sun, 22 May 2022 07:38 Go to previous message
Ed Merks is currently offline Ed MerksFriend
Messages: 33142
Registered: July 2009
Senior Member
You can save, via org.eclipse.emf.ecore.resource.Resource.save(OutputStream, Map<?, ?>), to any output stream. The URI of the containing resource is still relevant, but only if there are cross reference to other resources.

Ed Merks
Professional Support: https://www.macromodeling.com/
Previous Topic:Why refresh call in PlatformResourceURIHandlerImpl.WorkbenchHelper.createPlatformResourceInputStream
Next Topic:How to get a column with CDO
Goto Forum:
  


Current Time: Fri Apr 26 20:10:13 GMT 2024

Powered by FUDForum. Page generated in 0.02175 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top