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
Developer
Posts: 88
Registered: ‎05-24-2009
My Device: Not Specified
Accepted Solution

Writing files in Java - insert bytes

Hey.

 

I am using java.io.DataInputStream and  java.io.DataOutputStream to read/write files. However i need to modify a file now, which will require me to insert information into the middle of a previous file.

 

I don't see any methods of doing so in the DataOutputStream. Is there a way to 'insert' into a middle of a file easily on bb, or do i have to do custom -copy/overwrite/append stuff?

 

Best Regards.

Highlighted
Developer
Posts: 1,807
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.3.2.858, Z10 (STL100-3)-10.3.1.2576, Z30 (STA100-5)-10.3.1.2582, Passport (SQW100-1)-10.3.1.2576, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Writing files in Java - insert bytes

If it is a file you can call FileConnection.openOutputStream(long byteOffset). Otherwise you need to make a custom class.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.
Developer
Posts: 562
Registered: ‎09-30-2009
My Device: Not Specified

Re: Writing files in Java - insert bytes

You can seek into the file, but writing at that position will clobber what's there. This desired non-destructive file insert is normally not something that is supported usually elsewhere as well.

Developer
Posts: 88
Registered: ‎05-24-2009
My Device: Not Specified

Re: Writing files in Java - insert bytes

[ Edited ]

Yeah, that wasn't what i needed exactly but after some more designing i came up with another method of doing it. The problem now is that i need to be able to seek within the file forward and backwards. I am using FileConnector and DataInputStream, i can skip bytes to seek forward...but there doesn't seem to be any way to seek backwards. Is there another class i can use for this? Opening and closing a file a few times just so that i can seek from the beginning is not a good idea....:/

 

[EDIT]

Yeah, i know that non-destructive insert isn't usually support elsewhere either but it would be useful...though inherently slow... A/w see the above comments that i made, any ideas how to seek backwards? The DataINputStream doesn't allow that....

Developer
Posts: 562
Registered: ‎09-30-2009
My Device: Not Specified

Re: Writing files in Java - insert bytes

5.0 has Seekable, but the underlying streams have to support it.

Developer
Posts: 88
Registered: ‎05-24-2009
My Device: Not Specified

Re: Writing files in Java - insert bytes

Is there another interface that can be used to access files (not necesseraly as a stream), and would allow seekable features (4.6) ?

Developer
Posts: 562
Registered: ‎09-30-2009
My Device: Not Specified

Re: Writing files in Java - insert bytes

You could do it yourself, but it would be more difficult. You could buffer the file data yourself, but not knowing necessarily where to buffer to or how big to buffer could lead to reconsidering just reopening streams. Depends on your situation.

Developer
Posts: 88
Registered: ‎05-24-2009
My Device: Not Specified

Re: Writing files in Java - insert bytes

Yeah, thats my plan. I'll just read the data into a buffer and parse that....the sizes are predictable so shouldn't be a big perf hit. Too bad that something this basic is missing....Smiley Sad

Developer
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

Re: Writing files in Java - insert bytes

I don't think what you suggest is possible with any of the standard APIs OpenOutputStream (int offset) would overwrite, not replace (to the best of my understanding of these things). I wish you luck, but I don't believe you'll get out of this without reading the whole file into a local source first. On that note, you'll have to be pretty picky about what structure you use if you're manipulating large files. Using byte [] would be efficient (relatively), but you wouldn't be able to insert mid-file very easily. Vectors are great for inserting, but everything needs to be explicitly parsed, and they take up more space and processing power than arrays.

 

Good luck!

~Dom

----------------------------------------------------------------------------
chown -R us ./base
~J!NX
Developer
Posts: 1,807
Registered: ‎04-28-2009
My Device: Z10 (STL100-4)-10.3.2.858, Z10 (STL100-3)-10.3.1.2576, Z30 (STA100-5)-10.3.1.2582, Passport (SQW100-1)-10.3.1.2576, PlayBook (16GB)-2.1.0.1917
My Carrier: Verizon

Re: Writing files in Java - insert bytes

[ Edited ]

Yea, as others have said, the openOutputStream is destructive. Sorry if that didn't help.

---Spends time in #blackberrydev on freenode (IRC)----
Three simple rules:
1. Please use the search bar before making new posts.
2. "Like" posts that you find helpful.
3. If a solution has been found for your post, mark it as solved.
--I code too much. Well, too bad.