Android Question Random Access file data

RickV

Member
I need some assistance in making the following code work as a random access random record size read and write chunck or fixed width read and write if you will.

Spent a couple of hours reading many posts in the forum, with many coffees LOL, but not sure why I am getting a syntax error on the if and end end if statements.......... any ideas ?


Basically I am writing a zmodem crash recovery protocol to transfer image file data over a tcp-ip socket. Cannot use ftp must be done with this type of implementation as b4x data packets arrive out of sequence in the astreams new data event. (widely documented)

If anyone can see issues, please let me know the correct way.
Thanks

B4X:
    Dim aaa As String
    Dim RAF As RandomAccessFile
    Dim b() As Byte
    Dim x() As Byte
            
    TXRec = Rec ' record number
    RAF.Initialize(File.DirInternalCache, fName, True)
    RAF.CurrentPosition = Rec
    If RAF.Size - (RAF.CurrentPosition + 4096) => 4096 Then '   <------ this line throws syntax error in logs window
            x = RAF.ReadBytes(b, 0, 4096, TXRec)
            TXRec = TXRec + 4096
            aaa = BytesToString(x, 0, x.Length, CharSet)
            SendIt(18550, Pad(fName,20,0) & aaa) 'this handles the encryption, checksums etc and sends the data
        End If  '   <------ this line throws syntax error in logs window

        If RAF.Size - (RAF.CurrentPosition + 2048) => 2048 Then
            x = RAF.ReadBytes(b, 0, 2048, TXRec)
            TXRec = TXRec + 2048
            aaa = BytesToString(x,0,x.Length,CharSet)
            SendIt(18550, Pad(fName,20,0) & aaa)
        End If
'        '''''''  
'        '''''''  
        If RAF.Size - (RAF.CurrentPosition + 1) => 1 Then
            x = RAF.ReadBytes(b, 0, 1, TXRec)
            TXRec = TXRec + 1
            aaa = BytesToString(x,0,x.Length,CharSet)
            SendIt(18550, Pad(fName,20,0) & aaa)
        End If
        If RAF.Size - (RAF.CurrentPosition + 1) > RAF.Size  Then
            SendIt(18551, Pad(fName,20,0))
        End If
    'End If
    RAF.Close
 

teddybear

Well-Known Member
Licensed User
Yes zmodem-ish protocol. I think everyone has the zmdem part in focus and not the actual problem

Lets look at this
B4X:
dim b() as byte
b = File.ReadBytes(File.DirInternal,fName)
This gets the whole file in an array of bytes - a very long string of bytes.
Lets get rid of zmodem out of our heads for a moment, Just look at the file I / O
Rather than get all the bytes in one chunk and move along the string of bytes with say mid(b, x, 4096) I want to open as a random access file and pull a record or string of bytes xo many characters wide,., poke it into my data packet and send it.

I already have all the other stuff, headers, checksums, failsafes etc. I just need to get the file data x bytes wide.

Back in VB6 days it was a case of
Dim Chunk1 as string *4096
Dim Chunk2 as string * 2048

b4x I have discovered

dim Chunk1(4096) as byte
dim Chunk2(2048) as byte

inVB6 we used SEEK to get to a current byte position with the file where we would GET a record, a RANDOM set of sequential bytes x bytes wide.
GET #1 , , Chunk1
GET #1 , , Chunk2

What the b4x equivalent to SEEK ?
How do I move the current position within the file to byte x ?
This is the predominate question. Not the protocol.
1.What the b4x equivalent to SEEK ?
In B4X, the Seek is CurrentPosition of the raf(randomaccessfile), you can do same thing like VB6.
raf.ReadBytes(Chunk1,0,4096,raf.CurrentPosition) 'CurrentPosition += 4096
raf.ReadBytes(Chunk2,0,2048,raf.CurrentPosition)
2.How do I move the current position within the file to byte x ?
raf.CurrentPosition=x
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
So your looking for something like this from Library documentation: RandomAccessFile - v2.32:

Write2 (Buffer() As Byte, Start As Int, Length As Int) As Boolean​

Adds the given bytes array to the output stream queue.

If the object was initialized with InitializePrefix then the array length will be added before the array.
Returns False if the queue is full and it is not possible to queue the data.
 
Upvote 0
Top