Obtaining a bundle as bytes [message #67383] |
Mon, 15 May 2006 07: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
|
|
|
|
Powered by
FUDForum. Page generated in 0.03213 seconds