Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 15
Registered: ‎08-21-2009
My Device: Not Specified
Accepted Solution

JSR75 - openOutputStream() and write() truncates file

Hello all,

 

I'm developing my first BlackBerry app, targeting the 8800 device (4.2 emulator).

 

From the API javadoc for FileConnection.openOutputStream(long byteOffset): "Data written to the returned output stream at that position overwrites any existing data until EOF is reached, and then additional data is appended."

 

Which is fine, but if EOF is not reached, I expect the remaining file content to be preserved.

 

When writing to an SD card, this is the case, but when writing to the device store I am seeing all remaining data truncated from the file.  Here is a sample app to demonstrate:

 

 

import java.io.OutputStream; import javax.microedition.io.Connector; import javax.microedition.io.file.FileConnection; import net.rim.device.api.system.Application; public class JSR75Test extends Application { private JSR75Test() throws Exception { super(); String text1 = "This is 30 bytes worth of text"; String text2 = "Overwrite"; // File on SD card - works as expected // FileConnection fc = (FileConnection)Connector.open("file:///SDCard/BlackBerry/test.txt"); // File on device store - does not work FileConnection fc = (FileConnection)Connector.open("file:///store/home/user/test.txt"); if (!fc.exists()) fc.create(); fc.truncate(0); OutputStream out = fc.openOutputStream(); out.write(text1.getBytes()); out.close(); System.out.println("Wrote 30 bytes of data, and file size is " + fc.fileSize()); OutputStream out2 = fc.openOutputStream(0); // Re-open file at byte 0 out2.write(text2.getBytes()); // This should overwrite 9 bytes only out2.close(); // Device store prints "but is 9" // SD card prints "but is 30" System.out.println("Overwrote 9 bytes from position 0, file size should still be 30, but is " + fc.fileSize()); fc.close(); System.exit(0); } public static void main(String[] args) throws Exception { new JSR75Test(); } }

 

Is this a known bug in the API (and is there a workaround?), or am I missing something?

 

 

Many Thanks...

 

Highlighted
Developer
Posts: 166
Registered: ‎05-07-2009
My Device: Not Specified

Re: JSR75 - openOutputStream() and write() truncates file

[ Edited ]
I don't know if it is known bug but this is definitely a bug and I can reproduce it in my 4.2 simulator. AFAICS the bug is in handling of fc.openOutputStream(0). In this case file is treated as newly created and old content is wipped off by the first flush. On the other hand this means that there is an ugly workaround: don't use first byte of your files and use openOutputStream(1).
Message Edited by SergGr on 08-21-2009 03:24 PM

--------------------------------------------------------------------------------------------------------
If your issue is solved, set "Solution" mark at the relevant post.
Don't hesitate to Kudos people whose posts helped you.
New Developer
Posts: 15
Registered: ‎08-21-2009
My Device: Not Specified

Re: JSR75 - openOutputStream() and write() truncates file

Ah, good spot.  Your suggestion to start from byte 1 will probably work for me if I need to stick with 4.2.

 

I've done more testing on the other OS versions and it looks like this bug was fixed in 4.3

 

Thanks for a swift and helpful reply.

 

Developer
Posts: 166
Registered: ‎05-07-2009
My Device: Not Specified

Re: JSR75 - openOutputStream() and write() truncates file

You are welcomed Smiley Happy

And it was interesting to debug OS internals as I like it.


--------------------------------------------------------------------------------------------------------
If your issue is solved, set "Solution" mark at the relevant post.
Don't hesitate to Kudos people whose posts helped you.