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: 69
Registered: ‎06-10-2009
My Device: Not Specified

Reading bytes from a file starting from an offset

[ Edited ]

I need to read bytes from a file starting on a certain offset. This can be done using InputStream.skip()

but it seems to be very inefficient. Is there a better way of doing this? I'm using JDE 4.5.0

 

Thanks in advance.

Developer
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: Reading bytes from a file starting from an offset

You can use the read method. Just specify the "off"where you want to start reading...

public int read(byte[] b,
                int off,
                int len)
         throws IOException
Developer
Posts: 92
Registered: ‎01-15-2009
My Device: Blackberry 9550

Re: Reading bytes from a file starting from an offset

 Hi. Try to use DataInputStream for this task.

I write some sample code for you

 

 

int newPosition
FileConnection fc = (FileConnection)Connector.open("file:///SDCard/test.txt");
boolean bFileExists = fc.exists();
if (!bFileExists)
{
    Dialog.alert("Cannot find specified file.");
    System.exit(0);
}
DataInputStream in = fc.openDataInputStream();
byte[] widthBytes = new byte[1024];
     
if ( in instanceof Seekable ) 
{
   ((Seekable) in).setPosition(newPosition);
   in.read(widthBytes,0,1024);
} 
in.close();
fc.close();

 

Regards, Dmitry.

 

 

 

New Developer
Posts: 69
Registered: ‎06-10-2009
My Device: Not Specified

Re: Reading bytes from a file starting from an offset

The Javadoc says off - the start offset in array b at which the data is written.

 

I need to set an offset on the file I'm reading from, not the array I' reading into.

Developer
Posts: 92
Registered: ‎01-15-2009
My Device: Blackberry 9550

Re: Reading bytes from a file starting from an offset

Hi. check my post with DataInputStream.

 

Regards, Dmitry.

New Developer
Posts: 69
Registered: ‎06-10-2009
My Device: Not Specified

Re: Reading bytes from a file starting from an offset

Seekable is available since BlackBerry API 5.0.0, and I'm using 4.5.0

Developer
Posts: 713
Registered: ‎05-31-2010
My Device: 8520
My Carrier: Movistar

Re: Reading bytes from a file starting from an offset

Oooo! Yes, "offset" means the offset in the array, sorry but I did not understand clear your question. Well, may skipBytes could work...

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: Reading bytes from a file starting from an offset

Since your opening a FileConnection you can call openOutputStream(long byteOffset) which will open the OutputStream at exactly the point you want. This has been around since 4.2.

 

Oops, I just read "read". In that case you need to either read the the desired number of bytes to reach that point, use skip, or if on 5.0 and higher see if it is a Seekable and seek to that position.

---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.