Skip to content


Loading sibling cods using CodeModuleManager

I recently had occasion to use the CodeModuleManager to load a module onto the BlackBerry after downloading it from a remote server. This was a bit tricky because the javadocs from RIM about CodeModuleManager are at best misleading.


The Javadocs (from 4.7) say:

How to add sibling .cod files.

You need to write the data in two seperate chunks. The first data chunk must be less thank 64KB in size.

1. To write the first data chunk, invoke createNewModule( int totalLength, byte[] data, int length ).
2. To write the second data chunk, invoke writeNewModule( int newModuleHandle, byte[] data, int newModuleOffset, int length ).

First of all, the writeNewModule method metioned is deprecated! But that’s not the worst problem - I’ll explain.

If we have an application with 3 sibling cods, with a total size of say 150000 bytes, you might think (as I did) that the above docs imply that you need to do something like this:

	private void writeModule(byte[] cod1, byte[] cod2, byte[] cod3) {
		int handle = CodeModuleManager.createNewModule(150000, cod1, cod1.length);
		boolean result = CodeModuleManager.writeNewModule(handle, cod1.length, cod2, 0, cod2.length);
		result = CodeModuleManager.writeNewModule(handle, cod1.length + cod2.length, cod3, 0, cod3.length);
		CodeModuleManager.saveNewModule(handle);
	}

You would be wrong! The actual way to do it is:

	private void writeModule(byte[] cod1, byte[] cod2, byte[] cod3) {
		int handle1 = CodeModuleManager.createNewModule(cod1.length, cod1, cod1.length);
		CodeModuleManager.saveNewModule(handle1);
		int handle2 = CodeModuleManager.createNewModule(cod2.length, cod2, cod2.length);
		CodeModuleManager.saveNewModule(handle2);
		int handle3 = CodeModuleManager.createNewModule(cod3.length, cod3, cod3.length);
		CodeModuleManager.saveNewModule(handle3);
	}

The BlackBerry OS knows enough to put the sibling cods together into the same module.

So the above Javadocs are wrong - you only need to write the data in two separate chunks, for an individual sibling cod file, if that cod file is bigger than 64kb. So actually the above code sample will work only if your sibling cods are each less than 64kb. If one of them is more you’ll need to do this:

		int handle1 = CodeModuleManager.createNewModule(cod1.length, cod1, 64000);
		CodeModuleManager.writeNewModule(handle1, 64000, cod1, 64000, cod1.length-64000);
		CodeModuleManager.saveNewModule(handle1);

Of course, only if cod1.length > 64k (I just used 64000 here to be on the safe side - you could actually use 65536 (or maybe 65535?)

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google

Posted in advanced. Tagged with , , , , .

5 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Remember that sibling cods are most 64k code + 64k data (static stuff), so a sibling COD with resources / code can be 70-80k

  2. You’re right about the cod sizes - and that’s why I added that last bit of code. It seems like you’re limited to writing 64k of a single sibling cod at a time. Note it’s implicitly assuming the cod size will be less than 64k + 64k - which is as you point out the largest size for a sibling cod. (Actually the code is slightly wrong as I’m using 64000 + 64000 bytes).

  3. I am new to BlackBerry programming and am a little confused about the mention of multiple .cod files. All of the applications I have made consist of a single cod file. At what point do multiple cod files come into play? Why would I need to use multiple cod files?

  4. Cyril said

    If your application become bigger you will have maybe 2 or more cods files if your app excess ~70ko your can rename the generated cod into zip and extract it you will see the different cods
    The multiple cod files is a limit imposed by rim

  5. Theja said

    Hi, I guess I understood your explanation. I have a situation where I have Sample.cod file which is actually a zip file contains two sibbling cod files like sample.cod and sample_1.cod.

    I am downloading directly sample.cod (ie zip ) file. Is there a way to install it throught CodeModuleManager.

    Regards
    Theja

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.