| 
| Obtaining a bundle as bytes [message #67383] | Mon, 15 May 2006 03:05  |  | 
| Eclipse User  |  |  |  |  | Originally posted by: kkiran7.gmail.com 
 I have two entities , both of them running OSGi containers.
 Each of them can query the other for bundles installed on
 its intance and can request a bundle.
 
 So I would want to send the bundle as bytes. Here is what
 I am doing:
 ------------------------------------------------------------ ---------------------------------
 long bundleId = in.readLong();
 Bundle bundle = context.getBundle( bundleId);
 File file = bundleContext.getDataFile( bundle.getLocation() );
 bytes[] fileBytes = getBytes( file );
 // Here I write the bundle to the outputstream
 out.write(fileBytes, 0 , fileBytes.length);
 
 
 private byte[]  getBytes( File file ) throws IOException{
 byte[] bytes=null;
 int size,offset=0, numRead=0;
 
 size = (int)file.length();//  Integer.MAX_VALUE ~ 4.3 GB. We dont
 transfer such large files
 FileInputStream in =null;
 bytes = new byte[size];
 try{
 in = new FileInputStream( file );
 while( (offset < bytes.length)&&
 ( (numRead = in.read(bytes,offset,bytes.length-offset))>=0)){
 offset+=numRead;
 }
 if( offset < bytes.length) throw new IOException("Could not completely
 read the file "+file.getName());
 return bytes;
 }catch( FileNotFoundException ex){
 throw new IOException("File "+file.getName()+" is not found !");
 }finally{
 if( in!=null ){
 try{
 in.close();
 }catch( Exception ex){}
 }
 }
 }
 
 
 ------------------------------------------------------------ ---------------------------------------
 
 Am I doing the right thing. Please clarify.
 
 Thanks,
 
 Kiran Kuppa
 |  |  |  | 
| 
| Re: Obtaining a bundle as bytes [message #68041 is a reply to message #67383] | Wed, 24 May 2006 14:57  |  | 
| Eclipse User  |  |  |  |  | No that is not really what BundleContext#getDataFile is used for.  That will give your bundle access to a "private" File space where you can
 persist data for your bundle.  Bundle#getLocation will return a
 "location" string but that string can be in any format, it is not
 guaranteed to be a path to the bundle on disk.  In fact in Eclipse it
 does not really look like a file URL at all.
 
 You can use the FileLocator class to get what you want using bundle
 entry URLs to the root of your bundle.  Here is some code to do that ...
 
 import java.io.*;
 import java.net.URL;
 import org.eclipse.core.runtime.FileLocator;
 import org.osgi.framework.Bundle;
 
 ....
 
 public static InputStream getBundleStream(Bundle bundle) throws
 IOException {
 URL root = bundle.getEntry("");
 root = FileLocator.resolve(root);
 if (!"jar".equals(root.getProtocol())) {
 // nothing we can do if it is not a jar URL
 throw new IOException(
 "Bad bundle root URL: " + root.toExternalForm());
 }
 String bundlePath = root.getPath();
 // strip out the file: and !/
 bundlePath = bundlePath.substring(5, bundlePath.lastIndexOf('!'));
 return new FileInputStream(new File(bundlePath));
 }
 
 ....
 
 I hope that helps.
 
 Tom
 |  |  |  | 
Powered by 
FUDForum. Page generated in 0.04446 seconds